woocommerce/plugins/woocommerce-admin/client/store/notes/test/resolvers.js

53 lines
1.2 KiB
JavaScript
Raw Normal View History

2018-10-16 22:21:34 +00:00
/*
* @format
*/
/**
* External dependencies
*/
import apiFetch from '@wordpress/api-fetch';
import { dispatch } from '@wordpress/data';
2018-10-16 22:21:34 +00:00
/**
* Internal dependencies
*/
import resolvers from '../resolvers';
const { getNotes } = resolvers;
jest.mock( '@wordpress/data', () => ( {
dispatch: jest.fn().mockReturnValue( {
setNotes: jest.fn(),
} ),
} ) );
2018-10-16 22:21:34 +00:00
jest.mock( '@wordpress/api-fetch', () => jest.fn() );
describe( 'getNotes', () => {
const NOTES_1 = [ { id: 1214 }, { id: 1215 }, { id: 1216 } ];
const NOTES_2 = [ { id: 1 }, { id: 2 }, { id: 3 } ];
beforeAll( () => {
apiFetch.mockImplementation( options => {
if ( options.path === '/wc/v3/admin/notes' ) {
return Promise.resolve( NOTES_1 );
}
if ( options.path === '/wc/v3/admin/notes?page=2' ) {
2018-10-16 22:21:34 +00:00
return Promise.resolve( NOTES_2 );
}
} );
} );
it( 'returns requested data', async () => {
expect.assertions( 1 );
await getNotes();
expect( dispatch().setNotes ).toHaveBeenCalledWith( NOTES_1, undefined );
2018-10-16 22:21:34 +00:00
} );
it( 'returns requested data for a specific query', async () => {
expect.assertions( 1 );
await getNotes( { page: 2 } );
expect( dispatch().setNotes ).toHaveBeenCalledWith( NOTES_2, { page: 2 } );
2018-10-16 22:21:34 +00:00
} );
} );