-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
runAsyncTest.test.ts
226 lines (201 loc) · 6.56 KB
/
runAsyncTest.test.ts
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
217
218
219
220
221
222
223
224
225
226
import _ from 'lodash';
import expandStateRef from '../../../../testUtils/expandStateRef';
import itWithContext from '../../../../testUtils/itWithContext';
import runCreateRef from '../../../../testUtils/runCreateRef';
import VestTest from 'VestTest';
import context from 'ctx';
import { setPending } from 'pending';
import runAsyncTest from 'runAsyncTest';
import { usePending, useTestCallbacks } from 'stateHooks';
const message = 'some message string';
const CASE_PASSING = 'passing';
const CASE_FAILING = 'failing';
let stateRef;
const getCtx = () => ({ stateRef });
describe.each([CASE_PASSING, CASE_FAILING])('runAsyncTest: %s', testCase => {
let testObject, fieldName;
const runRunAsyncTest = (testObject: VestTest) =>
context.run(
{
stateRef,
},
() => runAsyncTest(testObject)
);
beforeEach(() => {
fieldName = 'field_1';
stateRef = runCreateRef();
context.run({ stateRef }, () => {
const [, setTestCallbacks] = useTestCallbacks();
setTestCallbacks(state => {
state.fieldCallbacks = {
...state.fieldCallbacks,
[fieldName]: state.fieldCallbacks[fieldName] || [],
};
return state;
});
});
testObject = new VestTest(fieldName, jest.fn(), {
message,
});
testObject.asyncTest =
testCase === CASE_PASSING ? Promise.resolve() : Promise.reject();
context.run({ stateRef }, () => {
setPending(testObject);
});
});
describe('State updates', () => {
itWithContext(
'Initial state matches snapshot (sanity)',
() => {
const [pending] = usePending();
expect(pending).toContain(testObject);
expect(expandStateRef(stateRef)).toMatchSnapshot();
runRunAsyncTest(testObject);
},
getCtx
);
it('Should remove test from pending array', () =>
new Promise<void>(done => {
runRunAsyncTest(testObject);
setTimeout(
context.bind({ stateRef }, () => {
const [pending] = usePending();
expect(pending).not.toContain(testObject);
done();
})
);
}));
});
describe('doneCallbacks', () => {
let fieldCallback_1, fieldCallback_2, doneCallback;
beforeEach(() => {
fieldCallback_1 = jest.fn();
fieldCallback_2 = jest.fn();
doneCallback = jest.fn();
context.run({ stateRef }, () => {
const [, setTestCallbacks] = useTestCallbacks();
setTestCallbacks(state => ({
fieldCallbacks: {
...state.fieldCallbacks,
[fieldName]: (state.fieldCallbacks[fieldName] || []).concat(
fieldCallback_1,
fieldCallback_2
),
},
doneCallbacks: state.doneCallbacks.concat(doneCallback),
}));
});
});
describe('When no remaining tests', () => {
it('Should run all callbacks', () =>
new Promise<void>(done => {
expect(fieldCallback_1).not.toHaveBeenCalled();
expect(fieldCallback_2).not.toHaveBeenCalled();
expect(doneCallback).not.toHaveBeenCalled();
runRunAsyncTest(testObject);
setTimeout(() => {
expect(fieldCallback_1).toHaveBeenCalled();
expect(fieldCallback_2).toHaveBeenCalled();
expect(doneCallback).toHaveBeenCalled();
done();
});
}));
});
describe('When there are more tests left', () => {
beforeEach(() => {
context.run({ stateRef }, () => {
setPending(
new VestTest('pending_field', jest.fn(), {
message,
})
);
});
});
it("Should only run current field's callbacks", () =>
new Promise<void>(done => {
expect(fieldCallback_1).not.toHaveBeenCalled();
expect(fieldCallback_2).not.toHaveBeenCalled();
expect(doneCallback).not.toHaveBeenCalled();
runRunAsyncTest(testObject);
setTimeout(() => {
expect(fieldCallback_1).toHaveBeenCalled();
expect(fieldCallback_2).toHaveBeenCalled();
expect(doneCallback).not.toHaveBeenCalled();
done();
});
}));
});
describe('When test is canceled', () => {
beforeEach(() => {
context.run({ stateRef }, () => {
testObject.cancel();
});
});
it('Should return without running any callback', () =>
new Promise<void>(done => {
expect(fieldCallback_1).not.toHaveBeenCalled();
expect(fieldCallback_2).not.toHaveBeenCalled();
expect(doneCallback).not.toHaveBeenCalled();
runRunAsyncTest(testObject);
setTimeout(() => {
expect(fieldCallback_1).not.toHaveBeenCalled();
expect(fieldCallback_2).not.toHaveBeenCalled();
expect(doneCallback).not.toHaveBeenCalled();
done();
});
}));
});
});
describe('testObject', () => {
let testObjectCopy;
beforeEach(() => {
testObject.fail = jest.fn();
testObjectCopy = _.cloneDeep(testObject);
});
if (testCase === CASE_PASSING) {
it('Should keep test object unchanged except for status', () =>
new Promise<void>(done => {
runRunAsyncTest(testObject);
setTimeout(() => {
expect(testObject).toEqual(
Object.assign(testObjectCopy, { status: 'PASSING' })
);
done();
});
}));
it('Should return without calling testObject.fail', () =>
new Promise<void>(done => {
runRunAsyncTest(testObject);
setTimeout(() => {
expect(testObject.fail).not.toHaveBeenCalled();
done();
});
}));
}
if (testCase === CASE_FAILING) {
it('Should call testObject.fail', () =>
new Promise<void>(done => {
runRunAsyncTest(testObject);
setTimeout(() => {
expect(testObject.fail).toHaveBeenCalled();
done();
});
}));
describe('When rejecting with a message', () => {
const rejectionString = 'rejection string';
beforeEach(() => {
testObject.asyncTest.catch(Function.prototype);
testObject.asyncTest = Promise.reject(rejectionString);
});
it('Should set test message to rejection string', () =>
new Promise<void>(done => {
runRunAsyncTest(testObject);
setTimeout(() => {
expect(testObject.message).toBe(rejectionString);
done();
});
}));
});
}
});
});