-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathTestHelper.js
216 lines (204 loc) · 6.92 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import _ from 'underscore';
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 NumberUtils from '../../src/libs/NumberUtils';
/**
* @param {String} login
* @param {Number} accountID
* @param {String} [firstName]
* @returns {Object}
*/
function buildPersonalDetails(login, accountID, firstName = 'Test') {
return {
accountID,
login,
avatar: 'https://d2k5nsl2zxldvw.cloudfront.net/images/avatars/avatar_7.png',
avatarThumbnail: 'https://d2k5nsl2zxldvw.cloudfront.net/images/avatars/avatar_7.png',
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.mockResolvedValue({
onyxData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.CREDENTIALS,
value: {
login,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
validated: true,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
value: {
[accountID]: 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 BeginSignIn
HttpUtils.xhr.mockResolvedValue({
onyxData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.SESSION,
value: {
authToken,
accountID,
email: login,
encryptedAuthToken: authToken,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.CREDENTIALS,
value: {
autoGeneratedLogin: Str.guid('expensify.cash-'),
autoGeneratedPassword: Str.guid(),
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.USER,
value: {
isUsingExpensifyCard: false,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.BETAS,
value: ['all'],
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.NVP_PRIVATE_PUSH_NOTIFICATION_ID,
value: 'randomID',
},
],
jsonCode: 200,
});
Session.signIn(password);
return waitForPromisesToResolve();
})
.then(() => {
HttpUtils.xhr = originalXhr;
});
}
function signOutTestUser() {
const originalXhr = HttpUtils.xhr;
HttpUtils.xhr = jest.fn();
HttpUtils.xhr.mockResolvedValue({jsonCode: 200});
Session.signOutAndRedirectToSignIn();
return waitForPromisesToResolve().then(() => (HttpUtils.xhr = originalXhr));
}
/**
* Use for situations where fetch() is required. This mock is stateful and has some additional methods to control its behavior:
*
* - pause() – stop resolving promises until you call resume()
* - resume() - flush the queue of promises, and start resolving new promises immediately
* - fail() - start returning a failure response
* - success() - go back to returning a success response
*
* @example
*
* beforeAll(() => {
* global.fetch = TestHelper.getGlobalFetchMock();
* });
*
* @returns {Function}
*/
function getGlobalFetchMock() {
const queue = [];
let isPaused = false;
let shouldFail = false;
const getResponse = () =>
shouldFail
? {
ok: true,
json: () => Promise.resolve({jsonCode: 400}),
}
: {
ok: true,
json: () => Promise.resolve({jsonCode: 200}),
};
const mockFetch = jest.fn().mockImplementation(() => {
if (!isPaused) {
return Promise.resolve(getResponse());
}
return new Promise((resolve) => queue.push(resolve));
});
mockFetch.pause = () => (isPaused = true);
mockFetch.resume = () => {
isPaused = false;
_.each(queue, (resolve) => resolve(getResponse()));
return waitForPromisesToResolve();
};
mockFetch.fail = () => (shouldFail = true);
mockFetch.succeed = () => (shouldFail = false);
return mockFetch;
}
/**
* @param {String} login
* @param {Number} accountID
* @returns {Promise}
*/
function setPersonalDetails(login, accountID) {
Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, {
[accountID]: buildPersonalDetails(login, accountID),
});
return waitForPromisesToResolve();
}
/**
* @param {String} created
* @param {Number} actorAccountID
* @param {String} actionID
* @returns {Object}
*/
function buildTestReportComment(created, actorAccountID, actionID = null) {
const reportActionID = actionID || NumberUtils.rand64();
return {
actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT,
person: [{type: 'TEXT', style: 'strong', text: 'User B'}],
created,
message: [{type: 'COMMENT', html: `Comment ${actionID}`, text: `Comment ${actionID}`}],
reportActionID,
actorAccountID,
};
}
export {getGlobalFetchMock, signInWithTestUser, signOutTestUser, setPersonalDetails, buildPersonalDetails, buildTestReportComment};