UNPKG

17.9 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const Matchers_1 = require("./Matchers");
4class Cls {
5}
6describe('Matchers', () => {
7 describe('any', () => {
8 test('returns true for false', () => {
9 expect((0, Matchers_1.any)().asymmetricMatch(false)).toBe(true);
10 });
11 test('returns true for undefined', () => {
12 expect((0, Matchers_1.any)().asymmetricMatch(undefined)).toBe(true);
13 });
14 test('returns true for null', () => {
15 expect((0, Matchers_1.any)().asymmetricMatch(null)).toBe(true);
16 });
17 test('Supports undefined in chain', () => {
18 const f = jest.fn();
19 f(undefined);
20 expect(f).toHaveBeenCalledWith((0, Matchers_1.any)());
21 });
22 });
23 describe('anyString', () => {
24 test('returns true for empty string', () => {
25 expect((0, Matchers_1.anyString)().asymmetricMatch('')).toBe(true);
26 });
27 test('returns true for non-empty string', () => {
28 expect((0, Matchers_1.anyString)().asymmetricMatch('123')).toBe(true);
29 });
30 test('returns false for number', () => {
31 // @ts-ignore
32 expect((0, Matchers_1.anyString)().asymmetricMatch(123)).toBe(false);
33 });
34 test('returns false for null', () => {
35 // @ts-ignore
36 expect((0, Matchers_1.anyString)().asymmetricMatch(null)).toBe(false);
37 });
38 test('returns false for undefined', () => {
39 // @ts-ignore
40 expect((0, Matchers_1.anyString)().asymmetricMatch(undefined)).toBe(false);
41 });
42 });
43 describe('anyNumber', () => {
44 test('returns true for 0', () => {
45 expect((0, Matchers_1.anyNumber)().asymmetricMatch(0)).toBe(true);
46 });
47 test('returns true for normal number', () => {
48 expect((0, Matchers_1.anyNumber)().asymmetricMatch(123)).toBe(true);
49 });
50 test('returns false for string', () => {
51 // @ts-ignore
52 expect((0, Matchers_1.anyNumber)().asymmetricMatch('123')).toBe(false);
53 });
54 test('returns false for null', () => {
55 // @ts-ignore
56 expect((0, Matchers_1.anyNumber)().asymmetricMatch(null)).toBe(false);
57 });
58 test('returns false for undefined', () => {
59 // @ts-ignore
60 expect((0, Matchers_1.anyNumber)().asymmetricMatch(undefined)).toBe(false);
61 });
62 test('returns false for NaN', () => {
63 expect((0, Matchers_1.anyNumber)().asymmetricMatch(NaN)).toBe(false);
64 });
65 });
66 describe('anyBoolean', () => {
67 test('returns true for true', () => {
68 expect((0, Matchers_1.anyBoolean)().asymmetricMatch(true)).toBe(true);
69 });
70 test('returns true for false', () => {
71 expect((0, Matchers_1.anyBoolean)().asymmetricMatch(false)).toBe(true);
72 });
73 test('returns false for string', () => {
74 // @ts-ignore
75 expect((0, Matchers_1.anyBoolean)().asymmetricMatch('true')).toBe(false);
76 });
77 test('returns false for null', () => {
78 // @ts-ignore
79 expect((0, Matchers_1.anyBoolean)().asymmetricMatch(null)).toBe(false);
80 });
81 test('returns false for undefined', () => {
82 // @ts-ignore
83 expect((0, Matchers_1.anyBoolean)().asymmetricMatch(undefined)).toBe(false);
84 });
85 });
86 describe('anyFunction', () => {
87 test('returns true for function', () => {
88 expect((0, Matchers_1.anyFunction)().asymmetricMatch(() => { })).toBe(true);
89 });
90 test('returns false for string', () => {
91 // @ts-ignore
92 expect((0, Matchers_1.anyFunction)().asymmetricMatch('true')).toBe(false);
93 });
94 test('returns false for null', () => {
95 // @ts-ignore
96 expect((0, Matchers_1.anyFunction)().asymmetricMatch(null)).toBe(false);
97 });
98 test('returns false for undefined', () => {
99 // @ts-ignore
100 expect((0, Matchers_1.anyFunction)().asymmetricMatch(undefined)).toBe(false);
101 });
102 });
103 describe('anySymbol', () => {
104 test('returns true for symbol', () => {
105 expect((0, Matchers_1.anySymbol)().asymmetricMatch(Symbol('123'))).toBe(true);
106 });
107 test('returns false for string', () => {
108 // @ts-ignore
109 expect((0, Matchers_1.anySymbol)().asymmetricMatch('123')).toBe(false);
110 });
111 test('returns false for null', () => {
112 // @ts-ignore
113 expect((0, Matchers_1.anySymbol)().asymmetricMatch(null)).toBe(false);
114 });
115 test('returns false for undefined', () => {
116 // @ts-ignore
117 expect((0, Matchers_1.anySymbol)().asymmetricMatch(undefined)).toBe(false);
118 });
119 });
120 describe('anyObject', () => {
121 test('returns true for object', () => {
122 expect((0, Matchers_1.anyObject)().asymmetricMatch({})).toBe(true);
123 });
124 test('returns true for new object', () => {
125 expect((0, Matchers_1.anyObject)().asymmetricMatch(new Object())).toBe(true);
126 });
127 test('returns true for new instance', () => {
128 expect((0, Matchers_1.anyObject)().asymmetricMatch(new Cls())).toBe(true);
129 });
130 test('returns true for new builtin', () => {
131 expect((0, Matchers_1.anyObject)().asymmetricMatch(new Map())).toBe(true);
132 });
133 test('returns false for string', () => {
134 expect((0, Matchers_1.anyObject)().asymmetricMatch('123')).toBe(false);
135 });
136 test('returns false for number', () => {
137 expect((0, Matchers_1.anyObject)().asymmetricMatch(123)).toBe(false);
138 });
139 test('returns false for null', () => {
140 expect((0, Matchers_1.anyObject)().asymmetricMatch(null)).toBe(false);
141 });
142 test('returns false for undefined', () => {
143 expect((0, Matchers_1.anyObject)().asymmetricMatch(undefined)).toBe(false);
144 });
145 });
146 describe('anyArray', () => {
147 test('returns true for empty array', () => {
148 expect((0, Matchers_1.anyArray)().asymmetricMatch([])).toBe(true);
149 });
150 test('returns true for non empty', () => {
151 expect((0, Matchers_1.anyArray)().asymmetricMatch([1, 2, 3])).toBe(true);
152 });
153 test('returns false for object', () => {
154 // @ts-ignore
155 expect((0, Matchers_1.anyArray)().asymmetricMatch({})).toBe(false);
156 });
157 test('returns false for null', () => {
158 // @ts-ignored
159 expect((0, Matchers_1.anyArray)().asymmetricMatch(null)).toBe(false);
160 });
161 test('returns false for undefined', () => {
162 // @ts-ignore
163 expect((0, Matchers_1.anyArray)().asymmetricMatch(undefined)).toBe(false);
164 });
165 });
166 describe('anyMap', () => {
167 test('returns true for empty Map', () => {
168 expect((0, Matchers_1.anyMap)().asymmetricMatch(new Map())).toBe(true);
169 });
170 test('returns true for non empty', () => {
171 const map = new Map();
172 map.set(1, 2);
173 expect((0, Matchers_1.anyMap)().asymmetricMatch(map)).toBe(true);
174 });
175 test('returns false for object', () => {
176 // @ts-ignore
177 expect((0, Matchers_1.anyMap)().asymmetricMatch({})).toBe(false);
178 });
179 test('returns false for null', () => {
180 // @ts-ignore
181 expect((0, Matchers_1.anyMap)().asymmetricMatch(null)).toBe(false);
182 });
183 test('returns false for undefined', () => {
184 // @ts-ignore
185 expect((0, Matchers_1.anyMap)().asymmetricMatch(undefined)).toBe(false);
186 });
187 });
188 describe('anySet', () => {
189 test('returns true for empty Set', () => {
190 expect((0, Matchers_1.anySet)().asymmetricMatch(new Set())).toBe(true);
191 });
192 test('returns true for non empty', () => {
193 const set = new Set();
194 set.add(2);
195 expect((0, Matchers_1.anySet)().asymmetricMatch(set)).toBe(true);
196 });
197 test('returns false for object', () => {
198 // @ts-ignore
199 expect((0, Matchers_1.anySet)().asymmetricMatch({})).toBe(false);
200 });
201 test('returns false for null', () => {
202 // @ts-ignore
203 expect((0, Matchers_1.anySet)().asymmetricMatch(null)).toBe(false);
204 });
205 test('returns false for undefined', () => {
206 // @ts-ignore
207 expect((0, Matchers_1.anySet)().asymmetricMatch(undefined)).toBe(false);
208 });
209 });
210 describe('isA', () => {
211 test('returns true when class is the same builtin', () => {
212 expect((0, Matchers_1.isA)(Map).asymmetricMatch(new Map())).toBe(true);
213 });
214 test('returns true for non empty', () => {
215 expect((0, Matchers_1.isA)(Cls).asymmetricMatch(new Cls())).toBe(true);
216 });
217 test('returns false for object', () => {
218 expect((0, Matchers_1.isA)(Cls).asymmetricMatch({})).toBe(false);
219 });
220 test('returns false for null', () => {
221 expect((0, Matchers_1.isA)(Cls).asymmetricMatch(null)).toBe(false);
222 });
223 test('returns false for undefined', () => {
224 expect((0, Matchers_1.isA)(Cls).asymmetricMatch(undefined)).toBe(false);
225 });
226 });
227 describe('arrayIncludes', () => {
228 test('returns true when array contains value', () => {
229 expect((0, Matchers_1.arrayIncludes)('val').asymmetricMatch(['val', 'val2'])).toBe(true);
230 });
231 test('returns false when array does not contain value', () => {
232 expect((0, Matchers_1.arrayIncludes)('val3').asymmetricMatch(['val', 'val2'])).toBe(false);
233 });
234 test('returns false when not a map', () => {
235 // @ts-ignore
236 expect((0, Matchers_1.arrayIncludes)('val3').asymmetricMatch({})).toBe(false);
237 });
238 test('returns false when for null', () => {
239 // @ts-ignore
240 expect((0, Matchers_1.arrayIncludes)('val3').asymmetricMatch(null)).toBe(false);
241 });
242 test('returns false when for undefined', () => {
243 // @ts-ignore
244 expect((0, Matchers_1.arrayIncludes)('val3').asymmetricMatch(undefined)).toBe(false);
245 });
246 });
247 describe('mapHas', () => {
248 test('returns true when map contains key', () => {
249 expect((0, Matchers_1.mapHas)('key').asymmetricMatch(new Map([['key', 'val']]))).toBe(true);
250 });
251 test('returns false when map does not contain key', () => {
252 expect((0, Matchers_1.mapHas)('key3').asymmetricMatch(new Map([['key', 'val']]))).toBe(false);
253 });
254 test('returns false when not a map', () => {
255 // @ts-ignore
256 expect((0, Matchers_1.mapHas)('val3').asymmetricMatch({})).toBe(false);
257 });
258 test('returns false when for null', () => {
259 // @ts-ignore
260 expect((0, Matchers_1.mapHas)('val3').asymmetricMatch(null)).toBe(false);
261 });
262 test('returns false when for undefined', () => {
263 // @ts-ignore
264 expect((0, Matchers_1.mapHas)('val3').asymmetricMatch(undefined)).toBe(false);
265 });
266 });
267 describe('setHas', () => {
268 test('returns true when set contains value', () => {
269 expect((0, Matchers_1.setHas)('val').asymmetricMatch(new Set(['val']))).toBe(true);
270 });
271 test('returns false when set does not contain value', () => {
272 expect((0, Matchers_1.setHas)('val3').asymmetricMatch(new Set(['val', 'val2']))).toBe(false);
273 });
274 test('returns false when not a set', () => {
275 // @ts-ignore
276 expect((0, Matchers_1.setHas)('val3').asymmetricMatch({})).toBe(false);
277 });
278 test('returns false when for null', () => {
279 // @ts-ignore
280 expect((0, Matchers_1.setHas)('val3').asymmetricMatch(null)).toBe(false);
281 });
282 test('returns false when for undefined', () => {
283 // @ts-ignore
284 expect((0, Matchers_1.setHas)('val3').asymmetricMatch(undefined)).toBe(false);
285 });
286 });
287 describe('objectContainsKey', () => {
288 test('returns true when object contains key', () => {
289 expect((0, Matchers_1.objectContainsKey)('key').asymmetricMatch({ key: 'val' })).toBe(true);
290 });
291 test('returns false when object does not contain key', () => {
292 expect((0, Matchers_1.objectContainsKey)('key3').asymmetricMatch({ key: 'val' })).toBe(false);
293 });
294 test('returns false when not a object', () => {
295 expect((0, Matchers_1.objectContainsKey)('val3').asymmetricMatch(213)).toBe(false);
296 });
297 test('returns false when for null', () => {
298 expect((0, Matchers_1.objectContainsKey)('val3').asymmetricMatch(null)).toBe(false);
299 });
300 test('returns false when for undefined', () => {
301 expect((0, Matchers_1.objectContainsKey)('val3').asymmetricMatch(undefined)).toBe(false);
302 });
303 });
304 describe('objectContainsValue', () => {
305 test('returns true when object contains value', () => {
306 expect((0, Matchers_1.objectContainsValue)('val').asymmetricMatch({ key: 'val' })).toBe(true);
307 });
308 test('returns false when object does not contain value', () => {
309 expect((0, Matchers_1.objectContainsValue)('val3').asymmetricMatch({ key: 'val' })).toBe(false);
310 });
311 test('returns false when not a object', () => {
312 expect((0, Matchers_1.objectContainsValue)('val3').asymmetricMatch(213)).toBe(false);
313 });
314 test('returns false when for null', () => {
315 expect((0, Matchers_1.objectContainsValue)('val3').asymmetricMatch(null)).toBe(false);
316 });
317 test('returns false when for undefined', () => {
318 expect((0, Matchers_1.objectContainsValue)('val3').asymmetricMatch(undefined)).toBe(false);
319 });
320 });
321 describe('notNull', () => {
322 test('returns true when object', () => {
323 expect((0, Matchers_1.notNull)().asymmetricMatch({ key: 'val' })).toBe(true);
324 });
325 test('returns true when undefined', () => {
326 expect((0, Matchers_1.notNull)().asymmetricMatch(undefined)).toBe(true);
327 });
328 test('returns true when empty string', () => {
329 expect((0, Matchers_1.notNull)().asymmetricMatch('')).toBe(true);
330 });
331 test('returns false when for null', () => {
332 expect((0, Matchers_1.notNull)().asymmetricMatch(null)).toBe(false);
333 });
334 });
335 describe('notUndefined', () => {
336 test('returns true when object', () => {
337 expect((0, Matchers_1.notUndefined)().asymmetricMatch({ key: 'val' })).toBe(true);
338 });
339 test('returns true when null', () => {
340 expect((0, Matchers_1.notUndefined)().asymmetricMatch(null)).toBe(true);
341 });
342 test('returns true when empty string', () => {
343 expect((0, Matchers_1.notUndefined)().asymmetricMatch('')).toBe(true);
344 });
345 test('returns false when for undefined', () => {
346 expect((0, Matchers_1.notUndefined)().asymmetricMatch(undefined)).toBe(false);
347 });
348 });
349 describe('notEmpty', () => {
350 test('returns true when object', () => {
351 expect((0, Matchers_1.notEmpty)().asymmetricMatch({ key: 'val' })).toBe(true);
352 });
353 test('returns true when null', () => {
354 expect((0, Matchers_1.notEmpty)().asymmetricMatch(null)).toBe(false);
355 });
356 test('returns true when empty string', () => {
357 expect((0, Matchers_1.notEmpty)().asymmetricMatch('')).toBe(false);
358 });
359 test('returns false when for undefined', () => {
360 expect((0, Matchers_1.notEmpty)().asymmetricMatch(undefined)).toBe(false);
361 });
362 });
363 describe('captor', () => {
364 let fn;
365 let doSomething;
366 beforeEach(() => {
367 fn = jest.fn();
368 doSomething = (fn, count) => {
369 fn(String(count), count, { 1: 2 });
370 };
371 });
372 test('can capture arg with other matchers', () => {
373 doSomething(fn, 1);
374 const argCaptor = (0, Matchers_1.captor)();
375 expect(fn).toHaveBeenCalledWith(argCaptor, (0, Matchers_1.any)(), (0, Matchers_1.any)());
376 expect(argCaptor.value).toBe('1');
377 });
378 test('stores all values', () => {
379 doSomething(fn, 1);
380 doSomething(fn, 2);
381 doSomething(fn, 3);
382 const argCaptor = (0, Matchers_1.captor)();
383 expect(fn).toHaveBeenNthCalledWith(1, argCaptor, (0, Matchers_1.any)(), (0, Matchers_1.any)());
384 expect(fn).toHaveBeenNthCalledWith(2, argCaptor, (0, Matchers_1.any)(), (0, Matchers_1.any)());
385 expect(fn).toHaveBeenNthCalledWith(3, argCaptor, (0, Matchers_1.any)(), (0, Matchers_1.any)());
386 expect(argCaptor.value).toBe('3');
387 expect(argCaptor.values).toEqual(['1', '2', '3']);
388 });
389 });
390 describe('matches function', () => {
391 test('expects passes for when it returns true', () => {
392 const fn = jest.fn();
393 fn(1);
394 expect(fn).toHaveBeenCalledWith((0, Matchers_1.matches)((val) => val === 1));
395 });
396 test('expects with not passes for when it returns false', () => {
397 const fn = jest.fn();
398 fn(1);
399 expect(fn).not.toHaveBeenCalledWith((0, Matchers_1.matches)((val) => val === 2));
400 });
401 });
402});