-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
TestHelper.js
183 lines (176 loc) · 5.63 KB
/
TestHelper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import Onyx from 'react-native-onyx';
import Str from 'expensify-common/lib/str';
import CONST from '../../src/CONST';
import * as Session from '../../src/libs/actions/Session';
import HttpUtils from '../../src/libs/HttpUtils';
import ONYXKEYS from '../../src/ONYXKEYS';
import waitForPromisesToResolve from './waitForPromisesToResolve';
import * as ReportUtils from '../../src/libs/ReportUtils';
import * as NumberUtils from '../../src/libs/NumberUtils';
/**
* @param {String} login
* @param {Number} accountID
* @param {String} [firstName]
* @returns {Object}
*/
function buildPersonalDetails(login, accountID, firstName = 'Test') {
const avatar = ReportUtils.getDefaultAvatar(login);
return {
accountID,
login,
avatar,
avatarThumbnail: avatar,
displayName: `${firstName} User`,
firstName,
lastName: 'User',
pronouns: '',
timezone: CONST.DEFAULT_TIME_ZONE,
payPalMeAddress: '',
phoneNumber: '',
};
}
/**
* Simulate signing in and make sure all API calls in this flow succeed. Every time we add
* a mockImplementationOnce() we are altering what Network.post() will return.
*
* @param {Number} [accountID]
* @param {String} [login]
* @param {String} [password]
* @param {String} [authToken]
* @param {String} [firstName]
* @return {Promise}
*/
function signInWithTestUser(accountID = 1, login = 'test@user.com', password = 'Password1', authToken = 'asdfqwerty', firstName = 'Test') {
const originalXhr = HttpUtils.xhr;
HttpUtils.xhr = jest.fn();
HttpUtils.xhr.mockImplementation(() => Promise.resolve({
onyxData: [
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: ONYXKEYS.CREDENTIALS,
value: {
login,
},
},
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
validated: true,
},
},
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: ONYXKEYS.PERSONAL_DETAILS,
value: {
[login]: buildPersonalDetails(login, accountID, firstName),
},
},
],
jsonCode: 200,
}));
// Simulate user entering their login and populating the credentials.login
Session.beginSignIn(login);
return waitForPromisesToResolve()
.then(() => {
// Response is the same for calls to Authenticate and CreateLogin
HttpUtils.xhr
.mockImplementation(() => Promise.resolve({
onyxData: [
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: ONYXKEYS.SESSION,
value: {
authToken,
accountID,
email: login,
encryptedAuthToken: authToken,
},
},
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: ONYXKEYS.CREDENTIALS,
value: {
autoGeneratedLogin: Str.guid('expensify.cash-'),
autoGeneratedPassword: Str.guid(),
},
},
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: ONYXKEYS.USER,
value: {
isUsingExpensifyCard: false,
},
},
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: ONYXKEYS.BETAS,
value: ['all'],
},
],
jsonCode: 200,
}));
Session.signIn(password);
return waitForPromisesToResolve()
.then(() => {
HttpUtils.xhr = originalXhr;
});
});
}
/**
* Use for situations where fetch() is required.
*
* @example
*
* beforeAll(() => {
* global.fetch = TestHelper.getGlobalFetchMock();
* });
*
* @returns {Function}
*/
function getGlobalFetchMock() {
return jest.fn()
.mockResolvedValue({
ok: true,
json: () => Promise.resolve({
jsonCode: 200,
}),
});
}
/**
* @param {String} login
* @param {Number} accountID
* @returns {Promise}
*/
function setPersonalDetails(login, accountID) {
Onyx.merge(ONYXKEYS.PERSONAL_DETAILS, {
[login]: buildPersonalDetails(login, accountID),
});
return waitForPromisesToResolve();
}
/**
* @param {String} actorEmail
* @param {String} created
* @param {Number} actorAccountID
* @param {String} actionID
* @returns {Object}
*/
function buildTestReportComment(actorEmail, created, actorAccountID, actionID = null) {
const reportActionID = actionID || NumberUtils.rand64();
return {
actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT,
actorEmail,
person: [{type: 'TEXT', style: 'strong', text: 'User B'}],
created,
message: [{type: 'COMMENT', html: `Comment ${actionID}`, text: `Comment ${actionID}`}],
reportActionID,
actorAccountID,
};
}
export {
getGlobalFetchMock,
signInWithTestUser,
setPersonalDetails,
buildPersonalDetails,
buildTestReportComment,
};