-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathtest.js
119 lines (95 loc) · 3.52 KB
/
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
'use strict';
// @flow
const cases = require('./');
function add(augend, addend) {
return augend + addend;
}
function subtract(minuend, subtrahend) {
return minuend - subtrahend;
}
beforeEach(() => {
jest.spyOn(global, 'describe').mockImplementation((title, fn) => fn());
jest.spyOn(global, 'test').mockImplementation((name, fn) => fn());
global.test.skip = jest.fn((name, fn) => fn());
global.test.only = jest.fn((name, fn) => fn());
});
afterEach(() => {
global.describe.mockRestore();
global.test.mockRestore();
});
test('array', () => {
let title = 'add(augend, addend)';
let tester = jest.fn(opts => {
expect(add(opts.augend, opts.addend)).toBe(opts.total);
});
let testCases = [
{ name: '1 + 1 = 2', augend: 1, addend: 1, total: 2 },
{ name: '2 + 1 = 3', augend: 2, addend: 1, total: 3 },
{ name: '3 + 1 = 4', augend: 3, addend: 1, total: 4 },
];
cases(title, tester, testCases);
expect(global.describe).toHaveBeenCalledTimes(1);
expect(global.test).toHaveBeenCalledTimes(3);
expect(tester).toHaveBeenCalledTimes(3);
expect(global.describe.mock.calls[0][0]).toBe(title);
expect(global.test.mock.calls[0][0]).toBe(testCases[0].name);
expect(global.test.mock.calls[1][0]).toBe(testCases[1].name);
expect(global.test.mock.calls[2][0]).toBe(testCases[2].name);
expect(global.test.mock.calls[0][1]).toHaveLength(0);
expect(global.test.mock.calls[1][1]).toHaveLength(0);
expect(global.test.mock.calls[2][1]).toHaveLength(0);
expect(tester).toHaveBeenCalledWith(testCases[0]);
expect(tester).toHaveBeenCalledWith(testCases[1]);
expect(tester).toHaveBeenCalledWith(testCases[2]);
});
test('object', () => {
jest.spyOn(global, 'describe').mockImplementation((title, fn) => fn());
jest.spyOn(global, 'test').mockImplementation((name, fn) => fn());
let title = 'add(augend, addend)';
let tester = jest.fn(opts => {
expect(subtract(opts.minuend, opts.subtrahend)).toBe(opts.difference);
});
let testCases = {
'1 - 1 = 0': { minuend: 1, subtrahend: 1, difference: 0 },
'2 - 1 = 1': { minuend: 2, subtrahend: 1, difference: 1 },
'3 - 1 = 2': { minuend: 3, subtrahend: 1, difference: 2 },
};
cases(title, tester, testCases);
expect(global.describe).toHaveBeenCalledTimes(1);
expect(global.test).toHaveBeenCalledTimes(3);
expect(tester).toHaveBeenCalledTimes(3);
expect(global.describe.mock.calls[0][0]).toBe(title);
expect(global.test.mock.calls[0][0]).toBe('1 - 1 = 0');
expect(global.test.mock.calls[1][0]).toBe('2 - 1 = 1');
expect(global.test.mock.calls[2][0]).toBe('3 - 1 = 2');
expect(global.test.mock.calls[0][1]).toHaveLength(0);
expect(global.test.mock.calls[1][1]).toHaveLength(0);
expect(global.test.mock.calls[2][1]).toHaveLength(0);
expect(tester.mock.calls[0][0]).toMatchObject(testCases['1 - 1 = 0']);
expect(tester.mock.calls[1][0]).toMatchObject(testCases['2 - 1 = 1']);
expect(tester.mock.calls[2][0]).toMatchObject(testCases['3 - 1 = 2']);
});
test('no names', () => {
cases('foo', () => {}, [
{},
{},
]);
expect(global.test.mock.calls[0][0]).toBe('case: 1');
expect(global.test.mock.calls[1][0]).toBe('case: 2');
});
test('only', () => {
cases('foo', () => {}, [
{},
{ only: true },
]);
expect(global.test.mock.calls[0][0]).toBe('case: 1');
expect(global.test.only.mock.calls[0][0]).toBe('case: 2');
});
test('skip', () => {
cases('foo', () => {}, [
{},
{ skip: true },
]);
expect(global.test.mock.calls[0][0]).toBe('case: 1');
expect(global.test.skip.mock.calls[0][0]).toBe('case: 2');
});