-
Notifications
You must be signed in to change notification settings - Fork 860
test: React 18 act() adjustments #6943
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
Merged
tkajtoch
merged 4 commits into
elastic:feature/react-18
from
tkajtoch:test/react-18-act-adjustments
Jul 14, 2023
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7c84353
test: replace @testing-library/dom with @testing-library/react as the…
tkajtoch c588347
test: wrap function calls with act() that need it
tkajtoch d0b31a3
test: use act() wrapper for jest.advanceTimersByTime calls and add ac…
tkajtoch d119980
test: wait for element to be rendered before taking a snapshot to fix…
tkajtoch 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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
|
|
||
| import React, { ReactNode } from 'react'; | ||
| import { shallow, render, mount } from 'enzyme'; | ||
| import { act } from '@testing-library/react'; | ||
| import { | ||
| requiredProps, | ||
| findTestSubject, | ||
|
|
@@ -89,7 +90,9 @@ describe('props', () => { | |
| /> | ||
| ); | ||
|
|
||
| component.setState({ isListOpen: true }); | ||
| act(() => { | ||
| component.setState({ isListOpen: true }); | ||
|
Member
Author
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. React 17 |
||
| }); | ||
| expect(takeMountedSnapshot(component)).toMatchSnapshot(); | ||
| }); | ||
|
|
||
|
|
@@ -282,10 +285,16 @@ describe('behavior', () => { | |
| /> | ||
| ); | ||
|
|
||
| component.setState({ searchValue: 'foo' }); | ||
| const searchInput = findTestSubject(component, 'comboBoxSearchInput'); | ||
| searchInput.simulate('focus'); | ||
| searchInput.simulate('keyDown', { key: comboBoxKeys.ENTER }); | ||
| act(() => { | ||
| component.setState({ searchValue: 'foo' }); | ||
| }); | ||
|
|
||
| act(() => { | ||
| const searchInput = findTestSubject(component, 'comboBoxSearchInput'); | ||
| searchInput.simulate('focus'); | ||
| searchInput.simulate('keyDown', { key: comboBoxKeys.ENTER }); | ||
| }); | ||
|
|
||
| expect(onCreateOptionHandler).toHaveBeenCalledTimes(1); | ||
| expect(onCreateOptionHandler).toHaveBeenNthCalledWith(1, 'foo', options); | ||
| }); | ||
|
|
@@ -344,15 +353,20 @@ describe('behavior', () => { | |
| /> | ||
| ); | ||
|
|
||
| component.setState({ searchValue: 'foo' }); | ||
| const searchInput = findTestSubject(component, 'comboBoxSearchInput'); | ||
| searchInput.simulate('focus'); | ||
|
|
||
| const searchInputNode = searchInput.getDOMNode(); | ||
| // React doesn't support `focusout` so we have to manually trigger it | ||
| searchInputNode.dispatchEvent( | ||
| new FocusEvent('focusout', { bubbles: true }) | ||
| ); | ||
| act(() => { | ||
| component.setState({ searchValue: 'foo' }); | ||
| searchInput.simulate('focus'); | ||
| }); | ||
|
|
||
| act(() => { | ||
| const searchInputNode = searchInput.getDOMNode(); | ||
| // React doesn't support `focusout` so we have to manually trigger it | ||
| searchInputNode.dispatchEvent( | ||
| new FocusEvent('focusout', { bubbles: true }) | ||
| ); | ||
| }); | ||
|
|
||
| expect(onCreateOptionHandler).toHaveBeenCalledTimes(1); | ||
| expect(onCreateOptionHandler).toHaveBeenNthCalledWith(1, 'foo', options); | ||
|
|
||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
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.
@testing-library/domis unused at this point as this PR switches to usingfireEventimported from@testing-library/react(and automatically wrapped inact()so we don't need to do that manually)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.
Nice - was that a recent change in
@testing-library/react? Or did I just add a library I didn't need to when I was originally setting up RTL? 😅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.
I believe it existed for quite some time now, but it was never well documented and people get confused all the time!
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.
Doh! Thanks a million for the cleanup in that case! 💯