Skip to content

Commit

Permalink
Testing: use simpler jest.fn() mocks for api-fetch calls in unit tests (
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad authored and gziolo committed Jul 31, 2018
1 parent 33f380f commit d903fe8
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 16 deletions.
6 changes: 0 additions & 6 deletions __mocks__/@wordpress/api-fetch/index.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import request, {
isRequestMethod,
} from '../request';

jest.mock( '@wordpress/api-fetch' );
jest.mock( '@wordpress/api-fetch', () => jest.fn() );

describe( 'request', () => {
const actualResponse = {
Expand All @@ -31,13 +31,7 @@ describe( 'request', () => {
delete cache[ key ];
}

apiFetch.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).
// Instead, we emulate by invoking the callback manually.
then: ( callback ) => Promise.resolve( callback( actualResponse ) ),
};
apiFetch.mockImplementation( () => Promise.resolve( actualResponse ) );
} );

describe( 'getCachedResponse()', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core-data/src/test/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import apiFetch from '@wordpress/api-fetch';
import { getMethodName, defaultEntities, getKindEntities } from '../entities';
import { addEntities } from '../actions';

jest.mock( '@wordpress/api-fetch' );
jest.mock( '@wordpress/api-fetch', () => jest.fn() );

describe( 'getMethodName', () => {
it( 'should return the right method name for an entity with the root kind', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core-data/src/test/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import apiFetch from '@wordpress/api-fetch';
import { getCategories, getEntityRecord, getEntityRecords } from '../resolvers';
import { receiveTerms, receiveEntityRecords, addEntities } from '../actions';

jest.mock( '@wordpress/api-fetch' );
jest.mock( '@wordpress/api-fetch', () => jest.fn() );

describe( 'getCategories', () => {
const CATEGORIES = [ { id: 1 } ];
Expand Down
2 changes: 2 additions & 0 deletions packages/editor/src/store/effects/test/reusable-blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import {
} from '../../actions';
import reducer from '../../reducer';

jest.mock( '@wordpress/api-fetch', () => jest.fn() );

describe( 'reusable blocks effects', () => {
beforeAll( () => {
registerBlockType( 'core/test-block', {
Expand Down

0 comments on commit d903fe8

Please sign in to comment.