-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathunsafe.test.jsx
125 lines (110 loc) · 4.9 KB
/
unsafe.test.jsx
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
/* eslint camelcase: 0, max-classes-per-file: 0, lines-between-class-members: 0, react/no-deprecated: 0 */
import React, { Component } from 'react';
import { render } from '@testing-library/react';
import ReactTestUtils from 'react-dom/test-utils';
import { traceLifecycle } from '../src';
// React Testing Library discourages tests on instances, but these test are extremely tedious to express without them,
// as there is no observable difference between the legacy methods and their UNSAFE_ counterparts.
// Return the TracedComponent instance for traceLifecycle(Comp).
const getTracedComponentInstance = (Comp) => {
let tracingComponentInstance; // Need TracingComponent instance as root for ReactTestUtils.findAllInRenderedTree().
const TracingComp = traceLifecycle(Comp);
class SpyComp extends TracingComp {
constructor(props, context) {
super(props, context);
tracingComponentInstance = this;
}
}
/* eslint-disable no-console */
// Disable console.warn to suppress React warnings about using legacy methods (emitted once per method).
const consoleWarn = console.warn;
console.warn = () => {};
render(<SpyComp/>);
console.warn = consoleWarn;
/* eslint-enable no-console */
const [tracedInstance] =
ReactTestUtils.findAllInRenderedTree(tracingComponentInstance, (c) => c.constructor.name === 'TracedComponent');
return tracedInstance;
};
describe('unsafe', () => {
it('Traces old methods if only old methods are defined', () => {
class Comp extends Component {
componentWillMount() {}
componentWillReceiveProps() {}
componentWillUpdate() {}
render() {
return '';
}
}
const tracedInstance = getTracedComponentInstance(Comp);
expect(tracedInstance).toHaveProperty('componentWillMount');
expect(tracedInstance).toHaveProperty('componentWillReceiveProps');
expect(tracedInstance).toHaveProperty('componentWillUpdate');
expect(tracedInstance).not.toHaveProperty('UNSAFE_componentWillMount');
expect(tracedInstance).not.toHaveProperty('UNSAFE_componentWillReceiveProps');
expect(tracedInstance).not.toHaveProperty('UNSAFE_componentWillUpdate');
});
it('Traces UNSAFE_ methods if only UNSAFE_ methods are defined', () => {
class Comp extends Component {
UNSAFE_componentWillMount() {}
UNSAFE_componentWillReceiveProps() {}
UNSAFE_componentWillUpdate() {}
render() {
return '';
}
}
const tracedInstance = getTracedComponentInstance(Comp);
expect(tracedInstance).not.toHaveProperty('componentWillMount');
expect(tracedInstance).not.toHaveProperty('componentWillReceiveProps');
expect(tracedInstance).not.toHaveProperty('componentWillUpdate');
expect(tracedInstance).toHaveProperty('UNSAFE_componentWillMount');
expect(tracedInstance).toHaveProperty('UNSAFE_componentWillReceiveProps');
expect(tracedInstance).toHaveProperty('UNSAFE_componentWillUpdate');
});
it('Traces UNSAFE_ methods if neither old nor UNSAFE_ methods are defined (1)', () => {
// Need two tests, since we need to define at least one UNSAFE_ method to turn it into a legacy component.
class Comp extends Component {
UNSAFE_componentWillMount() {}
render() {
return '';
}
}
const tracedInstance = getTracedComponentInstance(Comp);
expect(tracedInstance).not.toHaveProperty('componentWillReceiveProps');
expect(tracedInstance).not.toHaveProperty('componentWillUpdate');
expect(tracedInstance).toHaveProperty('UNSAFE_componentWillReceiveProps');
expect(tracedInstance).toHaveProperty('UNSAFE_componentWillUpdate');
});
it('Traces UNSAFE_ methods if neither old nor UNSAFE_ methods are defined (2)', () => {
class Comp extends Component {
UNSAFE_componentWillReceiveProps() {}
render() {
return '';
}
}
const tracedInstance = getTracedComponentInstance(Comp);
expect(tracedInstance).not.toHaveProperty('componentWillMount');
expect(tracedInstance).toHaveProperty('UNSAFE_componentWillMount');
});
it('Traces both old and UNSAFE_ methods if both are defined', () => {
// Kind of silly, but this is allowed by React.
class Comp extends Component {
UNSAFE_componentWillMount() {}
UNSAFE_componentWillReceiveProps() {}
UNSAFE_componentWillUpdate() {}
componentWillMount() {}
componentWillReceiveProps() {}
componentWillUpdate() {}
render() {
return '';
}
}
const tracedInstance = getTracedComponentInstance(Comp);
expect(tracedInstance).toHaveProperty('componentWillMount');
expect(tracedInstance).toHaveProperty('componentWillReceiveProps');
expect(tracedInstance).toHaveProperty('componentWillUpdate');
expect(tracedInstance).toHaveProperty('UNSAFE_componentWillMount');
expect(tracedInstance).toHaveProperty('UNSAFE_componentWillReceiveProps');
expect(tracedInstance).toHaveProperty('UNSAFE_componentWillUpdate');
});
});