Skip to content

Commit ec4d26c

Browse files
authored
ReactDOM: Remove every test-util except act() (#28541)
1 parent 4cc0f8e commit ec4d26c

9 files changed

+164
-5
lines changed

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

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ describe('ReactTestUtils', () => {
3131
expect(Object.keys(ReactTestUtils.Simulate).sort()).toMatchSnapshot();
3232
});
3333

34+
// @gate !disableDOMTestUtils
3435
it('gives Jest mocks a passthrough implementation with mockComponent()', async () => {
3536
class MockedComponent extends React.Component {
3637
render() {
@@ -60,6 +61,7 @@ describe('ReactTestUtils', () => {
6061
expect(container.textContent).toBe('Hello');
6162
});
6263

64+
// @gate !disableDOMTestUtils
6365
it('can scryRenderedComponentsWithType', async () => {
6466
class Child extends React.Component {
6567
render() {
@@ -88,6 +90,7 @@ describe('ReactTestUtils', () => {
8890
expect(scryResults.length).toBe(1);
8991
});
9092

93+
// @gate !disableDOMTestUtils
9194
it('can scryRenderedDOMComponentsWithClass with TextComponent', async () => {
9295
class Wrapper extends React.Component {
9396
render() {
@@ -112,6 +115,7 @@ describe('ReactTestUtils', () => {
112115
expect(scryResults.length).toBe(0);
113116
});
114117

118+
// @gate !disableDOMTestUtils
115119
it('can scryRenderedDOMComponentsWithClass with className contains \\n', async () => {
116120
class Wrapper extends React.Component {
117121
render() {
@@ -136,6 +140,7 @@ describe('ReactTestUtils', () => {
136140
expect(scryResults.length).toBe(1);
137141
});
138142

143+
// @gate !disableDOMTestUtils
139144
it('can scryRenderedDOMComponentsWithClass with multiple classes', async () => {
140145
class Wrapper extends React.Component {
141146
render() {
@@ -187,6 +192,7 @@ describe('ReactTestUtils', () => {
187192
expect(scryResults5.length).toBe(0);
188193
});
189194

195+
// @gate !disableDOMTestUtils
190196
it('traverses children in the correct order', async () => {
191197
class Wrapper extends React.Component {
192198
render() {
@@ -225,6 +231,7 @@ describe('ReactTestUtils', () => {
225231
expect(log).toEqual(['orangepurple', 'orange', 'purple']);
226232
});
227233

234+
// @gate !disableDOMTestUtils
228235
it('should support injected wrapper components as DOM components', async () => {
229236
const injectedDOMComponents = [
230237
'button',
@@ -291,6 +298,7 @@ describe('ReactTestUtils', () => {
291298
expect(ReactTestUtils.isDOMComponent(component.bodyRef.current)).toBe(true);
292299
});
293300

301+
// @gate !disableDOMTestUtils
294302
it('can scry with stateless components involved', async () => {
295303
const Function = () => (
296304
<div>
@@ -320,6 +328,7 @@ describe('ReactTestUtils', () => {
320328
expect(hrs.length).toBe(2);
321329
});
322330

331+
// @gate !disableDOMTestUtils
323332
it('provides a clear error when passing invalid objects to scry', () => {
324333
// This is probably too relaxed but it's existing behavior.
325334
ReactTestUtils.findAllInRenderedTree(null, 'span');
@@ -377,6 +386,7 @@ describe('ReactTestUtils', () => {
377386
});
378387

379388
describe('Simulate', () => {
389+
// @gate !disableDOMTestUtils
380390
it('should change the value of an input field', async () => {
381391
const obj = {
382392
handler: function (e) {
@@ -399,6 +409,7 @@ describe('ReactTestUtils', () => {
399409
);
400410
});
401411

412+
// @gate !disableDOMTestUtils
402413
it('should change the value of an input field in a component', async () => {
403414
class SomeComponent extends React.Component {
404415
inputRef = React.createRef();
@@ -442,6 +453,7 @@ describe('ReactTestUtils', () => {
442453
);
443454
});
444455

456+
// @gate !disableDOMTestUtils
445457
it('should not warn when used with extra properties', async () => {
446458
const CLIENT_X = 100;
447459

@@ -468,6 +480,7 @@ describe('ReactTestUtils', () => {
468480
});
469481
});
470482

483+
// @gate !disableDOMTestUtils
471484
it('should set the type of the event', async () => {
472485
let event;
473486
const stub = jest.fn().mockImplementation(e => {
@@ -488,6 +501,7 @@ describe('ReactTestUtils', () => {
488501
expect(event.nativeEvent.type).toBe('keydown');
489502
});
490503

504+
// @gate !disableDOMTestUtils
491505
it('should work with renderIntoDocument', async () => {
492506
const onChange = jest.fn();
493507

@@ -520,6 +534,7 @@ describe('ReactTestUtils', () => {
520534
);
521535
});
522536

537+
// @gate !disableDOMTestUtils
523538
it('should have mouse enter simulated by test utils', async () => {
524539
const idCallOrder = [];
525540
const recordID = function (id) {
@@ -560,8 +575,17 @@ describe('ReactTestUtils', () => {
560575
});
561576
expect(idCallOrder).toEqual([CHILD]);
562577
});
578+
579+
// @gate disableDOMTestUtils
580+
it('throws', async () => {
581+
expect(ReactTestUtils.Simulate.click).toThrow(
582+
'`Simulate` was removed from `react-dom/test-utils`. ' +
583+
'See https://react.dev/warnings/react-dom-test-utils for more info.',
584+
);
585+
});
563586
});
564587

588+
// @gate !disableDOMTestUtils
565589
it('should call setState callback with no arguments', async () => {
566590
let mockArgs;
567591
class Component extends React.Component {
@@ -573,14 +597,12 @@ describe('ReactTestUtils', () => {
573597
}
574598
}
575599

576-
const container = document.createElement('div');
577-
const root = ReactDOMClient.createRoot(container);
578-
await act(() => {
579-
root.render(<Component />);
580-
});
600+
ReactTestUtils.renderIntoDocument(<Component />);
581601

582602
expect(mockArgs.length).toEqual(0);
583603
});
604+
605+
// @gate !disableDOMTestUtils
584606
it('should find rendered component with type in document', async () => {
585607
class MyComponent extends React.Component {
586608
render() {
@@ -602,4 +624,12 @@ describe('ReactTestUtils', () => {
602624

603625
expect(renderedComponentType).toBe(instance);
604626
});
627+
628+
// @gate disableDOMTestUtils
629+
it('throws on every removed function', async () => {
630+
expect(ReactTestUtils.isDOMComponent).toThrow(
631+
'`isDOMComponent` was removed from `react-dom/test-utils`. ' +
632+
'See https://react.dev/warnings/react-dom-test-utils for more info.',
633+
);
634+
});
605635
});

0 commit comments

Comments
 (0)