forked from jestjs/jest
/
mockSerializer.test.ts
174 lines (160 loc) Β· 4.07 KB
/
mockSerializer.test.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
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import prettyFormat from 'pretty-format';
import plugin from '../mockSerializer';
test('mock with 0 calls and default name', () => {
const fn = jest.fn();
expect(fn).toMatchSnapshot();
});
test('mock with 2 calls, 1 return, 1 throw', () => {
const fn = jest.fn(value => {
if (value % 2 === 0) {
return value * 2;
} else {
throw new Error('Error Message!');
}
});
fn(2);
try {
fn(3);
} catch {
// ignore error
}
expect(fn).toMatchSnapshot();
});
test('mock with 0 calls and default name in React element', () => {
const fn = jest.fn();
const val = {
$$typeof: Symbol.for('react.test.json'),
children: ['Mock me!'],
props: {
onClick: fn,
},
type: 'button',
};
expect(val).toMatchSnapshot();
});
test('mock with 0 calls and non-default name', () => {
const fn = jest.fn();
fn.mockName('MyConstructor');
expect(fn).toMatchSnapshot();
});
test('mock with 1 calls and non-default name via new in object', () => {
const fn = jest.fn();
fn.mockName('MyConstructor');
// eslint-disable-next-line no-new
new fn({name: 'some fine name'});
const val = {
fn,
};
expect(val).toMatchSnapshot();
});
test('mock with 1 calls in React element', () => {
const fn = jest.fn();
fn('Mocking you!');
const val = {
$$typeof: Symbol.for('react.test.json'),
children: ['Mock me!'],
props: {
onClick: fn,
},
type: 'button',
};
expect(val).toMatchSnapshot();
});
test('mock with 2 calls', () => {
const fn = jest.fn();
fn();
fn({foo: 'bar'}, 42);
expect(fn).toMatchSnapshot();
});
test('indent option', () => {
const fn = jest.fn(val => val);
fn({key: 'value'});
const expected = [
'[MockFunction] {',
'"calls": Array [',
'Array [',
'Object {',
'"key": "value",',
'},',
'],',
'],',
'"results": Array [',
'Object {',
'"type": "return",',
'"value": Object {',
'"key": "value",',
'},',
'},',
'],',
'}',
].join('\n');
expect(prettyFormat(fn, {indent: 0, plugins: [plugin]})).toBe(expected);
});
test('min option', () => {
const fn = jest.fn(val => val);
fn({key: 'value'});
const expected =
'[MockFunction] {"calls": [[{"key": "value"}]], "results": [{"type": "return", "value": {"key": "value"}}]}';
expect(prettyFormat(fn, {min: true, plugins: [plugin]})).toBe(expected);
});
test('maxDepth option', () => {
const fn1 = jest.fn();
fn1.mockName('atDepth1');
fn1('primitive', {key: 'value'});
const fn2 = jest.fn();
fn2.mockName('atDepth2');
fn2('primitive', {key: 'value'});
const fn3 = jest.fn();
fn3.mockName('atDepth3');
fn3('primitive', {key: 'value'});
const val = {
fn1,
greaterThan1: {
fn2,
greaterThan2: {
fn3,
},
},
};
const expected = [
'Object {', // ++depth === 1
' "fn1": [MockFunction atDepth1] {',
' "calls": Array [', // ++depth === 2
' Array [', // ++depth === 3
' "primitive",',
' [Object],', // ++depth === 4
' ],',
' ],',
' "results": Array [', // ++depth === 2
' Object {', // ++depth === 3
' "type": "return",',
' "value": undefined,',
' },',
' ],',
' },',
' "greaterThan1": Object {', // ++depth === 2
' "fn2": [MockFunction atDepth2] {',
' "calls": Array [', // ++depth === 3
' [Array],', // ++depth === 4
' ],',
' "results": Array [', // ++depth === 3
' [Object],', // ++depth === 4
' ],',
' },',
' "greaterThan2": Object {', // ++depth === 3
' "fn3": [MockFunction atDepth3] {',
' "calls": [Array],', // ++depth === 4
' "results": [Array],', // ++depth === 4
' },',
' },',
' },',
'}',
].join('\n');
expect(prettyFormat(val, {maxDepth: 3, plugins: [plugin]})).toBe(expected);
});