diff --git a/packages/data/src/components/use-dispatch/test/use-dispatch.js b/packages/data/src/components/use-dispatch/test/use-dispatch.js index 115279dd097df9..810fc675121840 100644 --- a/packages/data/src/components/use-dispatch/test/use-dispatch.js +++ b/packages/data/src/components/use-dispatch/test/use-dispatch.js @@ -1,7 +1,8 @@ /** * External dependencies */ -import TestRenderer, { act } from 'react-test-renderer'; +import { render, screen } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; /** * Internal dependencies @@ -11,165 +12,171 @@ import createReduxStore from '../../../redux-store'; import { createRegistry } from '../../../registry'; import { RegistryProvider } from '../../registry-provider'; +jest.useRealTimers(); + describe( 'useDispatch', () => { + const counterStore = { + reducer: ( state = 0, action ) => { + if ( action.type === 'INC' ) { + return state + 1; + } + + return state; + }, + actions: { + inc: () => ( { type: 'INC' } ), + }, + selectors: { + get: ( state ) => state, + }, + }; + let registry; beforeEach( () => { registry = createRegistry(); } ); - it( 'returns dispatch function from store with no store name provided', () => { - registry.registerStore( 'demoStore', { - reducer: ( state ) => state, - actions: { - foo: () => 'bar', - }, - } ); + it( 'returns dispatch function from store with no store name provided', async () => { + const user = userEvent.setup(); + registry.registerStore( 'demo', counterStore ); + const TestComponent = () => { - return
; - }; - const Component = () => { const dispatch = useDispatch(); - return ; + return + + + ); }; - let testRenderer; - act( () => { - testRenderer = TestRenderer.create( - - - - ); - } ); - const testInstance = testRenderer.root; + render( + + + + ); - act( () => { - testInstance.findByType( 'button' ).props.onClick(); - } ); + await user.click( + screen.getByRole( 'button', { + name: 'foo', + } ) + ); - expect( firstRegistryAction ).toHaveBeenCalledTimes( 1 ); - expect( secondRegistryAction ).toHaveBeenCalledTimes( 0 ); + expect( fooAction ).toBeCalledTimes( 1 ); + expect( barAction ).toBeCalledTimes( 0 ); - const secondRegistry = createRegistry(); - secondRegistry.registerStore( 'demo', { - reducer, - actions: { - noop: secondRegistryAction, - }, - } ); + await user.click( + screen.getByRole( 'button', { + name: 'bar', + } ) + ); - act( () => { - testRenderer.update( - - - - ); - } ); - act( () => { - testInstance.findByType( 'button' ).props.onClick(); - } ); - expect( firstRegistryAction ).toHaveBeenCalledTimes( 1 ); - expect( secondRegistryAction ).toHaveBeenCalledTimes( 1 ); + expect( fooAction ).toBeCalledTimes( 1 ); + expect( barAction ).toBeCalledTimes( 1 ); } ); } );