Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka committed Jul 14, 2024
1 parent cb39c98 commit b6ac044
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 23 deletions.
8 changes: 6 additions & 2 deletions packages/block-library/src/block/test/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ describe( 'Synced patterns', () => {
if ( path.startsWith( endpoint ) ) {
response = getMockedReusableBlock( id );
}
return Promise.resolve( response );
return Promise.resolve( {
json: () => Promise.resolve( response ),
} );
} );

const screen = await initializeEditor( {
Expand Down Expand Up @@ -229,7 +231,9 @@ describe( 'Synced patterns', () => {
response.content.raw = `<!-- wp:image {"id":1,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://cldup.com/cXyG__fTLN.jpg" alt="" class="wp-image-1"/></figure>
<!-- /wp:image -->`;
return Promise.resolve( response );
return Promise.resolve( {
json: () => Promise.resolve( response ),
} );
} );

const screen = await initializeEditor( {
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/image/test/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function mockGetMedia( media ) {
const FETCH_MEDIA = {
request: {
path: `/wp/v2/media/1?context=edit`,
parse: false,
},
response: {
source_url: 'https://cldup.com/cXyG__fTLN.jpg',
Expand Down
8 changes: 5 additions & 3 deletions packages/core-data/src/hooks/test/use-entity-record.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ describe( 'useEntityRecord', () => {
} );

const TEST_RECORD = { id: 1, hello: 'world' };
const TEST_RECORD_RESPONSE = { json: () => Promise.resolve( TEST_RECORD ) };

it( 'resolves the entity record when missing from the state', async () => {
// Provide response
triggerFetch.mockImplementation( () => TEST_RECORD );
triggerFetch.mockImplementation( () => TEST_RECORD_RESPONSE );

let data;
const TestComponent = () => {
Expand Down Expand Up @@ -60,6 +61,7 @@ describe( 'useEntityRecord', () => {
await waitFor( () =>
expect( triggerFetch ).toHaveBeenCalledWith( {
path: '/wp/v2/widgets/1?context=edit',
parse: false,
} )
);

Expand All @@ -79,7 +81,7 @@ describe( 'useEntityRecord', () => {

it( 'applies edits to the entity record', async () => {
// Provide response
triggerFetch.mockImplementation( () => TEST_RECORD );
triggerFetch.mockImplementation( () => TEST_RECORD_RESPONSE );

let widget;
const TestComponent = () => {
Expand Down Expand Up @@ -119,7 +121,7 @@ describe( 'useEntityRecord', () => {
} );

it( 'does not resolve entity record when disabled via options', async () => {
triggerFetch.mockImplementation( () => TEST_RECORD );
triggerFetch.mockImplementation( () => TEST_RECORD_RESPONSE );

let data;
const TestComponent = ( { enabled } ) => {
Expand Down
35 changes: 20 additions & 15 deletions packages/core-data/src/test/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {

describe( 'getEntityRecord', () => {
const POST_TYPE = { slug: 'post' };
const POST_TYPE_RESPONSE = { json: () => Promise.resolve( POST_TYPE ) };
const ENTITIES = [
{
name: 'postType',
Expand All @@ -27,28 +28,37 @@ describe( 'getEntityRecord', () => {
baseURLParams: { context: 'edit' },
},
];
const registry = { batch: ( callback ) => callback() };

let dispatch;
beforeEach( async () => {
triggerFetch.mockReset();
} );

it( 'yields with requested post type', async () => {
const dispatch = Object.assign( jest.fn(), {
dispatch = Object.assign( jest.fn(), {
receiveEntityRecords: jest.fn(),
__unstableAcquireStoreLock: jest.fn(),
__unstableReleaseStoreLock: jest.fn(),
receiveUserPermission: jest.fn(),
finishResolution: jest.fn(),
} );
triggerFetch.mockReset();
} );

it( 'yields with requested post type', async () => {
// Provide entities
dispatch.mockReturnValueOnce( ENTITIES );

// Provide response
triggerFetch.mockImplementation( () => POST_TYPE );
triggerFetch.mockImplementation( () => POST_TYPE_RESPONSE );

await getEntityRecord( 'root', 'postType', 'post' )( { dispatch } );
await getEntityRecord(
'root',
'postType',
'post'
)( { dispatch, registry } );

// Fetch request should have been issued.
expect( triggerFetch ).toHaveBeenCalledWith( {
path: '/wp/v2/types/post?context=edit',
parse: false,
} );

// The record should have been received.
Expand All @@ -75,24 +85,18 @@ describe( 'getEntityRecord', () => {
const select = {
hasEntityRecords: jest.fn( () => {} ),
};

const dispatch = Object.assign( jest.fn(), {
receiveEntityRecords: jest.fn(),
__unstableAcquireStoreLock: jest.fn(),
__unstableReleaseStoreLock: jest.fn(),
} );
// Provide entities
dispatch.mockReturnValueOnce( ENTITIES );

// Provide response
triggerFetch.mockImplementation( () => POST_TYPE );
triggerFetch.mockImplementation( () => POST_TYPE_RESPONSE );

await getEntityRecord(
'root',
'postType',
'post',
query
)( { dispatch, select } );
)( { dispatch, select, registry } );

// Check resolution cache for an existing entity that fulfills the request with query.
expect( select.hasEntityRecords ).toHaveBeenCalledWith(
Expand All @@ -104,6 +108,7 @@ describe( 'getEntityRecord', () => {
// Trigger apiFetch, test that the query is present in the url.
expect( triggerFetch ).toHaveBeenCalledWith( {
path: '/wp/v2/types/post?context=view&_envelope=1',
parse: false,
} );

// The record should have been received.
Expand Down
12 changes: 9 additions & 3 deletions packages/editor/src/store/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ describe( 'Post actions', () => {
method === 'GET' &&
path.startsWith( '/wp/v2/types/post' )
) {
return {};
return {
json: () => Promise.resolve( {} ),
};
}

throw {
Expand Down Expand Up @@ -178,7 +180,9 @@ describe( 'Post actions', () => {
path.startsWith( '/wp/v2/types/post' ) ||
path.startsWith( `/wp/v2/posts/${ postId }` )
) {
return {};
return {
json: () => Promise.resolve( {} ),
};
}
}

Expand Down Expand Up @@ -262,7 +266,9 @@ describe( 'Post actions', () => {
method === 'GET' &&
path.startsWith( '/wp/v2/types/post' )
) {
return {};
return {
json: () => Promise.resolve( {} ),
};
}

throw {
Expand Down

0 comments on commit b6ac044

Please sign in to comment.