fix: enzyme testing replaced by react tester #36301
Closed
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.
Main changes between the first test and the second
Enzyme (used in the first test) is replaced by @testing-library/react, which is the library
currently recommended for testing React components.
Shallow and renderer.create() are removed and RTL render() is used to render components.
Screen, waitFor, and userEvent are used to interact with elements and check their status.
Using userEvent to Simulate Interactions
In the first test, Enzyme's .simulate('click') was used.
In the second test, userEvent.click() is used, which more realistically simulates the interaction of the
user.
Management of Asynchronous States
WaitFor() and act(async () => {}) are added to wait for changes to the DOM, which improves the
test reliability.
Change in Interface Validation
Instead of component.toJSON() and expect(tree).toMatchSnapshot(), now use
expect(container).toMatchSnapshot().
screen.queryByTestId() and screen.getByTestId() are used to check the existence of elements.
Added the following dependencies:
"@testing-library/react": "^12.1.5":
"@testing-library/jest-dom": "^6.6.3":
"@testing-library/user-event": "^12.8.3",
Enzyme dependencies were uninstalled:
"enzyme": "3.11.0",
"enzyme-adapter-react-16": "1.15.8",
"jest-enzyme": "7.1.2",