-
Notifications
You must be signed in to change notification settings - Fork 46.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Convert useFocusWithin to createRoot #28128
Merged
eps1lon
merged 3 commits into
facebook:main
from
eps1lon:test/create-root/useFocusWithin
Jan 29, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -13,7 +13,6 @@ import {createEventTarget, setPointerEvent} from 'dom-event-testing-library'; | |||||||
|
||||||||
let React; | ||||||||
let ReactFeatureFlags; | ||||||||
let ReactDOM; | ||||||||
let ReactDOMClient; | ||||||||
let useFocusWithin; | ||||||||
let act; | ||||||||
|
@@ -25,7 +24,6 @@ function initializeModules(hasPointerEvents) { | |||||||
ReactFeatureFlags.enableScopeAPI = true; | ||||||||
ReactFeatureFlags.enableCreateEventHandleAPI = true; | ||||||||
React = require('react'); | ||||||||
ReactDOM = require('react-dom'); | ||||||||
ReactDOMClient = require('react-dom/client'); | ||||||||
act = require('internal-test-utils').act; | ||||||||
|
||||||||
|
@@ -42,17 +40,22 @@ const table = [[forcePointerEvents], [!forcePointerEvents]]; | |||||||
describe.each(table)(`useFocus`, hasPointerEvents => { | ||||||||
let container; | ||||||||
let container2; | ||||||||
let root; | ||||||||
|
||||||||
beforeEach(() => { | ||||||||
initializeModules(hasPointerEvents); | ||||||||
container = document.createElement('div'); | ||||||||
document.body.appendChild(container); | ||||||||
container2 = document.createElement('div'); | ||||||||
document.body.appendChild(container2); | ||||||||
root = ReactDOMClient.createRoot(container); | ||||||||
}); | ||||||||
|
||||||||
afterEach(() => { | ||||||||
ReactDOM.render(null, container); | ||||||||
afterEach(async () => { | ||||||||
await act(() => { | ||||||||
root.render(null); | ||||||||
}); | ||||||||
|
||||||||
document.body.removeChild(container); | ||||||||
document.body.removeChild(container2); | ||||||||
container = null; | ||||||||
|
@@ -75,7 +78,7 @@ describe.each(table)(`useFocus`, hasPointerEvents => { | |||||||
return <div ref={focusWithinRef} />; | ||||||||
}; | ||||||||
await act(() => { | ||||||||
ReactDOM.render(<Component />, container); | ||||||||
root.render(<Component />); | ||||||||
}); | ||||||||
}; | ||||||||
|
||||||||
|
@@ -111,7 +114,7 @@ describe.each(table)(`useFocus`, hasPointerEvents => { | |||||||
innerRef = React.createRef(); | ||||||||
innerRef2 = React.createRef(); | ||||||||
await act(() => { | ||||||||
ReactDOM.render(<Component show={true} />, container); | ||||||||
root.render(<Component show={true} />); | ||||||||
}); | ||||||||
}; | ||||||||
|
||||||||
|
@@ -188,7 +191,7 @@ describe.each(table)(`useFocus`, hasPointerEvents => { | |||||||
innerRef = React.createRef(); | ||||||||
innerRef2 = React.createRef(); | ||||||||
await act(() => { | ||||||||
ReactDOM.render(<Component show={true} />, container); | ||||||||
root.render(<Component show={true} />); | ||||||||
}); | ||||||||
}; | ||||||||
|
||||||||
|
@@ -314,7 +317,7 @@ describe.each(table)(`useFocus`, hasPointerEvents => { | |||||||
); | ||||||||
}; | ||||||||
await act(() => { | ||||||||
ReactDOM.render(<Component />, container); | ||||||||
root.render(<Component />); | ||||||||
}); | ||||||||
|
||||||||
const target = createEventTarget(inputRef.current); | ||||||||
|
@@ -354,7 +357,7 @@ describe.each(table)(`useFocus`, hasPointerEvents => { | |||||||
}; | ||||||||
|
||||||||
await act(() => { | ||||||||
ReactDOM.render(<Component show={true} />, container); | ||||||||
root.render(<Component show={true} />); | ||||||||
}); | ||||||||
|
||||||||
const inner = innerRef.current; | ||||||||
|
@@ -363,7 +366,10 @@ describe.each(table)(`useFocus`, hasPointerEvents => { | |||||||
target.focus(); | ||||||||
expect(onBeforeBlurWithin).toHaveBeenCalledTimes(0); | ||||||||
expect(onAfterBlurWithin).toHaveBeenCalledTimes(0); | ||||||||
ReactDOM.render(<Component show={false} />, container); | ||||||||
await act(() => { | ||||||||
root.render(<Component show={false} />); | ||||||||
}); | ||||||||
|
||||||||
expect(onBeforeBlurWithin).toHaveBeenCalledTimes(1); | ||||||||
expect(onAfterBlurWithin).toHaveBeenCalledTimes(1); | ||||||||
expect(onAfterBlurWithin).toHaveBeenCalledWith( | ||||||||
|
@@ -391,7 +397,7 @@ describe.each(table)(`useFocus`, hasPointerEvents => { | |||||||
}; | ||||||||
|
||||||||
await act(() => { | ||||||||
ReactDOM.render(<Component show={true} />, container); | ||||||||
root.render(<Component show={true} />); | ||||||||
}); | ||||||||
|
||||||||
const inner = innerRef.current; | ||||||||
|
@@ -400,7 +406,11 @@ describe.each(table)(`useFocus`, hasPointerEvents => { | |||||||
target.focus(); | ||||||||
expect(onBeforeBlurWithin).toHaveBeenCalledTimes(0); | ||||||||
expect(onAfterBlurWithin).toHaveBeenCalledTimes(0); | ||||||||
ReactDOM.render(<Component show={false} />, container); | ||||||||
|
||||||||
await act(() => { | ||||||||
root.render(<Component show={false} />); | ||||||||
}); | ||||||||
|
||||||||
expect(onBeforeBlurWithin).toHaveBeenCalledTimes(1); | ||||||||
expect(onAfterBlurWithin).toHaveBeenCalledTimes(1); | ||||||||
expect(onAfterBlurWithin).toHaveBeenCalledWith( | ||||||||
|
@@ -433,13 +443,16 @@ describe.each(table)(`useFocus`, hasPointerEvents => { | |||||||
}; | ||||||||
|
||||||||
await act(() => { | ||||||||
ReactDOM.render(<Component show={true} />, container); | ||||||||
root.render(<Component show={true} />); | ||||||||
}); | ||||||||
|
||||||||
inputRef.current.focus(); | ||||||||
expect(onBeforeBlurWithin).toHaveBeenCalledTimes(0); | ||||||||
expect(onAfterBlurWithin).toHaveBeenCalledTimes(0); | ||||||||
ReactDOM.render(<Component show={false} />, container); | ||||||||
await act(() => { | ||||||||
root.render(<Component show={false} />); | ||||||||
}); | ||||||||
|
||||||||
expect(onBeforeBlurWithin).toHaveBeenCalledTimes(1); | ||||||||
expect(onAfterBlurWithin).toHaveBeenCalledTimes(1); | ||||||||
}); | ||||||||
|
@@ -469,15 +482,15 @@ describe.each(table)(`useFocus`, hasPointerEvents => { | |||||||
}; | ||||||||
|
||||||||
await act(() => { | ||||||||
ReactDOM.render(<Component show={true} />, container); | ||||||||
root.render(<Component show={true} />); | ||||||||
}); | ||||||||
|
||||||||
const inner = innerRef.current; | ||||||||
const target = createEventTarget(inner); | ||||||||
target.keydown({key: 'Tab'}); | ||||||||
target.focus(); | ||||||||
await act(() => { | ||||||||
ReactDOM.render(<Component show={false} />, container); | ||||||||
root.render(<Component show={false} />); | ||||||||
}); | ||||||||
expect(targetNodes).toEqual([targetNode]); | ||||||||
}); | ||||||||
|
@@ -512,10 +525,10 @@ describe.each(table)(`useFocus`, hasPointerEvents => { | |||||||
); | ||||||||
}; | ||||||||
|
||||||||
const root = ReactDOMClient.createRoot(container2); | ||||||||
const root2 = ReactDOMClient.createRoot(container2); | ||||||||
|
||||||||
await act(() => { | ||||||||
root.render(<Component />); | ||||||||
root2.render(<Component />); | ||||||||
}); | ||||||||
expect(container2.innerHTML).toBe('<div><input></div>'); | ||||||||
|
||||||||
|
@@ -528,14 +541,18 @@ describe.each(table)(`useFocus`, hasPointerEvents => { | |||||||
|
||||||||
suspend = true; | ||||||||
await act(() => { | ||||||||
root.render(<Component />); | ||||||||
root2.render(<Component />); | ||||||||
}); | ||||||||
expect(container2.innerHTML).toBe( | ||||||||
'<div><input style="display: none;">Loading...</div>', | ||||||||
); | ||||||||
expect(onBeforeBlurWithin).toHaveBeenCalledTimes(1); | ||||||||
expect(onAfterBlurWithin).toHaveBeenCalledTimes(1); | ||||||||
resolve(); | ||||||||
await act(() => { | ||||||||
suspend = false; | ||||||||
resolve(); | ||||||||
}); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense, the infinite loop error is because of the
Suggested change
|
||||||||
expect(container2.innerHTML).toBe('<div><input style=""></div>'); | ||||||||
}); | ||||||||
|
||||||||
// @gate www | ||||||||
|
@@ -569,37 +586,41 @@ describe.each(table)(`useFocus`, hasPointerEvents => { | |||||||
); | ||||||||
}; | ||||||||
|
||||||||
const root = ReactDOMClient.createRoot(container2); | ||||||||
const root2 = ReactDOMClient.createRoot(container2); | ||||||||
|
||||||||
await act(() => { | ||||||||
root.render(<Component />); | ||||||||
root2.render(<Component />); | ||||||||
}); | ||||||||
|
||||||||
expect(onBeforeBlurWithin).toHaveBeenCalledTimes(0); | ||||||||
expect(onAfterBlurWithin).toHaveBeenCalledTimes(0); | ||||||||
|
||||||||
suspend = true; | ||||||||
await act(() => { | ||||||||
root.render(<Component />); | ||||||||
root2.render(<Component />); | ||||||||
}); | ||||||||
expect(onBeforeBlurWithin).toHaveBeenCalledTimes(0); | ||||||||
expect(onAfterBlurWithin).toHaveBeenCalledTimes(0); | ||||||||
|
||||||||
await act(() => { | ||||||||
root.render(<Component />); | ||||||||
root2.render(<Component />); | ||||||||
}); | ||||||||
expect(onBeforeBlurWithin).toHaveBeenCalledTimes(0); | ||||||||
expect(onAfterBlurWithin).toHaveBeenCalledTimes(0); | ||||||||
|
||||||||
buttonRef.current.focus(); | ||||||||
suspend = false; | ||||||||
await act(() => { | ||||||||
root.render(<Component />); | ||||||||
root2.render(<Component />); | ||||||||
}); | ||||||||
expect(onBeforeBlurWithin).toHaveBeenCalledTimes(1); | ||||||||
expect(onAfterBlurWithin).toHaveBeenCalledTimes(1); | ||||||||
|
||||||||
resolve(); | ||||||||
await act(() => { | ||||||||
suspend = false; | ||||||||
resolve(); | ||||||||
}); | ||||||||
expect(container2.innerHTML).toBe('<div><input style=""></div>'); | ||||||||
}); | ||||||||
}); | ||||||||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Matches other testing patterns e.g.
react/packages/react-dom/src/__tests__/ReactTestUtilsAct-test.js
Lines 773 to 777 in a8875ea
Without also ensuring we don't throw the promise, we end up in an infinite loop and ultimately crash with
RangeError: Potential infinite loop: exceeded 1500 iterations.