-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Data: Refactor 'withDispatch' tests to use RTL #44855
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
Conversation
|
Size Change: +71 B (0%) Total Size: 1.27 MB
ℹ️ View Unchanged
|
| ).resolves.toEqual( { | ||
| type: 'increment', | ||
| count, | ||
| } ); |
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.
This is not a bad thing to check: that the increment function prop injected to Component has the right return value. In case of a plain action, it's a promise of the action object. Would be nice to keep the check.
I would put it to the user-land code, to the button onClick handler:
async function onClick() {
const result = await props.increment();
expect( result ).toEqual( ... );
}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.
Restored in 1859ac6.
|
|
||
| // Function value reference should not have changed in props update. | ||
| expect( testInstance.findByType( 'button' ).props.onClick ).toBe( | ||
| incrementBeforeSetProps |
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.
This is also a useful property worth testing: that the increment prop is always an identical function, even though after a rerender with different props it behaves differently. One way to test this is wrapping the button in a helper component with React.memo and then checking there are no extra rerenders.
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.
Would something like 2fb959a work?
| expect( registry.select( 'counter' ).getCount() ).toBe( 3 ); | ||
|
|
||
| await user.click( screen.getByRole( 'button' ) ); | ||
| expect( registry.select( 'counter' ).getCount() ).toBe( 7 ); |
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 find this odd. How come from 1 after the first click it becomes 7 after the third click? I'd expect this to stay 1, 2 and 3, respectively.
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.
The test is testing whether the dispatch function can correctly read current state with registry.select. And the increment action increments by current_value + 1, equivalent to new_value = 2 * current_value + 1.
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.
Sorry for being unclear - I understand how it works, but I think it might be easy to miss why it's working that way. Might be a good idea to elaborate, either in a comment or in the name of the test.
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 will add the inline comment.
| const Button = memo( ( { onClick } ) => { | ||
| buttonSpy(); | ||
| return <button onClick={ onClick } />; | ||
| } ); |
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.
It's also possible, and elegant, to wrap the component function itself in a spy:
const ButtonSpy = jest.fn( ( { onClick } ) => <button onClick={ onClick } /> );
const Button = memo( ButtonSpy );The useSelect tests use this pattern to count renders.
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.
Oh, that's a neat one. Thanks, @jsnajdr!
jsnajdr
left a comment
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.
👍
What?
PR of #44780.
PR refactors
withDispatchHOC tests to use@testing-library/reactinstead ofreact-test-renderer.Why?
It is a part of recent efforts to use
@testing-library/reactas the project's primary testing library.How?
Updates the
rendermethod to use RTL, improves assertions and usesuserEventfor user actions.Testing Instructions