From 4217d324ae5766b3201e682bb4d67b8855652694 Mon Sep 17 00:00:00 2001 From: Ricky Date: Tue, 23 Jan 2024 23:01:53 -0500 Subject: [PATCH] Convert ReactDOMTestSelectors-test.js to createRoot (#27993) Straightforward adding createRoot and act --- .../__tests__/ReactDOMTestSelectors-test.js | 275 ++++++++++++------ .../react-dom/unstable_testing.classic.fb.js | 26 +- .../unstable_testing.experimental.js | 24 +- packages/react-dom/unstable_testing.js | 25 +- .../react-dom/unstable_testing.modern.fb.js | 18 +- packages/react-dom/unstable_testing.stable.js | 22 +- 6 files changed, 301 insertions(+), 89 deletions(-) diff --git a/packages/react-dom/src/__tests__/ReactDOMTestSelectors-test.js b/packages/react-dom/src/__tests__/ReactDOMTestSelectors-test.js index cd7a668246686..65bb49e06a1c8 100644 --- a/packages/react-dom/src/__tests__/ReactDOMTestSelectors-test.js +++ b/packages/react-dom/src/__tests__/ReactDOMTestSelectors-test.js @@ -11,7 +11,7 @@ describe('ReactDOMTestSelectors', () => { let React; - + let createRoot; let act; let createComponentSelector; let createHasPseudoClassSelector; @@ -23,7 +23,6 @@ describe('ReactDOMTestSelectors', () => { let focusWithin; let getFindAllNodesFailureDescription; let observeVisibleRects; - let render; let container; @@ -31,7 +30,8 @@ describe('ReactDOMTestSelectors', () => { jest.resetModules(); React = require('react'); - act = React.unstable_act; + + act = require('internal-test-utils').act; if (__EXPERIMENTAL__ || global.__WWW__) { const ReactDOM = require('react-dom/unstable_testing'); @@ -46,7 +46,7 @@ describe('ReactDOMTestSelectors', () => { getFindAllNodesFailureDescription = ReactDOM.getFindAllNodesFailureDescription; observeVisibleRects = ReactDOM.observeVisibleRects; - render = ReactDOM.render; + createRoot = ReactDOM.createRoot; } container = document.createElement('div'); @@ -59,7 +59,7 @@ describe('ReactDOMTestSelectors', () => { describe('findAllNodes', () => { // @gate www || experimental - it('should support searching from the document root', () => { + it('should support searching from the document root', async () => { function Example() { return (
@@ -68,7 +68,10 @@ describe('ReactDOMTestSelectors', () => { ); } - render(, container); + const root = createRoot(container); + await act(() => { + root.render(); + }); const matches = findAllNodes(document.body, [ createComponentSelector(Example), @@ -79,7 +82,7 @@ describe('ReactDOMTestSelectors', () => { }); // @gate www || experimental - it('should support searching from the container', () => { + it('should support searching from the container', async () => { function Example() { return (
@@ -88,7 +91,10 @@ describe('ReactDOMTestSelectors', () => { ); } - render(, container); + const root = createRoot(container); + await act(() => { + root.render(); + }); const matches = findAllNodes(container, [ createComponentSelector(Example), @@ -99,7 +105,7 @@ describe('ReactDOMTestSelectors', () => { }); // @gate www || experimental - it('should support searching from a previous match if the match had a data-testname', () => { + it('should support searching from a previous match if the match had a data-testname', async () => { function Outer() { return (
@@ -112,7 +118,10 @@ describe('ReactDOMTestSelectors', () => { return
; } - render(, container); + const root = createRoot(container); + await act(() => { + root.render(); + }); let matches = findAllNodes(container, [ createComponentSelector(Outer), @@ -130,7 +139,7 @@ describe('ReactDOMTestSelectors', () => { }); // @gate www || experimental - it('should not support searching from a previous match if the match did not have a data-testname', () => { + it('should not support searching from a previous match if the match did not have a data-testname', async () => { function Outer() { return (
@@ -143,7 +152,10 @@ describe('ReactDOMTestSelectors', () => { return
; } - render(, container); + const root = createRoot(container); + await act(() => { + root.render(); + }); const matches = findAllNodes(container, [createComponentSelector(Outer)]); expect(matches).toHaveLength(1); @@ -160,7 +172,7 @@ describe('ReactDOMTestSelectors', () => { }); // @gate www || experimental - it('should support an multiple component types in the selector array', () => { + it('should support an multiple component types in the selector array', async () => { function Outer() { return ( <> @@ -185,7 +197,10 @@ describe('ReactDOMTestSelectors', () => { ); } - render(, container); + const root = createRoot(container); + await act(() => { + root.render(); + }); let matches = findAllNodes(document.body, [ createComponentSelector(Outer), @@ -214,7 +229,7 @@ describe('ReactDOMTestSelectors', () => { }); // @gate www || experimental - it('should find multiple matches', () => { + it('should find multiple matches', async () => { function Example1() { return (
@@ -232,13 +247,15 @@ describe('ReactDOMTestSelectors', () => { ); } - render( - <> - - - , - container, - ); + const root = createRoot(container); + await act(() => { + root.render( + <> + + + , + ); + }); const matches = findAllNodes(document.body, [ createTestNameSelector('match'), @@ -252,7 +269,7 @@ describe('ReactDOMTestSelectors', () => { }); // @gate www || experimental - it('should ignore nested matches', () => { + it('should ignore nested matches', async () => { function Example() { return (
@@ -261,7 +278,10 @@ describe('ReactDOMTestSelectors', () => { ); } - render(, container); + const root = createRoot(container); + await act(() => { + root.render(); + }); const matches = findAllNodes(document.body, [ createComponentSelector(Example), @@ -272,7 +292,7 @@ describe('ReactDOMTestSelectors', () => { }); // @gate www || experimental - it('should enforce the specific order of selectors', () => { + it('should enforce the specific order of selectors', async () => { function Outer() { return ( <> @@ -285,7 +305,10 @@ describe('ReactDOMTestSelectors', () => { return
; } - render(, container); + const root = createRoot(container); + await act(() => { + root.render(); + }); expect( findAllNodes(document.body, [ @@ -297,7 +320,7 @@ describe('ReactDOMTestSelectors', () => { }); // @gate www || experimental - it('should not search within hidden subtrees', () => { + it('should not search within hidden subtrees', async () => { const ref1 = React.createRef(null); const ref2 = React.createRef(null); @@ -315,7 +338,10 @@ describe('ReactDOMTestSelectors', () => { return
; } - render(, container); + const root = createRoot(container); + await act(() => { + root.render(); + }); const matches = findAllNodes(document.body, [ createComponentSelector(Outer), @@ -327,7 +353,7 @@ describe('ReactDOMTestSelectors', () => { }); // @gate www || experimental - it('should support filtering by display text', () => { + it('should support filtering by display text', async () => { function Example() { return (
@@ -339,7 +365,10 @@ describe('ReactDOMTestSelectors', () => { ); } - render(, container); + const root = createRoot(container); + await act(() => { + root.render(); + }); const matches = findAllNodes(document.body, [ createComponentSelector(Example), @@ -350,7 +379,7 @@ describe('ReactDOMTestSelectors', () => { }); // @gate www || experimental - it('should support filtering by explicit accessibiliy role', () => { + it('should support filtering by explicit accessibiliy role', async () => { function Example() { return (
@@ -364,7 +393,10 @@ describe('ReactDOMTestSelectors', () => { ); } - render(, container); + const root = createRoot(container); + await act(() => { + root.render(); + }); const matches = findAllNodes(document.body, [ createComponentSelector(Example), @@ -375,7 +407,7 @@ describe('ReactDOMTestSelectors', () => { }); // @gate www || experimental - it('should support filtering by explicit secondary accessibiliy role', () => { + it('should support filtering by explicit secondary accessibiliy role', async () => { const ref = React.createRef(); function Example() { @@ -389,7 +421,10 @@ describe('ReactDOMTestSelectors', () => { ); } - render(, container); + const root = createRoot(container); + await act(() => { + root.render(); + }); const matches = findAllNodes(document.body, [ createComponentSelector(Example), @@ -400,7 +435,7 @@ describe('ReactDOMTestSelectors', () => { }); // @gate www || experimental - it('should support filtering by implicit accessibiliy role', () => { + it('should support filtering by implicit accessibiliy role', async () => { function Example() { return (
@@ -412,7 +447,10 @@ describe('ReactDOMTestSelectors', () => { ); } - render(, container); + const root = createRoot(container); + await act(() => { + root.render(); + }); const matches = findAllNodes(document.body, [ createComponentSelector(Example), @@ -423,7 +461,7 @@ describe('ReactDOMTestSelectors', () => { }); // @gate www || experimental - it('should support filtering by implicit accessibiliy role with attributes qualifications', () => { + it('should support filtering by implicit accessibiliy role with attributes qualifications', async () => { function Example() { return (
@@ -435,7 +473,10 @@ describe('ReactDOMTestSelectors', () => { ); } - render(, container); + const root = createRoot(container); + await act(() => { + root.render(); + }); const matches = findAllNodes(document.body, [ createComponentSelector(Example), @@ -446,7 +487,7 @@ describe('ReactDOMTestSelectors', () => { }); // @gate www || experimental - it('should support searching ahead with the has() selector', () => { + it('should support searching ahead with the has() selector', async () => { function Example() { return (
@@ -466,7 +507,10 @@ describe('ReactDOMTestSelectors', () => { ); } - render(, container); + const root = createRoot(container); + await act(() => { + root.render(); + }); const matches = findAllNodes(document.body, [ createComponentSelector(Example), @@ -489,13 +533,16 @@ describe('ReactDOMTestSelectors', () => { }); // @gate www || experimental - it('should throw if an invalid host root is specified', () => { + it('should throw if an invalid host root is specified', async () => { const ref = React.createRef(); function Example() { return
; } - render(, container); + const root = createRoot(container); + await act(() => { + root.render(); + }); expect(() => findAllNodes(ref.current, [])).toThrow( 'Invalid host root specified. Should be either a React container or a node with a testname attribute.', @@ -505,7 +552,7 @@ describe('ReactDOMTestSelectors', () => { describe('getFindAllNodesFailureDescription', () => { // @gate www || experimental - it('should describe findAllNodes failures caused by the component type selector', () => { + it('should describe findAllNodes failures caused by the component type selector', async () => { function Outer() { return ; } @@ -516,7 +563,10 @@ describe('ReactDOMTestSelectors', () => { return
; } - render(, container); + const root = createRoot(container); + await act(() => { + root.render(); + }); const description = getFindAllNodesFailureDescription(document.body, [ createComponentSelector(Outer), @@ -535,7 +585,7 @@ No matching component was found for: }); // @gate www || experimental - it('should return null if findAllNodes was able to find a match', () => { + it('should return null if findAllNodes was able to find a match', async () => { function Example() { return (
@@ -544,7 +594,10 @@ No matching component was found for: ); } - render(, container); + const root = createRoot(container); + await act(() => { + root.render(); + }); const description = getFindAllNodesFailureDescription(document.body, [ createComponentSelector(Example), @@ -571,7 +624,7 @@ No matching component was found for: } // @gate www || experimental - it('should return a single rect for a component that returns a single root host element', () => { + it('should return a single rect for a component that returns a single root host element', async () => { const ref = React.createRef(); function Example() { @@ -583,7 +636,10 @@ No matching component was found for: ); } - render(, container); + const root = createRoot(container); + await act(() => { + root.render(); + }); setBoundingClientRect(ref.current, { x: 10, @@ -605,7 +661,7 @@ No matching component was found for: }); // @gate www || experimental - it('should return a multiple rects for multiple matches', () => { + it('should return a multiple rects for multiple matches', async () => { const outerRef = React.createRef(); const innerRef = React.createRef(); @@ -621,7 +677,10 @@ No matching component was found for: return
; } - render(, container); + const root = createRoot(container); + await act(() => { + root.render(); + }); setBoundingClientRect(outerRef.current, { x: 10, @@ -655,7 +714,7 @@ No matching component was found for: }); // @gate www || experimental - it('should return a multiple rects for single match that returns a fragment', () => { + it('should return a multiple rects for single match that returns a fragment', async () => { const refA = React.createRef(); const refB = React.createRef(); @@ -671,7 +730,10 @@ No matching component was found for: ); } - render(, container); + const root = createRoot(container); + await act(() => { + root.render(); + }); setBoundingClientRect(refA.current, { x: 10, @@ -705,7 +767,7 @@ No matching component was found for: }); // @gate www || experimental - it('should merge overlapping rects', () => { + it('should merge overlapping rects', async () => { const refA = React.createRef(); const refB = React.createRef(); const refC = React.createRef(); @@ -720,7 +782,10 @@ No matching component was found for: ); } - render(, container); + const root = createRoot(container); + await act(() => { + root.render(); + }); setBoundingClientRect(refA.current, { x: 10, @@ -760,7 +825,7 @@ No matching component was found for: }); // @gate www || experimental - it('should merge some types of adjacent rects (if they are the same in one dimension)', () => { + it('should merge some types of adjacent rects (if they are the same in one dimension)', async () => { const refA = React.createRef(); const refB = React.createRef(); const refC = React.createRef(); @@ -783,7 +848,10 @@ No matching component was found for: ); } - render(, container); + const root = createRoot(container); + await act(() => { + root.render(); + }); // A, B, and C are all adjacent and/or overlapping, with the same height. setBoundingClientRect(refA.current, { @@ -860,7 +928,7 @@ No matching component was found for: }); // @gate www || experimental - it('should not search within hidden subtrees', () => { + it('should not search within hidden subtrees', async () => { const refA = React.createRef(); const refB = React.createRef(); const refC = React.createRef(); @@ -875,7 +943,10 @@ No matching component was found for: ); } - render(, container); + const root = createRoot(container); + await act(() => { + root.render(); + }); setBoundingClientRect(refA.current, { x: 10, @@ -917,7 +988,7 @@ No matching component was found for: describe('focusWithin', () => { // @gate www || experimental - it('should return false if the specified component path has no matches', () => { + it('should return false if the specified component path has no matches', async () => { function Example() { return ; } @@ -928,7 +999,10 @@ No matching component was found for: return null; } - render(, container); + const root = createRoot(container); + await act(() => { + root.render(); + }); const didFocus = focusWithin(document.body, [ createComponentSelector(Example), @@ -938,7 +1012,7 @@ No matching component was found for: }); // @gate www || experimental - it('should return false if there are no focusable elements within the matched subtree', () => { + it('should return false if there are no focusable elements within the matched subtree', async () => { function Example() { return ; } @@ -946,7 +1020,10 @@ No matching component was found for: return 'not focusable'; } - render(, container); + const root = createRoot(container); + await act(() => { + root.render(); + }); const didFocus = focusWithin(document.body, [ createComponentSelector(Example), @@ -956,7 +1033,7 @@ No matching component was found for: }); // @gate www || experimental - it('should return false if the only focusable elements are disabled', () => { + it('should return false if the only focusable elements are disabled', async () => { function Example() { return ( ; } - render(, container); + const root = createRoot(container); + await act(() => { + root.render(); + }); const didFocus = focusWithin(document.body, [ createComponentSelector(Example), @@ -988,7 +1071,7 @@ No matching component was found for: }); // @gate www || experimental - it('should successfully focus the first focusable element within the tree', () => { + it('should successfully focus the first focusable element within the tree', async () => { const secondRef = React.createRef(null); const handleFirstFocus = jest.fn(); @@ -1029,7 +1112,10 @@ No matching component was found for: ); } - render(, container); + const root = createRoot(container); + await act(() => { + root.render(); + }); const didFocus = focusWithin(document.body, [ createComponentSelector(Example), @@ -1043,7 +1129,7 @@ No matching component was found for: }); // @gate www || experimental - it('should successfully focus the first focusable element even if application logic interferes', () => { + it('should successfully focus the first focusable element even if application logic interferes', async () => { const ref = React.createRef(null); const handleFocus = jest.fn(event => { @@ -1061,7 +1147,10 @@ No matching component was found for: ); } - render(, container); + const root = createRoot(container); + await act(() => { + root.render(); + }); const didFocus = focusWithin(document.body, [ createComponentSelector(Example), @@ -1073,7 +1162,7 @@ No matching component was found for: }); // @gate www || experimental - it('should not focus within hidden subtrees', () => { + it('should not focus within hidden subtrees', async () => { const secondRef = React.createRef(null); const handleFirstFocus = jest.fn(); @@ -1116,7 +1205,10 @@ No matching component was found for: ); } - render(, container); + const root = createRoot(container); + await act(() => { + root.render(); + }); const didFocus = focusWithin(document.body, [ createComponentSelector(Example), @@ -1197,14 +1289,17 @@ No matching component was found for: }); // @gate www || experimental - it('should notify a listener when the underlying instance intersection changes', () => { + it('should notify a listener when the underlying instance intersection changes', async () => { const ref = React.createRef(null); function Example() { return
; } - render(, container); + const root = createRoot(container); + await act(() => { + root.render(); + }); // Stub out the size of the element this test will be observing. const rect = { @@ -1234,7 +1329,7 @@ No matching component was found for: }); // @gate www || experimental - it('should notify a listener of multiple targets when the underlying instance intersection changes', () => { + it('should notify a listener of multiple targets when the underlying instance intersection changes', async () => { const ref1 = React.createRef(null); const ref2 = React.createRef(null); @@ -1247,7 +1342,10 @@ No matching component was found for: ); } - render(, container); + const root = createRoot(container); + await act(() => { + root.render(); + }); // Stub out the size of the element this test will be observing. const rect1 = { @@ -1311,14 +1409,17 @@ No matching component was found for: }); // @gate www || experimental - it('should stop listening when its disconnected', () => { + it('should stop listening when its disconnected', async () => { const ref = React.createRef(null); function Example() { return
; } - render(, container); + const root = createRoot(container); + await act(() => { + root.render(); + }); // Stub out the size of the element this test will be observing. const rect = { @@ -1346,7 +1447,7 @@ No matching component was found for: // This test reuires gating because it relies on the __DEV__ only commit hook to work. // @gate www || experimental && __DEV__ - it('should update which targets its listening to after a commit', () => { + it('should update which targets its listening to after a commit', async () => { const ref1 = React.createRef(null); const ref2 = React.createRef(null); @@ -1363,7 +1464,10 @@ No matching component was found for: ); } - render(, container); + const root = createRoot(container); + await act(() => { + root.render(); + }); // Stub out the size of the element this test will be observing. const rect1 = { @@ -1389,7 +1493,7 @@ No matching component was found for: {rect: rect1, ratio: 1}, ]); - act(() => increment()); + await act(() => increment()); const rect2 = { x: 110, @@ -1412,7 +1516,7 @@ No matching component was found for: {rect: rect2, ratio: 0.25}, ]); - act(() => increment()); + await act(() => increment()); handleVisibilityChange.mockClear(); @@ -1425,7 +1529,7 @@ No matching component was found for: }); // @gate www || experimental - it('should not observe components within hidden subtrees', () => { + it('should not observe components within hidden subtrees', async () => { const ref1 = React.createRef(null); const ref2 = React.createRef(null); @@ -1438,7 +1542,10 @@ No matching component was found for: ); } - render(, container); + const root = createRoot(container); + await act(() => { + root.render(); + }); // Stub out the size of the element this test will be observing. const rect1 = { diff --git a/packages/react-dom/unstable_testing.classic.fb.js b/packages/react-dom/unstable_testing.classic.fb.js index 2f623787958d3..6dd68f3669dc3 100644 --- a/packages/react-dom/unstable_testing.classic.fb.js +++ b/packages/react-dom/unstable_testing.classic.fb.js @@ -7,7 +7,31 @@ * @flow */ -export * from './index.classic.fb.js'; +export { + createPortal, + findDOMNode, + flushSync, + hydrate, + render, + unmountComponentAtNode, + unstable_batchedUpdates, + unstable_createEventHandle, + unstable_renderSubtreeIntoContainer, + unstable_runWithPriority, // DO NOT USE: Temporarily exposed to migrate off of Scheduler.runWithPriority. + useFormStatus, + useFormState, + prefetchDNS, + preconnect, + preload, + preloadModule, + preinit, + preinitModule, + version, + __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED, +} from './index.classic.fb.js'; + +export {createRoot, hydrateRoot} from './client.js'; + export { createComponentSelector, createHasPseudoClassSelector, diff --git a/packages/react-dom/unstable_testing.experimental.js b/packages/react-dom/unstable_testing.experimental.js index 3ed081d85b9b1..b84561272bef3 100644 --- a/packages/react-dom/unstable_testing.experimental.js +++ b/packages/react-dom/unstable_testing.experimental.js @@ -7,7 +7,29 @@ * @flow */ -export * from './index.experimental.js'; +export { + createPortal, + findDOMNode, + flushSync, + hydrate, + render, + unmountComponentAtNode, + unstable_batchedUpdates, + unstable_renderSubtreeIntoContainer, + useFormStatus, + useFormState, + prefetchDNS, + preconnect, + preload, + preloadModule, + preinit, + preinitModule, + version, + __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED, +} from './index.experimental.js'; + +export {createRoot, hydrateRoot} from './client.js'; + export { createComponentSelector, createHasPseudoClassSelector, diff --git a/packages/react-dom/unstable_testing.js b/packages/react-dom/unstable_testing.js index fdad56d13c77d..9729a427e4aa6 100644 --- a/packages/react-dom/unstable_testing.js +++ b/packages/react-dom/unstable_testing.js @@ -7,7 +7,30 @@ * @flow */ -export * from './index.js'; +export { + createPortal, + findDOMNode, + flushSync, + hydrate, + render, + unmountComponentAtNode, + unstable_batchedUpdates, + unstable_createEventHandle, + unstable_renderSubtreeIntoContainer, + unstable_runWithPriority, // DO NOT USE: Temporarily exposed to migrate off of Scheduler.runWithPriority. + useFormStatus, + useFormState, + prefetchDNS, + preconnect, + preload, + preloadModule, + preinit, + preinitModule, + version, +} from './index.js'; + +export {createRoot, hydrateRoot} from './client.js'; + export { createComponentSelector, createHasPseudoClassSelector, diff --git a/packages/react-dom/unstable_testing.modern.fb.js b/packages/react-dom/unstable_testing.modern.fb.js index c0a0e45ad79ca..17e78ae012adc 100644 --- a/packages/react-dom/unstable_testing.modern.fb.js +++ b/packages/react-dom/unstable_testing.modern.fb.js @@ -7,7 +7,23 @@ * @flow */ -export * from './index.modern.fb.js'; +export { + createPortal, + flushSync, + unstable_batchedUpdates, + unstable_createEventHandle, + useFormStatus, + useFormState, + prefetchDNS, + preconnect, + preload, + preloadModule, + preinit, + preinitModule, + version, + __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED, +} from './index.modern.fb.js'; +export {createRoot, hydrateRoot} from './client.js'; export { createComponentSelector, createHasPseudoClassSelector, diff --git a/packages/react-dom/unstable_testing.stable.js b/packages/react-dom/unstable_testing.stable.js index b983e17bef9bd..453e8b4fdece7 100644 --- a/packages/react-dom/unstable_testing.stable.js +++ b/packages/react-dom/unstable_testing.stable.js @@ -7,4 +7,24 @@ * @flow */ -export * from './index.stable.js'; +export { + createPortal, + findDOMNode, + flushSync, + hydrate, + render, + unmountComponentAtNode, + unstable_batchedUpdates, + unstable_renderSubtreeIntoContainer, + useFormStatus, + useFormState, + prefetchDNS, + preconnect, + preload, + preloadModule, + preinit, + preinitModule, + version, + __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED, +} from './index.stable.js'; +export {createRoot, hydrateRoot} from './client.js';