Skip to content
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

useSelect: add unit tests for static select mode #46606

Merged
merged 3 commits into from
Dec 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions packages/data/src/components/use-select/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1146,4 +1146,38 @@ describe( 'useSelect', () => {
expect( screen.getByRole( 'status' ) ).toHaveTextContent( '10' );
} );
} );

describe( 'static store selection mode', () => {
it( 'can read the current value from store', () => {
registry.registerStore( 'testStore', {
reducer: ( s = 0, a ) => ( a.type === 'INC' ? s + 1 : s ),
actions: { inc: () => ( { type: 'INC' } ) },
selectors: { get: ( s ) => s },
} );

const record = jest.fn();

function TestComponent() {
const { get } = useSelect( 'testStore' );
return (
<button onClick={ () => record( get() ) }>record</button>
);
}

render(
<RegistryProvider value={ registry }>
<TestComponent />
</RegistryProvider>
);

fireEvent.click( screen.getByRole( 'button' ) );
expect( record ).toHaveBeenLastCalledWith( 0 );

// no need to act() as the component doesn't react to the updates
registry.dispatch( 'testStore' ).inc();

fireEvent.click( screen.getByRole( 'button' ) );
expect( record ).toHaveBeenLastCalledWith( 1 );
} );
} );
} );