-
-
Notifications
You must be signed in to change notification settings - Fork 225
/
Copy pathtoBeEmpty.test.js
97 lines (77 loc) Β· 2.53 KB
/
toBeEmpty.test.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
import * as matcher from 'src/matchers/toBeEmpty';
expect.extend(matcher);
describe('.toBeEmpty', () => {
test('passes when given empty string', () => {
expect('').toBeEmpty();
});
test('passes when given empty string object', () => {
expect(new String('')).toBeEmpty();
});
test('passes when given empty array', () => {
expect([]).toBeEmpty();
});
test('passes when given empty object', () => {
expect({}).toBeEmpty();
});
test('When empty Set is passed', () => {
expect(new Set()).toBeEmpty();
});
test('When empty Map is passed', () => {
expect(new Map([])).toBeEmpty();
});
test('When empty generator is passed', () => {
function* yieldsNothing() {}
expect(yieldsNothing()).toBeEmpty();
});
test('fails when given non-empty string', () => {
expect(() => expect('string').toBeEmpty()).toThrowErrorMatchingSnapshot();
});
});
describe('.not.toBeEmpty', () => {
test('passes when given a non-empty string', () => {
expect('string').not.toBeEmpty();
});
test('passes when given a non-empty string object', () => {
expect(new String('string')).not.toBeEmpty();
});
test('passes when given a non-empty array', () => {
expect([1, 2]).not.toBeEmpty();
});
test('passes when given a non-empty object', () => {
expect({ foo: 'bar' }).not.toBeEmpty();
});
test('When empty Set is passed', () => {
expect(new Set(['value'])).not.toBeEmpty();
});
test('When empty Map is passed', () => {
expect(new Map([['k', 'v']])).not.toBeEmpty();
});
test('When empty generator is passed', () => {
function* yieldsNothing() {
yield 'a thing';
}
expect(yieldsNothing()).not.toBeEmpty();
});
test('fails when given empty string', () => {
expect(() => expect('').not.toBeEmpty()).toThrowErrorMatchingSnapshot();
});
});
// Note - custom equality tester must be at the end of the file because once we add it, it cannot be removed
describe('toBeEmpty with custom equality tester', () => {
let mockEqualityTester;
beforeAll(() => {
mockEqualityTester = jest.fn();
expect.addEqualityTesters([mockEqualityTester]);
});
afterEach(() => {
mockEqualityTester.mockReset();
});
test('passes when custom equality matches empty object', () => {
mockEqualityTester.mockReturnValueOnce(true);
expect('a').toBeEmpty();
});
test('fails when custom equality does not match empty object', () => {
mockEqualityTester.mockReturnValueOnce(false);
expect(() => expect({}).toBeEmpty()).toThrowErrorMatchingSnapshot();
});
});