2019-09-04 16:07:00 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { formatError } from '../errors';
|
|
|
|
|
|
|
|
describe( 'formatError', () => {
|
2023-08-30 06:56:28 +00:00
|
|
|
const mockResponseBody = JSON.stringify( { message: 'Lorem Ipsum' } );
|
|
|
|
const mockMalformedJson = '{ "message": "Lorem Ipsum"';
|
|
|
|
|
2019-09-04 16:07:00 +00:00
|
|
|
test( 'should format general errors', async () => {
|
|
|
|
const error = await formatError( {
|
|
|
|
message: 'Lorem Ipsum',
|
|
|
|
} );
|
|
|
|
const expectedError = {
|
|
|
|
message: 'Lorem Ipsum',
|
|
|
|
type: 'general',
|
|
|
|
};
|
|
|
|
|
|
|
|
expect( error ).toEqual( expectedError );
|
|
|
|
} );
|
|
|
|
|
|
|
|
test( 'should format API errors', async () => {
|
2023-08-30 06:56:28 +00:00
|
|
|
const mockResponse = new Response( mockResponseBody, { status: 400 } );
|
|
|
|
|
|
|
|
const error = await formatError( mockResponse );
|
2019-09-04 16:07:00 +00:00
|
|
|
const expectedError = {
|
|
|
|
message: 'Lorem Ipsum',
|
|
|
|
type: 'api',
|
|
|
|
};
|
|
|
|
|
|
|
|
expect( error ).toEqual( expectedError );
|
|
|
|
} );
|
|
|
|
|
|
|
|
test( 'should format JSON parse errors', async () => {
|
2023-08-30 06:56:28 +00:00
|
|
|
const mockResponse = new Response( mockMalformedJson, { status: 400 } );
|
|
|
|
|
|
|
|
const error = await formatError( mockResponse );
|
2019-09-04 16:07:00 +00:00
|
|
|
|
2023-11-02 13:01:42 +00:00
|
|
|
expect( error.message ).toContain(
|
|
|
|
'invalid json response body at reason:'
|
|
|
|
);
|
|
|
|
expect( error.type ).toEqual( 'general' );
|
2019-09-04 16:07:00 +00:00
|
|
|
} );
|
|
|
|
} );
|