-
-
Notifications
You must be signed in to change notification settings - Fork 223
/
toHaveBeenCalledOnceWith.test.js
82 lines (66 loc) · 2.72 KB
/
toHaveBeenCalledOnceWith.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
import * as matcher from 'src/matchers/toHaveBeenCalledOnceWith';
expect.extend(matcher);
describe('.toHaveBeenCalledOnceWith', () => {
let mock;
beforeEach(() => {
mock = jest.fn();
});
test('passes if mock was invoked exactly once with the expected value', () => {
mock('hello');
expect(mock).toHaveBeenCalledOnceWith('hello');
});
test('passes if mock was invoked exactly once with the expected values in an array', () => {
mock(['hello', 'there']);
expect(mock).toHaveBeenCalledOnceWith(['hello', 'there']);
});
test('passes if mock was invoked exactly once with the expected values', () => {
mock('hello', 'there');
expect(mock).toHaveBeenCalledOnceWith('hello', 'there');
});
test('fails if mock was never invoked indicating that it was invoked 0 times', () => {
expect(() => expect(mock).toHaveBeenCalledOnceWith('hello')).toThrowErrorMatchingSnapshot();
});
test('fails if mock was invoked more than once, indicating how many times it was invoked', () => {
// Invoke mock 17 times
new Array(17).fill(mock).forEach(e => e(Math.random()));
expect(() => expect(mock).toHaveBeenCalledOnceWith('hello')).toThrowErrorMatchingSnapshot();
});
test('fails when given value is not a jest spy or mock', () => {
const mock1 = () => {};
expect(() => expect(mock1).toHaveBeenCalledOnceWith('hello')).toThrowErrorMatchingSnapshot();
});
test('fails when given value is not the expected one', () => {
mock('not hello');
expect(() => expect(mock).toHaveBeenCalledOnceWith('hello')).toThrowErrorMatchingSnapshot();
});
});
describe('.not.toHaveBeenCalledOnceWith', () => {
let mock;
beforeEach(() => {
mock = jest.fn();
});
test('passes if mock was never invoked', () => {
expect(mock).not.toHaveBeenCalledOnceWith('hello');
});
test('passes if mock was invoked more than once', () => {
mock('hello');
mock('hello');
expect(mock).not.toHaveBeenCalledOnceWith('hello');
});
test('fails if mock was invoked exactly once with the expected value', () => {
mock('hello');
expect(() => expect(mock).not.toHaveBeenCalledOnceWith('hello')).toThrowErrorMatchingSnapshot();
});
test('passes if mock was invoked exactly once without the expected value', () => {
mock('not hello');
expect(mock).not.toHaveBeenCalledOnceWith('hello');
});
test('passes if mock was invoked exactly once without both expected values in an array', () => {
mock(['hello', 'there']);
expect(mock).not.toHaveBeenCalledOnceWith(['hello', 'not there']);
});
test('passes if mock was invoked exactly once without both expected values', () => {
mock('hello', 'there');
expect(mock).not.toHaveBeenCalledOnceWith('hello', 'not there');
});
});