From e215a427d70e323395103123fb3f1ec8a4732183 Mon Sep 17 00:00:00 2001 From: sundowndev Date: Tue, 29 Oct 2019 23:27:23 +0100 Subject: [PATCH] test(client): schools store --- client/tests/unit/store/schools.spec.ts | 66 +++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 client/tests/unit/store/schools.spec.ts diff --git a/client/tests/unit/store/schools.spec.ts b/client/tests/unit/store/schools.spec.ts new file mode 100644 index 0000000..8be088b --- /dev/null +++ b/client/tests/unit/store/schools.spec.ts @@ -0,0 +1,66 @@ +import { MutationTree, GetterTree, ActionTree } from 'vuex'; +import schools, { IStoreSchools } from '../../../src/store/modules/schools'; +import axios from 'axios'; +import config from '@/config'; + +const getters = schools.getters as GetterTree; +const mutations = schools.mutations as MutationTree; +const actions = schools.actions as any; + +let state: IStoreSchools; + +describe('Store - Questions', () => { + beforeEach(() => { + state = { + schools: [], + }; + }); + + describe('Getters', () => { + describe('#schools', () => { + it('should get schools', () => { + state.schools = [{ id: '1', name: 'test' }, { id: '2', name: 'test2' }]; + + const data = getters.schools(state, null, null, null); + + expect(data).toStrictEqual(state.schools); + expect(data.length).toBe(2); + }); + }); + }); + + describe('Mutations', () => { + describe('#setSchools', () => { + it('should set schools state', () => { + mutations.setSchools(state, [ + { id: '1', name: 'test' }, + { id: '2', name: 'test2' }, + ]); + + expect(state.schools.length).toEqual(2); + expect(typeof state.schools[0].id).toEqual('string'); + expect(typeof state.schools[0].name).toEqual('string'); + }); + }); + }); + + describe('Actions', () => { + describe('#fetchSchools', () => { + it('should fetch schools from API', async () => { + const payload = [{ id: '2', name: 'test' }]; + const axiosMock = jest.spyOn(axios, 'get').mockResolvedValue({ + data: { schools: payload }, + } as any); + const commitMock = jest.fn(); + + await actions.fetchSchools({ commit: commitMock }); + + expect(axiosMock).toBeCalledTimes(1); + expect(axiosMock).toHaveBeenCalledWith(`${config.apiUrl}/schools`); + + expect(commitMock).toBeCalledTimes(1); + expect(commitMock).toHaveBeenCalledWith('setSchools', payload); + }); + }); + }); +});