diff --git a/packages/core-data/src/hooks/test/use-entity-record.js b/packages/core-data/src/hooks/test/use-entity-record.js index 6bd20f3494b089..f86906f4f74467 100644 --- a/packages/core-data/src/hooks/test/use-entity-record.js +++ b/packages/core-data/src/hooks/test/use-entity-record.js @@ -9,7 +9,7 @@ jest.mock( '@wordpress/api-fetch' ); /** * External dependencies */ -import { act, render } from '@testing-library/react'; +import { act, render, waitFor } from '@testing-library/react'; /** * Internal dependencies @@ -21,17 +21,10 @@ describe( 'useEntityRecord', () => { let registry; beforeEach( () => { - jest.useFakeTimers(); - registry = createRegistry(); registry.register( coreDataStore ); } ); - afterEach( () => { - jest.runOnlyPendingTimers(); - jest.useRealTimers(); - } ); - const TEST_RECORD = { id: 1, hello: 'world' }; it( 'resolves the entity record when missing from the state', async () => { @@ -60,14 +53,12 @@ describe( 'useEntityRecord', () => { status: 'IDLE', } ); - await act( async () => { - jest.advanceTimersByTime( 1 ); - } ); - // Fetch request should have been issued - expect( triggerFetch ).toHaveBeenCalledWith( { - path: '/wp/v2/widgets/1?context=edit', - } ); + await waitFor( () => + expect( triggerFetch ).toHaveBeenCalledWith( { + path: '/wp/v2/widgets/1?context=edit', + } ) + ); expect( data ).toEqual( { edit: expect.any( Function ), @@ -96,27 +87,25 @@ describe( 'useEntityRecord', () => { ); - await act( async () => { - jest.advanceTimersByTime( 1 ); - } ); - - expect( widget ).toEqual( { - edit: expect.any( Function ), - editedRecord: { hello: 'world', id: 1 }, - hasEdits: false, - record: { hello: 'world', id: 1 }, - save: expect.any( Function ), - hasResolved: true, - isResolving: false, - status: 'SUCCESS', - } ); + await waitFor( () => + expect( widget ).toEqual( { + edit: expect.any( Function ), + editedRecord: { hello: 'world', id: 1 }, + hasEdits: false, + record: { hello: 'world', id: 1 }, + save: expect.any( Function ), + hasResolved: true, + isResolving: false, + status: 'SUCCESS', + } ) + ); await act( async () => { widget.edit( { hello: 'foo' } ); - jest.advanceTimersByTime( 1 ); } ); - expect( widget.hasEdits ).toEqual( true ); + await waitFor( () => expect( widget.hasEdits ).toEqual( true ) ); + expect( widget.record ).toEqual( { hello: 'world', id: 1 } ); expect( widget.editedRecord ).toEqual( { hello: 'foo', id: 1 } ); } ); diff --git a/packages/core-data/src/hooks/test/use-entity-records.js b/packages/core-data/src/hooks/test/use-entity-records.js index e944d260ff574f..d75d2e8af849c9 100644 --- a/packages/core-data/src/hooks/test/use-entity-records.js +++ b/packages/core-data/src/hooks/test/use-entity-records.js @@ -9,7 +9,7 @@ jest.mock( '@wordpress/api-fetch' ); /** * External dependencies */ -import { act, render } from '@testing-library/react'; +import { render, waitFor } from '@testing-library/react'; /** * Internal dependencies @@ -21,17 +21,10 @@ describe( 'useEntityRecords', () => { let registry; beforeEach( () => { - jest.useFakeTimers(); - registry = createRegistry(); registry.register( coreDataStore ); } ); - afterEach( () => { - jest.runOnlyPendingTimers(); - jest.useRealTimers(); - } ); - const TEST_RECORDS = [ { id: 1, hello: 'world1' }, { id: 2, hello: 'world2' }, @@ -60,14 +53,12 @@ describe( 'useEntityRecords', () => { status: 'IDLE', } ); - await act( async () => { - jest.advanceTimersByTime( 1 ); - } ); - // Fetch request should have been issued - expect( triggerFetch ).toHaveBeenCalledWith( { - path: '/wp/v2/widgets?context=edit&status=draft', - } ); + await waitFor( () => + expect( triggerFetch ).toHaveBeenCalledWith( { + path: '/wp/v2/widgets?context=edit&status=draft', + } ) + ); expect( data ).toEqual( { records: TEST_RECORDS, diff --git a/packages/core-data/src/hooks/test/use-query-select.js b/packages/core-data/src/hooks/test/use-query-select.js index 217a76c57f533e..52435f44eddfb3 100644 --- a/packages/core-data/src/hooks/test/use-query-select.js +++ b/packages/core-data/src/hooks/test/use-query-select.js @@ -10,7 +10,7 @@ import { /** * External dependencies */ -import { act, render, screen } from '@testing-library/react'; +import { render, screen, waitFor } from '@testing-library/react'; /** * Internal dependencies @@ -21,8 +21,6 @@ describe( 'useQuerySelect', () => { let registry; beforeEach( () => { - jest.useFakeTimers(); - registry = createRegistry(); registry.registerStore( 'testStore', { reducer: () => ( { foo: 'bar' } ), @@ -33,11 +31,6 @@ describe( 'useQuerySelect', () => { } ); } ); - afterEach( () => { - jest.runOnlyPendingTimers(); - jest.useRealTimers(); - } ); - const getTestComponent = ( mapSelectSpy, dependencyKey ) => ( props ) => { const dependencies = props[ dependencyKey ]; mapSelectSpy.mockImplementation( ( select ) => ( { @@ -177,21 +170,20 @@ describe( 'useQuerySelect', () => { status: 'IDLE', } ); - await act( async () => { - jest.advanceTimersToNextTimer(); - } ); - // Re-render, expect resolved data render( ); - expect( querySelectData ).toEqual( { - data: 15, - isResolving: false, - hasResolved: true, - status: 'SUCCESS', - } ); + + await waitFor( () => + expect( querySelectData ).toEqual( { + data: 15, + isResolving: false, + hasResolved: true, + status: 'SUCCESS', + } ) + ); } ); } ); diff --git a/packages/core-data/src/hooks/test/use-resource-permissions.js b/packages/core-data/src/hooks/test/use-resource-permissions.js index e36bf61bd1d7f1..b7b206c3a879b7 100644 --- a/packages/core-data/src/hooks/test/use-resource-permissions.js +++ b/packages/core-data/src/hooks/test/use-resource-permissions.js @@ -9,7 +9,7 @@ jest.mock( '@wordpress/api-fetch' ); /** * External dependencies */ -import { act, render } from '@testing-library/react'; +import { render, waitFor } from '@testing-library/react'; /** * Internal dependencies @@ -20,8 +20,6 @@ import useResourcePermissions from '../use-resource-permissions'; describe( 'useResourcePermissions', () => { let registry; beforeEach( () => { - jest.useFakeTimers(); - registry = createRegistry(); registry.register( coreDataStore ); @@ -34,11 +32,6 @@ describe( 'useResourcePermissions', () => { } ) ); } ); - afterEach( () => { - jest.runOnlyPendingTimers(); - jest.useRealTimers(); - } ); - it( 'retrieves the relevant permissions for a key-less resource', async () => { let data; const TestComponent = () => { @@ -58,18 +51,15 @@ describe( 'useResourcePermissions', () => { canRead: false, } ); - // Required to make sure no updates happen outside of act() - await act( async () => { - jest.advanceTimersByTime( 1 ); - } ); - - expect( data ).toEqual( { - status: 'SUCCESS', - isResolving: false, - hasResolved: true, - canCreate: true, - canRead: false, - } ); + await waitFor( () => + expect( data ).toEqual( { + status: 'SUCCESS', + isResolving: false, + hasResolved: true, + canCreate: true, + canRead: false, + } ) + ); } ); it( 'retrieves the relevant permissions for a resource with a key', async () => { @@ -93,19 +83,16 @@ describe( 'useResourcePermissions', () => { canDelete: false, } ); - // Required to make sure no updates happen outside of act() - await act( async () => { - jest.advanceTimersByTime( 1 ); - } ); - - expect( data ).toEqual( { - status: 'SUCCESS', - isResolving: false, - hasResolved: true, - canCreate: true, - canRead: false, - canUpdate: false, - canDelete: false, - } ); + await waitFor( () => + expect( data ).toEqual( { + status: 'SUCCESS', + isResolving: false, + hasResolved: true, + canCreate: true, + canRead: false, + canUpdate: false, + canDelete: false, + } ) + ); } ); } );