/
jest-when-tests.ts
182 lines (157 loc) · 4.61 KB
/
jest-when-tests.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
import { when, resetAllWhenMocks, verifyAllWhenMocksCalled } from 'jest-when';
describe('mock-when test', () => {
it('basic usage:', () => {
const fn = jest.fn();
when(fn)
.calledWith(1)
.mockReturnValue('yay!');
const result = fn(1);
expect(result).toEqual('yay!');
});
it('Supports multiple args:', () => {
const fn = jest.fn();
when(fn)
.calledWith(1, true, 'foo')
.mockReturnValue('yay!');
const result = fn(1, true, 'foo');
expect(result).toEqual('yay!');
});
it('Supports jest matchers:', () => {
const fn = jest.fn();
when(fn)
.calledWith(
expect.anything(),
expect.any(Number),
expect.arrayContaining([false]),
)
.mockReturnValue('yay!');
const result = fn('whatever', 100, [true, false]);
expect(result).toEqual('yay!');
});
it('Supports compound declarations:', () => {
const fn = jest.fn();
when(fn)
.calledWith(1)
.mockReturnValueOnce('no')
.mockReturnValue('yes');
when(fn)
.calledWith(2)
.mockReturnValue('way?');
when(fn)
.calledWith(3)
.mockResolvedValueOnce('no');
when(fn)
.calledWith(3)
.mockResolvedValue('yes');
when(fn)
.calledWith(4)
.mockRejectedValueOnce('no');
when(fn)
.calledWith(4)
.mockRejectedValue('yes');
expect(fn(1)).toEqual('no');
expect(fn(1)).toEqual('yes');
expect(fn(2)).toEqual('way?');
expect(fn(3)).resolves.toEqual('no');
expect(fn(3)).resolves.toEqual('yes');
expect(fn(4)).rejects.toEqual('no');
expect(fn(4)).rejects.toEqual('yes');
});
it('Supports chained calls:', () => {
const fn = jest.fn();
when(fn)
.calledWith(1)
.mockReturnValue('no')
.calledWith(2)
.mockReturnValue('way?')
.calledWith(3)
.mockReturnValue('yes')
.calledWith(4)
.mockReturnValue('way!');
expect(fn(1)).toEqual('no');
expect(fn(2)).toEqual('way?');
expect(fn(3)).toEqual('yes');
expect(fn(4)).toEqual('way!');
expect(fn(5)).toEqual(undefined);
});
it('Supports chained calls with defaults:', () => {
const fn = jest.fn();
when(fn)
.mockReturnValue('nice')
.calledWith(1)
.mockReturnValue('no')
.calledWith(2)
.mockReturnValue('way?')
.calledWith(3)
.mockReturnValue('yes')
.calledWith(4)
.mockReturnValue('way!');
expect(fn(1)).toEqual('no');
expect(fn(2)).toEqual('way?');
expect(fn(3)).toEqual('yes');
expect(fn(4)).toEqual('way!');
expect(fn(5)).toEqual('nice');
});
it('Assert the args:', () => {
const fn = jest.fn();
when(fn)
.expectCalledWith(1)
.mockReturnValue('x');
let testPassed = false;
try {
fn(2); // Will throw a helpful jest assertion error with args diff
} catch (e) {
testPassed = true;
}
expect(testPassed).toBeTruthy();
});
it('should support for mockImplementation', () => {
const fn = jest.fn();
const expectValue = { a: 1, b: 2 };
when(fn)
.calledWith(expect.anything())
.mockImplementation(() => expectValue);
const result = fn('whatever');
expect(result).toMatchObject(expectValue);
});
it('should support for mockImplementationOnce', () => {
const fn = jest.fn();
const expectValue = { a: 1, b: 2 };
when(fn)
.calledWith(expect.anything())
.mockImplementationOnce(() => expectValue);
const result = fn('whatever');
const result2 = fn('whatever');
expect(result).toMatchObject(expectValue);
expect(result2).toEqual(undefined);
});
it('should support resetAllWhenMocks', () => {
const fn = jest.fn(_ => 'initial');
when(fn)
.expectCalledWith(1)
.mockReturnValueOnce('x');
resetAllWhenMocks();
expect(fn(1)).toEqual('initial');
});
it('should supper verifyAllWhenMocksCalled', () => {
const fn1 = jest.fn();
const fn2 = jest.fn();
when(fn1)
.expectCalledWith(1)
.mockReturnValue('z');
when(fn2)
.expectCalledWith(1)
.mockReturnValueOnce('x');
when(fn2)
.expectCalledWith(1)
.mockReturnValueOnce('y');
when(fn2)
.expectCalledWith(1)
.mockReturnValue('z');
fn1(1);
fn2(1);
fn2(1);
fn2(1);
expect(verifyAllWhenMocksCalled).not.toThrow();
});
});