Skip to content

Commit 7e33f7e

Browse files
Sebastian Silbermanneps1lon
authored andcommitted
Keep removed APIs but throw with a helpful error.
1 parent 3f3a23c commit 7e33f7e

File tree

2 files changed

+181
-3
lines changed

2 files changed

+181
-3
lines changed

packages/react-dom/src/__tests__/ReactTestUtils-test.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,21 @@
1212
import * as ReactTestUtils from 'react-dom/test-utils';
1313

1414
describe('ReactTestUtils', () => {
15-
it('only contains act', async () => {
15+
it('contains act', async () => {
1616
expect(typeof ReactTestUtils.act).toBe('function');
17-
expect(ReactTestUtils.isDOMComponent).toBe(undefined);
17+
});
18+
19+
it('throws on every removed function with a special message', async () => {
20+
expect(ReactTestUtils.isDOMComponent).toThrowError(
21+
'`isDOMComponent` was removed from `react-dom/test-utils`. ' +
22+
'For testing React, we recommend React Testing Library instead: https://testing-library.com/docs/react-testing-library/intro.',
23+
);
24+
});
25+
26+
it('Simulate throws with a message recommending the relevant React Testing Library API', async () => {
27+
expect(ReactTestUtils.Simulate.click).toThrowError(
28+
'`Simulate` was removed from `react-dom/test-utils`. ' +
29+
'For testing events, we recommend `fireEvent.click` from `@testing-library/react` instead: https://testing-library.com/docs/dom-testing-library/api-events/.',
30+
);
1831
});
1932
});

packages/react-dom/src/test-utils/ReactTestUtils.js

Lines changed: 166 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,169 @@ import * as React from 'react';
1111
// importing directly from the React package instead.
1212
const act = React.act;
1313

14-
export {act};
14+
function makeRemovedFunction(name) {
15+
return function () {
16+
throw new Error(
17+
'`' +
18+
name +
19+
'` was removed from `react-dom/test-utils`. ' +
20+
'For testing React, we recommend React Testing Library instead: https://testing-library.com/docs/react-testing-library/intro.',
21+
);
22+
};
23+
}
24+
25+
const renderIntoDocument = makeRemovedFunction('renderIntoDocument');
26+
const isElement = makeRemovedFunction('isElement');
27+
const isElementOfType = makeRemovedFunction('isElementOfType');
28+
const isDOMComponent = makeRemovedFunction('isDOMComponent');
29+
const isDOMComponentElement = makeRemovedFunction('isDOMComponentElement');
30+
const isCompositeComponent = makeRemovedFunction('isCompositeComponent');
31+
const isCompositeComponentWithType = makeRemovedFunction(
32+
'isCompositeComponentWithType',
33+
);
34+
const findAllInRenderedTree = makeRemovedFunction('findAllInRenderedTree');
35+
const scryRenderedDOMComponentsWithClass = makeRemovedFunction(
36+
'scryRenderedDOMComponentsWithClass',
37+
);
38+
const findRenderedDOMComponentWithClass = makeRemovedFunction(
39+
'findRenderedDOMComponentWithClass',
40+
);
41+
const scryRenderedDOMComponentsWithTag = makeRemovedFunction(
42+
'scryRenderedDOMComponentsWithTag',
43+
);
44+
const findRenderedDOMComponentWithTag = makeRemovedFunction(
45+
'findRenderedDOMComponentWithTag',
46+
);
47+
const scryRenderedComponentsWithType = makeRemovedFunction(
48+
'scryRenderedComponentsWithType',
49+
);
50+
const findRenderedComponentWithType = makeRemovedFunction(
51+
'findRenderedComponentWithType',
52+
);
53+
const mockComponent = makeRemovedFunction('mockComponent');
54+
const nativeTouchData = makeRemovedFunction('nativeTouchData');
55+
56+
// Snapshot of events supported by Simulate before we removed it.
57+
// Do not add new events here since the new ones were never supported in the first place.
58+
const simulatedEventTypes = [
59+
'blur',
60+
'cancel',
61+
'click',
62+
'close',
63+
'contextMenu',
64+
'copy',
65+
'cut',
66+
'auxClick',
67+
'doubleClick',
68+
'dragEnd',
69+
'dragStart',
70+
'drop',
71+
'focus',
72+
'input',
73+
'invalid',
74+
'keyDown',
75+
'keyPress',
76+
'keyUp',
77+
'mouseDown',
78+
'mouseUp',
79+
'paste',
80+
'pause',
81+
'play',
82+
'pointerCancel',
83+
'pointerDown',
84+
'pointerUp',
85+
'rateChange',
86+
'reset',
87+
'resize',
88+
'seeked',
89+
'submit',
90+
'touchCancel',
91+
'touchEnd',
92+
'touchStart',
93+
'volumeChange',
94+
'drag',
95+
'dragEnter',
96+
'dragExit',
97+
'dragLeave',
98+
'dragOver',
99+
'mouseMove',
100+
'mouseOut',
101+
'mouseOver',
102+
'pointerMove',
103+
'pointerOut',
104+
'pointerOver',
105+
'scroll',
106+
'toggle',
107+
'touchMove',
108+
'wheel',
109+
'abort',
110+
'animationEnd',
111+
'animationIteration',
112+
'animationStart',
113+
'canPlay',
114+
'canPlayThrough',
115+
'durationChange',
116+
'emptied',
117+
'encrypted',
118+
'ended',
119+
'error',
120+
'gotPointerCapture',
121+
'load',
122+
'loadedData',
123+
'loadedMetadata',
124+
'loadStart',
125+
'lostPointerCapture',
126+
'playing',
127+
'progress',
128+
'seeking',
129+
'stalled',
130+
'suspend',
131+
'timeUpdate',
132+
'transitionEnd',
133+
'waiting',
134+
'mouseEnter',
135+
'mouseLeave',
136+
'pointerEnter',
137+
'pointerLeave',
138+
'change',
139+
'select',
140+
'beforeInput',
141+
'compositionEnd',
142+
'compositionStart',
143+
'compositionUpdate',
144+
];
145+
146+
const Simulate = {};
147+
148+
simulatedEventTypes.forEach(eventType => {
149+
Simulate[eventType] = function () {
150+
throw new Error(
151+
'`Simulate` was removed from `react-dom/test-utils`. ' +
152+
'For testing events, we recommend `fireEvent.' +
153+
eventType +
154+
'` from `@testing-library/react` instead: https://testing-library.com/docs/dom-testing-library/api-events/.',
155+
);
156+
};
157+
});
158+
159+
export {
160+
act,
161+
// Removed APIs
162+
renderIntoDocument,
163+
isElement,
164+
isElementOfType,
165+
isDOMComponent,
166+
isDOMComponentElement,
167+
isCompositeComponent,
168+
isCompositeComponentWithType,
169+
findAllInRenderedTree,
170+
scryRenderedDOMComponentsWithClass,
171+
findRenderedDOMComponentWithClass,
172+
scryRenderedDOMComponentsWithTag,
173+
findRenderedDOMComponentWithTag,
174+
scryRenderedComponentsWithType,
175+
findRenderedComponentWithType,
176+
mockComponent,
177+
nativeTouchData,
178+
Simulate,
179+
};

0 commit comments

Comments
 (0)