From cd8451d98b98dbd1150e6d0aa96bd93180d57ea8 Mon Sep 17 00:00:00 2001 From: Jarda Snajdr Date: Fri, 16 Dec 2022 13:11:59 +0100 Subject: [PATCH] useSelect: add unit tests for static select mode (#46606) --- .../src/components/use-select/test/index.js | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/packages/data/src/components/use-select/test/index.js b/packages/data/src/components/use-select/test/index.js index 6dccfd74b741f..4b7babcedeaac 100644 --- a/packages/data/src/components/use-select/test/index.js +++ b/packages/data/src/components/use-select/test/index.js @@ -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 ( + + ); + } + + render( + + + + ); + + 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 ); + } ); + } ); } );