Skip to content

Commit

Permalink
Adjust unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adamziel committed Sep 8, 2021
1 parent eb8c3d6 commit 1ce7581
Showing 1 changed file with 27 additions and 37 deletions.
64 changes: 27 additions & 37 deletions packages/core-data/src/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,17 @@ describe( 'editEntityRecord', () => {
const select = {
getEntity: jest.fn(),
};
const fulfillment = editEntityRecord(
entity.kind,
entity.name,
entity.id,
{}
)( { select } );
expect( select.getEntity ).toHaveBeenCalledTimes( 1 );
await expect( fulfillment ).rejects.toThrow(
const fulfillment = () =>
editEntityRecord(
entity.kind,
entity.name,
entity.id,
{}
)( { select } );
expect( fulfillment ).toThrow(
`The entity being edited (${ entity.kind }, ${ entity.name }) does not have a loaded config.`
);
expect( select.getEntity ).toHaveBeenCalledTimes( 1 );
} );
} );

Expand Down Expand Up @@ -386,21 +387,7 @@ describe( 'receiveCurrentUser', () => {

describe( '__experimentalBatch', () => {
it( 'batches multiple actions together', async () => {
const generator = __experimentalBatch(
[
( { saveEntityRecord: _saveEntityRecord } ) =>
_saveEntityRecord( 'root', 'widget', {} ),
( { saveEditedEntityRecord: _saveEditedEntityRecord } ) =>
_saveEditedEntityRecord( 'root', 'widget', 123 ),
( { deleteEntityRecord: _deleteEntityRecord } ) =>
_deleteEntityRecord( 'root', 'widget', 123, {} ),
],
{ __unstableProcessor: ( inputs ) => Promise.resolve( inputs ) }
);
// Run generator up to `yield getDispatch()`.
const { value: getDispatchControl } = generator.next();
expect( getDispatchControl ).toEqual( { type: 'GET_DISPATCH' } );
const actions = {
const dispatch = {
saveEntityRecord: jest.fn(
( kind, name, record, { __unstableFetch } ) => {
__unstableFetch( {} );
Expand All @@ -420,36 +407,39 @@ describe( '__experimentalBatch', () => {
}
),
};
const dispatch = () => actions;
// Run generator up to `yield __unstableAwaitPromise( ... )`.
const { value: awaitPromiseControl } = generator.next( dispatch );
expect( actions.saveEntityRecord ).toHaveBeenCalledWith(

const results = await __experimentalBatch(
[
( { saveEntityRecord: _saveEntityRecord } ) =>
_saveEntityRecord( 'root', 'widget', {} ),
( { saveEditedEntityRecord: _saveEditedEntityRecord } ) =>
_saveEditedEntityRecord( 'root', 'widget', 123 ),
( { deleteEntityRecord: _deleteEntityRecord } ) =>
_deleteEntityRecord( 'root', 'widget', 123, {} ),
],
{ __unstableProcessor: ( inputs ) => Promise.resolve( inputs ) }
)( { dispatch } );

expect( dispatch.saveEntityRecord ).toHaveBeenCalledWith(
'root',
'widget',
{},
{ __unstableFetch: expect.any( Function ) }
);
expect( actions.saveEditedEntityRecord ).toHaveBeenCalledWith(
expect( dispatch.saveEditedEntityRecord ).toHaveBeenCalledWith(
'root',
'widget',
123,
{ __unstableFetch: expect.any( Function ) }
);
expect( actions.deleteEntityRecord ).toHaveBeenCalledWith(
expect( dispatch.deleteEntityRecord ).toHaveBeenCalledWith(
'root',
'widget',
123,
{},
{ __unstableFetch: expect.any( Function ) }
);
expect( awaitPromiseControl ).toEqual( {
type: 'AWAIT_PROMISE',
promise: expect.any( Promise ),
} );
// Run generator to the end.
const { value: results } = generator.next(
await awaitPromiseControl.promise
);

expect( results ).toEqual( [
{ id: 123, created: true },
{ id: 123, updated: true },
Expand Down

0 comments on commit 1ce7581

Please sign in to comment.