Add unit tests for notes store

This commit is contained in:
Allen Snook 2018-10-16 15:21:34 -07:00
parent bd8fd2c9ec
commit b0be112277
3 changed files with 214 additions and 0 deletions

View File

@ -0,0 +1,80 @@
/**
* @format
*/
/**
* External dependencies
*/
import deepFreeze from 'deep-freeze';
/**
* Internal dependencies
*/
import { ERROR } from 'store/constants';
import notesReducer from '../reducer';
import { getJsonString } from 'store/utils';
describe( 'notesReducer()', () => {
it( 'returns an empty data object by default', () => {
const state = notesReducer( undefined, {} );
expect( state ).toEqual( {} );
} );
it( 'returns with received notes data', () => {
const originalState = deepFreeze( {} );
const query = {
page: 2,
};
const notes = [ { id: 1214 }, { id: 1215 }, { id: 1216 } ];
const state = notesReducer( originalState, {
type: 'SET_NOTES',
query,
notes,
} );
const queryKey = getJsonString( query );
expect( state[ queryKey ] ).toEqual( notes );
} );
it( 'tracks multiple queries in notes data', () => {
const otherQuery = {
page: 3,
};
const otherQueryKey = getJsonString( otherQuery );
const otherNotes = [ { id: 1 }, { id: 2 }, { id: 3 } ];
const otherQueryState = {
[ otherQueryKey ]: otherNotes,
};
const originalState = deepFreeze( otherQueryState );
const query = {
page: 2,
};
const notes = [ { id: 1214 }, { id: 1215 }, { id: 1216 } ];
const state = notesReducer( originalState, {
type: 'SET_NOTES',
query,
notes,
} );
const queryKey = getJsonString( query );
expect( state[ queryKey ] ).toEqual( notes );
expect( state[ otherQueryKey ] ).toEqual( otherNotes );
} );
it( 'returns with received error data', () => {
const originalState = deepFreeze( {} );
const query = {
page: 2,
};
const state = notesReducer( originalState, {
type: 'SET_NOTES_ERROR',
query,
} );
const queryKey = getJsonString( query );
expect( state[ queryKey ] ).toEqual( ERROR );
} );
} );

View File

@ -0,0 +1,42 @@
/*
* @format
*/
/**
* External dependencies
*/
import apiFetch from '@wordpress/api-fetch';
/**
* Internal dependencies
*/
import resolvers from '../resolvers';
const { getNotes } = resolvers;
jest.mock( '@wordpress/api-fetch', () => jest.fn() );
describe( 'getNotes', () => {
const NOTES_1 = [ { id: 1214 }, { id: 1215 }, { id: 1216 } ];
const NOTES_2 = [ { id: 1 }, { id: 2 }, { id: 3 } ];
beforeAll( () => {
apiFetch.mockImplementation( options => {
if ( options.path === '/wc/v3/admin/notes' ) {
return Promise.resolve( NOTES_1 );
}
if ( options.path === '/wc/v3/admin/notes/&page=2' ) {
return Promise.resolve( NOTES_2 );
}
} );
} );
it( 'returns requested data', async () => {
getNotes().then( data => expect( data ).toEqual( NOTES_1 ) );
} );
it( 'returns requested data for a specific query', async () => {
getNotes( { page: 2 } ).then( data => expect( data ).toEqual( NOTES_2 ) );
} );
} );

View File

@ -0,0 +1,92 @@
/*
* @format
*/
/**
* External dependencies
*/
import deepFreeze from 'deep-freeze';
/**
* Internal dependencies
*/
import { ERROR } from 'store/constants';
import selectors from '../selectors';
import { select } from '@wordpress/data';
import { getJsonString } from 'store/utils';
const { getNotes, isGetNotesRequesting, isGetNotesError } = selectors;
jest.mock( '@wordpress/data', () => ( {
...require.requireActual( '@wordpress/data' ),
select: jest.fn().mockReturnValue( {} ),
} ) );
const query = { page: 1 };
const queryKey = getJsonString( query );
describe( 'getNotes()', () => {
it( 'returns an empty array when no notes are available', () => {
const state = deepFreeze( {} );
expect( getNotes( state, query ) ).toEqual( [] );
} );
it( 'returns stored notes for current query', () => {
const notes = [ { id: 1214 }, { id: 1215 }, { id: 1216 } ];
const state = deepFreeze( {
notes: {
[ queryKey ]: notes,
},
} );
expect( getNotes( state, query ) ).toEqual( notes );
} );
} );
describe( 'isGetNotesRequesting()', () => {
beforeAll( () => {
select( 'core/data' ).isResolving = jest.fn().mockReturnValue( false );
} );
afterAll( () => {
select( 'core/data' ).isResolving.mockRestore();
} );
function setIsResolving( isResolving ) {
select( 'core/data' ).isResolving.mockImplementation(
( reducerKey, selectorName ) =>
isResolving && reducerKey === 'wc-admin' && selectorName === 'getNotes'
);
}
it( 'returns false if never requested', () => {
const result = isGetNotesRequesting( query );
expect( result ).toBe( false );
} );
it( 'returns false if request finished', () => {
setIsResolving( false );
const result = isGetNotesRequesting( query );
expect( result ).toBe( false );
} );
it( 'returns true if requesting', () => {
setIsResolving( true );
const result = isGetNotesRequesting( query );
expect( result ).toBe( true );
} );
} );
describe( 'isGetNotesError()', () => {
it( 'returns false by default', () => {
const state = deepFreeze( {} );
expect( isGetNotesError( state, query ) ).toEqual( false );
} );
it( 'returns true if ERROR constant is found', () => {
const state = deepFreeze( {
notes: {
[ queryKey ]: ERROR,
},
} );
expect( isGetNotesError( state, query ) ).toEqual( true );
} );
} );