Skip to content

Commit

Permalink
Keep removed APIs but throw with a helpful error.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Silbermann committed Mar 11, 2024
1 parent 2e8144f commit 5c74ee7
Show file tree
Hide file tree
Showing 2 changed files with 180 additions and 3 deletions.
16 changes: 14 additions & 2 deletions packages/react-dom/src/__tests__/ReactTestUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,21 @@
import * as ReactTestUtils from 'react-dom/test-utils';

describe('ReactTestUtils', () => {
it('only contains act', async () => {
it('contains act', async () => {
ReactTestUtils.act(() => {});
});

it('throws on every other function with a special message', async () => {
expect(ReactTestUtils.isDOMComponent).toThrowError(
'`isDOMComponent` was removed from `react-dom/test-utils`. ' +
'For testing React, we recommend React Testing Library instead: https://testing-library.com/docs/react-testing-library/intro.',
);
});

expect(ReactTestUtils.isDOMComponent).toBe(undefined);
it('Simulate thorws with a message recomming the relevant React Testing Library API', async () => {
expect(ReactTestUtils.Simulate.click).toThrowError(
'`Simulate` was removed from `react-dom/test-utils`. ' +
'For testing events, we recommend `fireEvent.click` from `@testing-library/react` instead: https://testing-library.com/docs/dom-testing-library/api-events/.',
);
});
});
167 changes: 166 additions & 1 deletion packages/react-dom/src/test-utils/ReactTestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,169 @@ import * as React from 'react';
// importing directly from the React package instead.
const act = React.act;

export {act};
function makeRemovedFunction(name) {
return function () {
throw new Error(
'`' +
name +
'` was removed from `react-dom/test-utils`. ' +
'For testing React, we recommend React Testing Library instead: https://testing-library.com/docs/react-testing-library/intro.',
);
};
}

const renderIntoDocument = makeRemovedFunction('renderIntoDocument');
const isElement = makeRemovedFunction('isElement');
const isElementOfType = makeRemovedFunction('isElementOfType');
const isDOMComponent = makeRemovedFunction('isDOMComponent');
const isDOMComponentElement = makeRemovedFunction('isDOMComponentElement');
const isCompositeComponent = makeRemovedFunction('isCompositeComponent');
const isCompositeComponentWithType = makeRemovedFunction(
'isCompositeComponentWithType',
);
const findAllInRenderedTree = makeRemovedFunction('findAllInRenderedTree');
const scryRenderedDOMComponentsWithClass = makeRemovedFunction(
'scryRenderedDOMComponentsWithClass',
);
const findRenderedDOMComponentWithClass = makeRemovedFunction(
'findRenderedDOMComponentWithClass',
);
const scryRenderedDOMComponentsWithTag = makeRemovedFunction(
'scryRenderedDOMComponentsWithTag',
);
const findRenderedDOMComponentWithTag = makeRemovedFunction(
'findRenderedDOMComponentWithTag',
);
const scryRenderedComponentsWithType = makeRemovedFunction(
'scryRenderedComponentsWithType',
);
const findRenderedComponentWithType = makeRemovedFunction(
'findRenderedComponentWithType',
);
const mockComponent = makeRemovedFunction('mockComponent');
const nativeTouchData = makeRemovedFunction('nativeTouchData');

// Snapshot of events supported by Simulate before we removed it.
// Do not add new events here since the new ones were never supported in the first place.
const simulatedEventTypes = [
'blur',
'cancel',
'click',
'close',
'contextMenu',
'copy',
'cut',
'auxClick',
'doubleClick',
'dragEnd',
'dragStart',
'drop',
'focus',
'input',
'invalid',
'keyDown',
'keyPress',
'keyUp',
'mouseDown',
'mouseUp',
'paste',
'pause',
'play',
'pointerCancel',
'pointerDown',
'pointerUp',
'rateChange',
'reset',
'resize',
'seeked',
'submit',
'touchCancel',
'touchEnd',
'touchStart',
'volumeChange',
'drag',
'dragEnter',
'dragExit',
'dragLeave',
'dragOver',
'mouseMove',
'mouseOut',
'mouseOver',
'pointerMove',
'pointerOut',
'pointerOver',
'scroll',
'toggle',
'touchMove',
'wheel',
'abort',
'animationEnd',
'animationIteration',
'animationStart',
'canPlay',
'canPlayThrough',
'durationChange',
'emptied',
'encrypted',
'ended',
'error',
'gotPointerCapture',
'load',
'loadedData',
'loadedMetadata',
'loadStart',
'lostPointerCapture',
'playing',
'progress',
'seeking',
'stalled',
'suspend',
'timeUpdate',
'transitionEnd',
'waiting',
'mouseEnter',
'mouseLeave',
'pointerEnter',
'pointerLeave',
'change',
'select',
'beforeInput',
'compositionEnd',
'compositionStart',
'compositionUpdate',
];

const Simulate = {};

simulatedEventTypes.forEach(eventType => {
Simulate[eventType] = function () {
throw new Error(
'`Simulate` was removed from `react-dom/test-utils`. ' +
'For testing events, we recommend `fireEvent.' +
eventType +
'` from `@testing-library/react` instead: https://testing-library.com/docs/dom-testing-library/api-events/.',
);
};
});

export {
act,
// Removed APIs
renderIntoDocument,
isElement,
isElementOfType,
isDOMComponent,
isDOMComponentElement,
isCompositeComponent,
isCompositeComponentWithType,
findAllInRenderedTree,
scryRenderedDOMComponentsWithClass,
findRenderedDOMComponentWithClass,
scryRenderedDOMComponentsWithTag,
findRenderedDOMComponentWithTag,
scryRenderedComponentsWithType,
findRenderedComponentWithType,
mockComponent,
nativeTouchData,
Simulate,
};

0 comments on commit 5c74ee7

Please sign in to comment.