forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
withComponentStack-test.js
190 lines (155 loc) · 4.8 KB
/
withComponentStack-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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react-core
*/
'use strict';
function normalizeCodeLocInfo(str) {
return str && str.replace(/at .+?:\d+/g, 'at **');
}
function expectHelper(spy, prefix, ...expectedArgs) {
const expectedStack = expectedArgs.pop();
expect(spy).toHaveBeenCalledTimes(1);
const actualArgs = spy.calls.mostRecent().args;
let actualStack = undefined;
if (expectedStack !== undefined) {
actualStack = actualArgs.pop();
expect(normalizeCodeLocInfo(actualStack)).toBe(expectedStack);
}
expect(actualArgs).toHaveLength(expectedArgs.length);
actualArgs.forEach((actualArg, index) => {
const expectedArg = expectedArgs[index];
expect(actualArg).toBe(
index === 0 ? `${prefix}: ${expectedArg}` : expectedArg,
);
});
}
function expectMessageAndStack(...expectedArgs) {
expectHelper(console.error, 'error', ...expectedArgs);
expectHelper(console.warn, 'warn', ...expectedArgs);
}
describe('withComponentStack', () => {
let React = null;
let ReactTestRenderer = null;
let error = null;
let warn = null;
beforeEach(() => {
jest.resetModules();
jest.mock('scheduler', () => require('scheduler/unstable_mock'));
React = require('react');
ReactTestRenderer = require('react-test-renderer');
error = React.error;
warn = React.warn;
spyOnDevAndProd(console, 'error');
spyOnDevAndProd(console, 'warn');
});
if (!__DEV__) {
it('does nothing in production mode', () => {
error('error');
warn('warning');
expect(console.error).toHaveBeenCalledTimes(0);
expect(console.warn).toHaveBeenCalledTimes(0);
});
}
if (__DEV__) {
it('does not include component stack when called outside of render', () => {
error('error: logged outside of render');
warn('warn: logged outside of render');
expectMessageAndStack('logged outside of render', undefined);
});
it('should support multiple args', () => {
function Component() {
error('error: number:', 123, 'boolean:', true);
warn('warn: number:', 123, 'boolean:', true);
return null;
}
ReactTestRenderer.create(<Component />);
expectMessageAndStack(
'number:',
123,
'boolean:',
true,
'\n in Component (at **)',
);
});
it('includes component stack when called from a render method', () => {
class Parent extends React.Component {
render() {
return <Child />;
}
}
function Child() {
error('error: logged in child render method');
warn('warn: logged in child render method');
return null;
}
ReactTestRenderer.create(<Parent />);
expectMessageAndStack(
'logged in child render method',
'\n in Child (at **)' + '\n in Parent (at **)',
);
});
it('includes component stack when called from a render phase lifecycle method', () => {
function Parent() {
return <Child />;
}
class Child extends React.Component {
UNSAFE_componentWillMount() {
error('error: logged in child cWM lifecycle');
warn('warn: logged in child cWM lifecycle');
}
render() {
return null;
}
}
ReactTestRenderer.create(<Parent />);
expectMessageAndStack(
'logged in child cWM lifecycle',
'\n in Child (at **)' + '\n in Parent (at **)',
);
});
it('includes component stack when called from a commit phase lifecycle method', () => {
function Parent() {
return <Child />;
}
class Child extends React.Component {
componentDidMount() {
error('error: logged in child cDM lifecycle');
warn('warn: logged in child cDM lifecycle');
}
render() {
return null;
}
}
ReactTestRenderer.create(<Parent />);
expectMessageAndStack(
'logged in child cDM lifecycle',
'\n in Child (at **)' + '\n in Parent (at **)',
);
});
it('includes component stack when called from a passive effect handler', () => {
class Parent extends React.Component {
render() {
return <Child />;
}
}
function Child() {
React.useEffect(() => {
error('error: logged in child render method');
warn('warn: logged in child render method');
});
return null;
}
ReactTestRenderer.act(() => {
ReactTestRenderer.create(<Parent />);
});
expectMessageAndStack(
'logged in child render method',
'\n in Child (at **)' + '\n in Parent (at **)',
);
});
}
});