Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Jun 1, 2018
1 parent 269e45d commit caa9ae9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 35 deletions.
27 changes: 15 additions & 12 deletions components/higher-order/with-api-data/test/request.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* WordPress dependencies
*/
import apiRequest from '@wordpress/api-request';

/**
* Internal dependencies
*/
Expand Down Expand Up @@ -25,15 +30,13 @@ describe( 'request', () => {
),
};

let wpApiRequest;
beforeEach( () => {
getStablePath.clear();
for ( const key in cache ) {
delete cache[ key ];
}

wpApiRequest = wp.apiRequest;
wp.apiRequest = jest.fn( () => ( {
apiRequest.mockReturnValue = {
// jQuery.Deferred aren't true promises, particularly in their
// treatment of resolved arguments. $.ajax will spread resolved
// arguments, but this is not valid for Promise (only single).
Expand All @@ -43,11 +46,7 @@ describe( 'request', () => {
'success',
xhr
) ),
} ) );
} );

afterEach( () => {
wp.apiRequest = wpApiRequest;
};
} );

describe( 'getResponseHeaders()', () => {
Expand Down Expand Up @@ -97,7 +96,7 @@ describe( 'request', () => {
} );

return awaitResponse.then( ( data ) => {
expect( wp.apiRequest ).toHaveBeenCalled();
expect( apiRequest ).toHaveBeenCalled();
expect( data ).toEqual( actualResponse );
} );
} );
Expand Down Expand Up @@ -129,6 +128,10 @@ describe( 'request', () => {
} );

describe( 'request()', () => {
beforeEach( () => {
apiRequest.mockClear();
} );

it( 'should try from cache for GET', () => {
cache[ getStablePath( '/wp?c=5&a=5&b=5' ) ] = actualResponse;
const awaitResponse = request( {
Expand All @@ -137,7 +140,7 @@ describe( 'request', () => {
} );

return awaitResponse.then( ( data ) => {
expect( wp.apiRequest ).not.toHaveBeenCalled();
expect( apiRequest ).not.toHaveBeenCalled();
expect( data ).toEqual( actualResponse );
} );
} );
Expand All @@ -150,7 +153,7 @@ describe( 'request', () => {
} );

return awaitResponse.then( ( data ) => {
expect( wp.apiRequest ).toHaveBeenCalled();
expect( apiRequest ).toHaveBeenCalled();
expect( data ).toEqual( actualResponse );
} );
} );
Expand All @@ -162,7 +165,7 @@ describe( 'request', () => {
} );

return awaitResponse.then( ( data ) => {
expect( wp.apiRequest ).toHaveBeenCalled();
expect( apiRequest ).toHaveBeenCalled();
expect( data ).toEqual( actualResponse );
} );
} );
Expand Down
35 changes: 12 additions & 23 deletions editor/store/test/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
registerBlockType,
createBlock,
} from '@wordpress/blocks';
import apiRequest from '@wordpress/api-request';

/**
* Internal dependencies
Expand Down Expand Up @@ -535,6 +536,10 @@ describe( 'effects', () => {
describe( '.FETCH_SHARED_BLOCKS', () => {
const handler = effects.FETCH_SHARED_BLOCKS;

afterEach( () => {
jest.unmock( '@wordpress/api-request' );
} );

it( 'should fetch multiple shared blocks', () => {
const promise = Promise.resolve( [
{
Expand All @@ -543,9 +548,8 @@ describe( 'effects', () => {
content: '<!-- wp:test-block {"name":"Big Bird"} /-->',
},
] );

apiRequest.mockReturnValue = promise;
set( global, [ 'wp', 'api', 'getPostTypeRoute' ], () => 'blocks' );
set( global, [ 'wp', 'apiRequest' ], () => promise );

const dispatch = jest.fn();
const store = { getState: noop, dispatch };
Expand Down Expand Up @@ -581,9 +585,8 @@ describe( 'effects', () => {
title: 'My cool block',
content: '<!-- wp:test-block {"name":"Big Bird"} /-->',
} );

apiRequest.mockReturnValue = promise;
set( global, [ 'wp', 'api', 'getPostTypeRoute' ], () => 'blocks' );
set( global, [ 'wp', 'apiRequest' ], () => promise );

const dispatch = jest.fn();
const store = { getState: noop, dispatch };
Expand Down Expand Up @@ -615,9 +618,8 @@ describe( 'effects', () => {

it( 'should handle an API error', () => {
const promise = Promise.reject( {} );

apiRequest.mockReturnValue = promise;
set( global, [ 'wp', 'api', 'getPostTypeRoute' ], () => 'blocks' );
set( global, [ 'wp', 'apiRequest' ], () => promise );

const dispatch = jest.fn();
const store = { getState: noop, dispatch };
Expand Down Expand Up @@ -656,14 +658,10 @@ describe( 'effects', () => {
const handler = effects.SAVE_SHARED_BLOCK;

it( 'should save a shared block and swap its id', () => {
let modelAttributes;
const promise = Promise.resolve( { id: 456 } );
apiRequest.mockReturnValue = promise;

set( global, [ 'wp', 'api', 'getPostTypeRoute' ], () => 'blocks' );
set( global, [ 'wp', 'apiRequest' ], ( request ) => {
modelAttributes = request.data;
return promise;
} );

const sharedBlock = { id: 123, title: 'My cool block' };
const parsedBlock = createBlock( 'core/test-block', { name: 'Big Bird' } );
Expand All @@ -678,12 +676,6 @@ describe( 'effects', () => {

handler( saveSharedBlock( 123 ), store );

expect( modelAttributes ).toEqual( {
id: 123,
title: 'My cool block',
content: '<!-- wp:test-block {\"name\":\"Big Bird\"} /-->',
} );

return promise.then( () => {
expect( dispatch ).toHaveBeenCalledWith( {
type: 'SAVE_SHARED_BLOCK_SUCCESS',
Expand All @@ -695,9 +687,8 @@ describe( 'effects', () => {

it( 'should handle an API error', () => {
const promise = Promise.reject( {} );

apiRequest.mockReturnValue = promise;
set( global, [ 'wp', 'api', 'getPostTypeRoute' ], () => 'blocks' );
set( global, [ 'wp', 'apiRequest' ], () => promise );

const sharedBlock = { id: 123, title: 'My cool block' };
const parsedBlock = createBlock( 'core/test-block', { name: 'Big Bird' } );
Expand Down Expand Up @@ -726,9 +717,8 @@ describe( 'effects', () => {

it( 'should delete a shared block', () => {
const promise = Promise.resolve( {} );

apiRequest.mockReturnValue = promise;
set( global, [ 'wp', 'api', 'getPostTypeRoute' ], () => 'blocks' );
set( global, [ 'wp', 'apiRequest' ], () => promise );

const associatedBlock = createBlock( 'core/block', { ref: 123 } );
const sharedBlock = { id: 123, title: 'My cool block' };
Expand Down Expand Up @@ -766,9 +756,8 @@ describe( 'effects', () => {

it( 'should handle an API error', () => {
const promise = Promise.reject( {} );

apiRequest.mockReturnValue = promise;
set( global, [ 'wp', 'api', 'getPostTypeRoute' ], () => 'blocks' );
set( global, [ 'wp', 'apiRequest' ], () => promise );

const sharedBlock = { id: 123, title: 'My cool block' };
const parsedBlock = createBlock( 'core/test-block', { name: 'Big Bird' } );
Expand Down
9 changes: 9 additions & 0 deletions test/unit/setup-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,12 @@ jest.mock( '../../components/button', () => {
}
};
} );

jest.mock( '@wordpress/api-request', () => {
const apiRequest = jest.fn( () => {
return apiRequest.mockReturnValue;
} );
apiRequest.mockReturnValue = 'mock this value by overriding apiRequest.mockReturnValue';

return apiRequest;
} );

0 comments on commit caa9ae9

Please sign in to comment.