-
Notifications
You must be signed in to change notification settings - Fork 48.1k
/
Copy pathcomponentStacks-test.js
89 lines (75 loc) · 2.49 KB
/
componentStacks-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
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import {getVersionedRenderImplementation, normalizeCodeLocInfo} from './utils';
describe('component stack', () => {
let React;
let act;
let mockError;
let mockWarn;
beforeEach(() => {
// Intercept native console methods before DevTools bootstraps.
// Normalize component stack locations.
mockError = jest.fn();
mockWarn = jest.fn();
console.error = (...args) => {
mockError(...args.map(normalizeCodeLocInfo));
};
console.warn = (...args) => {
mockWarn(...args.map(normalizeCodeLocInfo));
};
const utils = require('./utils');
act = utils.act;
React = require('react');
});
const {render} = getVersionedRenderImplementation();
// @reactVersion >=16.9
it('should log the current component stack along with an error or warning', () => {
const Grandparent = () => <Parent />;
const Parent = () => <Child />;
const Child = () => {
console.error('Test error.');
console.warn('Test warning.');
return null;
};
act(() => render(<Grandparent />));
expect(mockError).toHaveBeenCalledWith(
'Test error.',
'\n in Child (at **)' +
'\n in Parent (at **)' +
'\n in Grandparent (at **)',
);
expect(mockWarn).toHaveBeenCalledWith(
'Test warning.',
'\n in Child (at **)' +
'\n in Parent (at **)' +
'\n in Grandparent (at **)',
);
});
// This test should have caught #19911
// but didn't because both DevTools and ReactDOM are running in the same memory space,
// so the case we're testing against (DevTools prod build and React DEV build) doesn't exist.
// It would be nice to figure out a way to test this combination at some point...
xit('should disable the current dispatcher before shallow rendering so no effects get scheduled', () => {
let useEffectCount = 0;
const Example = props => {
React.useEffect(() => {
useEffectCount++;
expect(props).toBeDefined();
}, [props]);
console.warn('Warning to trigger appended component stacks.');
return null;
};
act(() => render(<Example test="abc" />));
expect(useEffectCount).toBe(1);
expect(mockWarn).toHaveBeenCalledWith(
'Warning to trigger appended component stacks.',
'\n in Example (at **)',
);
});
});