Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing: use simpler jest.fn() mocks for api-fetch calls in unit tests #8298

Merged
merged 1 commit into from
Jul 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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