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

core-data: Refactor tests to use real timers #47218

Merged
merged 1 commit into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
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
51 changes: 20 additions & 31 deletions packages/core-data/src/hooks/test/use-entity-record.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 () => {
Expand Down Expand Up @@ -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 ),
Expand Down Expand Up @@ -96,27 +87,25 @@ describe( 'useEntityRecord', () => {
</RegistryProvider>
);

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 } );
} );
Expand Down
21 changes: 6 additions & 15 deletions packages/core-data/src/hooks/test/use-entity-records.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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' },
Expand Down Expand Up @@ -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,
Expand Down
28 changes: 10 additions & 18 deletions packages/core-data/src/hooks/test/use-query-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,8 +21,6 @@ describe( 'useQuerySelect', () => {
let registry;

beforeEach( () => {
jest.useFakeTimers();

registry = createRegistry();
registry.registerStore( 'testStore', {
reducer: () => ( { foo: 'bar' } ),
Expand All @@ -33,11 +31,6 @@ describe( 'useQuerySelect', () => {
} );
} );

afterEach( () => {
jest.runOnlyPendingTimers();
jest.useRealTimers();
} );

const getTestComponent = ( mapSelectSpy, dependencyKey ) => ( props ) => {
const dependencies = props[ dependencyKey ];
mapSelectSpy.mockImplementation( ( select ) => ( {
Expand Down Expand Up @@ -177,21 +170,20 @@ describe( 'useQuerySelect', () => {
status: 'IDLE',
} );

await act( async () => {
jest.advanceTimersToNextTimer();
} );

// Re-render, expect resolved data
render(
<RegistryProvider value={ registry }>
<TestComponent />
</RegistryProvider>
);
expect( querySelectData ).toEqual( {
data: 15,
isResolving: false,
hasResolved: true,
status: 'SUCCESS',
} );

await waitFor( () =>
expect( querySelectData ).toEqual( {
data: 15,
isResolving: false,
hasResolved: true,
status: 'SUCCESS',
} )
);
} );
} );
55 changes: 21 additions & 34 deletions packages/core-data/src/hooks/test/use-resource-permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,8 +20,6 @@ import useResourcePermissions from '../use-resource-permissions';
describe( 'useResourcePermissions', () => {
let registry;
beforeEach( () => {
jest.useFakeTimers();

registry = createRegistry();
registry.register( coreDataStore );

Expand All @@ -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 = () => {
Expand All @@ -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 () => {
Expand All @@ -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,
} )
);
} );
} );