Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Return action call tests
  • Loading branch information
Mamaduka committed Oct 10, 2022
commit f6c1a3c672e40cfdf375bf981852d6ae45ab0d7b
48 changes: 48 additions & 0 deletions packages/data/src/components/use-dispatch/test/use-dispatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,52 @@ describe( 'useDispatch', () => {
expect( firstRegistry.select( 'demo' ).get() ).toBe( 1 );
expect( secondRegistry.select( 'demo' ).get() ).toBe( 1 );
} );

it( 'dispatches returned actions correctly', async () => {
const user = userEvent.setup();
const fooAction = jest.fn( () => ( { type: '__INERT__' } ) );
const barAction = jest.fn( () => ( { type: '__INERT__' } ) );
const store = createReduxStore( 'demo', {
reducer: ( state ) => state,
actions: {
foo: fooAction,
bar: barAction,
},
} );
registry.register( store );

const TestComponent = () => {
const { foo, bar } = useDispatch( store );
return (
<>
<button onClick={ foo }>foo</button>
<button onClick={ bar }>bar</button>
</>
);
};

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

await user.click(
screen.getByRole( 'button', {
name: 'foo',
} )
);

expect( fooAction ).toBeCalledTimes( 1 );
expect( barAction ).toBeCalledTimes( 0 );

await user.click(
screen.getByRole( 'button', {
name: 'bar',
} )
);

expect( fooAction ).toBeCalledTimes( 1 );
expect( barAction ).toBeCalledTimes( 1 );
} );
} );