useSelect: add unit tests for static select mode#46606
Conversation
|
Size Change: +7 B (0%) Total Size: 1.32 MB
ℹ️ View Unchanged
|
tyxla
left a comment
There was a problem hiding this comment.
LGTM 👍 Thanks!
I hope you don't mind I used the opportunity to refactor fireEvent usage to user-event.
🚀
I actually do mind in this particular case 🙂 These are very low-level tests, some of them try to reproduce precise timing of events -- like a store update between render and effect -- and I don't want any Can you please revert the commit? |
|
I tend to disagree because while I understand your rationale, I consider the |
This reverts commit f97baf6.
That's true, in a realistic UI a click wouldn't happen this way but for this particular test suite it's not relevant. We're doing the clicks just as an easy way to trigger something inside the component. Doing a state update, or calling a function returned from a hook. We could achieve the goal also by assigning the let innerSetFoo;
function TestComponent() {
const [ foo, setFoo ] = useState( 'foo' );
innerSetFoo = setFoo;
return <div>{ foo }</div>;
}
render( <TestComponent /> );
expect( screen ).toHaveTextContent( 'foo' );
act( () => innerSetFoo( 'bar' ) );
expect( screen ).toHaveTextContent( 'bar' ); |
When working on new
useSelectimplementation in #46538 I discovered we don't have any unit tests for the "static select mode", i.e., usinguseSelectin a non-reactive way, typically to read the latest value in an event handler:This PR fixes that omission. There is a new test that passes with the old
useSelect, and will also pass with the newuseSelectin #46538. Test-driven development!