Skip to content

Commit

Permalink
[TestUtils] Build limited test-utils (facebook#28782)
Browse files Browse the repository at this point in the history
We landed a flag to disable test utils in many builds but we need to
fork the entrypoint to make it work with tests properly. This also
removes test-utils implementations from builds that do not support it.
Currently in OSS builds the only thing in test-utils is a reexport of
`act`
  • Loading branch information
gnoff authored and AndyPengc12 committed Apr 15, 2024
1 parent a10e36a commit b390d75
Show file tree
Hide file tree
Showing 6 changed files with 996 additions and 971 deletions.
125 changes: 103 additions & 22 deletions packages/react-dom/src/__tests__/ReactTestUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

'use strict';

import {act} from 'internal-test-utils';
import * as React from 'react';
import * as ReactDOMClient from 'react-dom/client';
import * as ReactDOMServer from 'react-dom/server';
import * as ReactTestUtils from 'react-dom/test-utils';
let React;
let ReactDOMClient;
let ReactDOMServer;
let ReactTestUtils;
let act;

function getTestDocument(markup) {
const doc = document.implementation.createHTMLDocument('');
Expand All @@ -27,8 +27,105 @@ function getTestDocument(markup) {
}

describe('ReactTestUtils', () => {
beforeEach(() => {
React = require('react');
ReactDOMClient = require('react-dom/client');
ReactDOMServer = require('react-dom/server');
ReactTestUtils = require('react-dom/test-utils');
act = require('internal-test-utils').act;
});

// @gate !disableDOMTestUtils
it('Simulate should have locally attached media events', () => {
expect(Object.keys(ReactTestUtils.Simulate).sort()).toMatchSnapshot();
expect(Object.keys(ReactTestUtils.Simulate).sort()).toMatchInlineSnapshot(`
[
"abort",
"animationEnd",
"animationIteration",
"animationStart",
"auxClick",
"beforeInput",
"blur",
"canPlay",
"canPlayThrough",
"cancel",
"change",
"click",
"close",
"compositionEnd",
"compositionStart",
"compositionUpdate",
"contextMenu",
"copy",
"cut",
"doubleClick",
"drag",
"dragEnd",
"dragEnter",
"dragExit",
"dragLeave",
"dragOver",
"dragStart",
"drop",
"durationChange",
"emptied",
"encrypted",
"ended",
"error",
"focus",
"gotPointerCapture",
"input",
"invalid",
"keyDown",
"keyPress",
"keyUp",
"load",
"loadStart",
"loadedData",
"loadedMetadata",
"lostPointerCapture",
"mouseDown",
"mouseEnter",
"mouseLeave",
"mouseMove",
"mouseOut",
"mouseOver",
"mouseUp",
"paste",
"pause",
"play",
"playing",
"pointerCancel",
"pointerDown",
"pointerEnter",
"pointerLeave",
"pointerMove",
"pointerOut",
"pointerOver",
"pointerUp",
"progress",
"rateChange",
"reset",
"resize",
"scroll",
"seeked",
"seeking",
"select",
"stalled",
"submit",
"suspend",
"timeUpdate",
"toggle",
"touchCancel",
"touchEnd",
"touchMove",
"touchStart",
"transitionEnd",
"volumeChange",
"waiting",
"wheel",
]
`);
});

// @gate !disableDOMTestUtils
Expand Down Expand Up @@ -575,14 +672,6 @@ describe('ReactTestUtils', () => {
});
expect(idCallOrder).toEqual([CHILD]);
});

// @gate disableDOMTestUtils
it('throws', async () => {
expect(ReactTestUtils.Simulate.click).toThrow(
'`Simulate` was removed from `react-dom/test-utils`. ' +
'See https://react.dev/warnings/react-dom-test-utils for more info.',
);
});
});

// @gate !disableDOMTestUtils
Expand Down Expand Up @@ -626,14 +715,6 @@ describe('ReactTestUtils', () => {
expect(renderedComponentType).toBe(instance);
});

// @gate disableDOMTestUtils
it('throws on every removed function', async () => {
expect(ReactTestUtils.isDOMComponent).toThrow(
'`isDOMComponent` was removed from `react-dom/test-utils`. ' +
'See https://react.dev/warnings/react-dom-test-utils for more info.',
);
});

// @gate __DEV__
it('warns when using `act`', () => {
expect(() => {
Expand Down

This file was deleted.

Loading

0 comments on commit b390d75

Please sign in to comment.