Testing Lab 7: Action Tests
Objectives
- Mock the API
- Mock the Store
- Test Success and Failure
Steps
Mock the API
- Create the directory - src\projects\__mocks__.- __mocks__begins and ends with two underscores and is case-sensitive
- Create the file - src\projects\__mocks__\projectAPI.tsto mock the- projectAPI.
- Mock the - getmethod.- src\projects\__mocks__\projectAPI.tsimport { MOCK_PROJECTS } from '../MockProjects';const projectAPI = {get(page = 1, limit = 20) {return Promise.resolve(MOCK_PROJECTS);},};export { projectAPI };
Mock the Store
- Open a - command prompt(Windows) or- terminal(Mac).
- Change the current directory to - working\keeptrack.
- Run one of the following sets of commands: - npmnpm install redux-mock-store @types/redux-mock-store --save-dev- Yarnyarn add redux-mock-store @types/redux-mock-store --save-dev
- Create the directory - src\projects\state\__tests__.- __tests__begins and ends with two underscores and is case-sensitive
- Create the file - src\projects\state\__tests__\projectActions-test.ts
- Add the test setup code including mocking the store away. - src\projects\state\__tests__\projectActions-test.tsimport configureMockStore from 'redux-mock-store';import ReduxThunk from 'redux-thunk';import { initialAppState } from '../../../state';import { loadProjects } from '../projectActions';import {LOAD_PROJECTS_REQUEST,LOAD_PROJECTS_SUCCESS,LOAD_PROJECTS_FAILURE,} from '../projectTypes';import { projectAPI } from '../../projectAPI';import { MOCK_PROJECTS } from '../../MockProjects';jest.mock('../../projectAPI');const middlewares = [ReduxThunk];const mockStoreCreator = configureMockStore(middlewares);describe('Project Actions', () => {let store: any;beforeEach(() => {store = mockStoreCreator(initialAppState);});});
Note that the
jest.mockreplaces the actual implementation of theprojectAPIwith the mock we created in the last step. This is done by convention in Jest which replaces the implementation with the exported function or object in the__mocks__directory.
- At this point, you will receive the following error: FAIL src/projects/state/__tests__/projectActions-test.ts● Test suite failed to runYour test suite must contain at least one test.
Test Success and Failure
- Attempt to test that the projects load successfully by adding the code.
src/projects/state/__tests__/projectActions-test.ts
- All tests including the - 'should load projects successfully'should pass.PASS src/projects/state/__tests__/projectActions-test.ts
- Test that an error is returned when loading projects fails. 
src/projects/state/__tests__/projectActions-test.ts
- The new test should pass. PASS src/projects/state/__tests__/projectActions-test.ts