2020-06-12 06:38:33 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { dispatch } from '@wordpress/data';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { createNoticesFromResponse } from '../../notices';
|
|
|
|
|
|
|
|
jest.mock( '@wordpress/data', () => ( {
|
|
|
|
dispatch: jest.fn().mockReturnValue( {
|
|
|
|
createNotice: jest.fn(),
|
|
|
|
} ),
|
|
|
|
} ) );
|
|
|
|
|
|
|
|
describe( 'createNoticesFromResponse', () => {
|
|
|
|
beforeEach( () => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
} );
|
|
|
|
|
|
|
|
const { createNotice } = dispatch( 'core/notices' );
|
|
|
|
|
|
|
|
test( 'should create notice based on message when no errors exist', () => {
|
|
|
|
const response = { message: 'Generic response message' };
|
|
|
|
|
|
|
|
createNoticesFromResponse( response );
|
|
|
|
expect( createNotice ).toHaveBeenCalledWith(
|
|
|
|
'success',
|
2020-07-28 02:32:58 +00:00
|
|
|
response.message
|
2020-06-12 06:38:33 +00:00
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
|
|
|
test( 'should create an error notice when an error code and message exists', () => {
|
|
|
|
const response = { code: 'invalid_code', message: 'Error message' };
|
|
|
|
|
|
|
|
createNoticesFromResponse( response );
|
|
|
|
expect( createNotice ).toHaveBeenCalledWith(
|
|
|
|
'error',
|
2020-07-28 02:32:58 +00:00
|
|
|
response.message
|
2020-06-12 06:38:33 +00:00
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
|
|
|
test( 'should create error messages for each item', () => {
|
2020-07-28 02:32:58 +00:00
|
|
|
const response = {
|
|
|
|
errors: {
|
|
|
|
item1: [ 'Item1 - Error 1.', 'Item1 - Error 2.' ],
|
|
|
|
item2: [ 'Item2 - Error 1.' ],
|
|
|
|
},
|
|
|
|
error_data: [],
|
|
|
|
};
|
2020-06-12 06:38:33 +00:00
|
|
|
|
|
|
|
createNoticesFromResponse( response );
|
2020-07-28 02:32:58 +00:00
|
|
|
expect( createNotice ).toHaveBeenCalledTimes( 2 );
|
|
|
|
const call1 = createNotice.mock.calls[ 0 ];
|
|
|
|
const call2 = createNotice.mock.calls[ 1 ];
|
|
|
|
expect( call1 ).toEqual( [
|
|
|
|
'error',
|
|
|
|
response.errors.item1.join( ' ' ),
|
|
|
|
] );
|
|
|
|
expect( call2 ).toEqual( [ 'error', response.errors.item2[ 0 ] ] );
|
2020-06-12 06:38:33 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
test( 'should not call createNotice when no message or errors exist', () => {
|
|
|
|
const response = { data: {} };
|
|
|
|
|
|
|
|
createNoticesFromResponse( response );
|
|
|
|
expect( createNotice ).not.toHaveBeenCalled();
|
|
|
|
} );
|
|
|
|
} );
|