Created
September 29, 2021 08:15
-
-
Save ispoljari/9f89c13f94449166d667bca40685565b to your computer and use it in GitHub Desktop.
Multiple solution related files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // auth.spec.ts | |
| import Vuex, { Store } from 'vuex'; | |
| import { createLocalVue } from '@vue/test-utils'; | |
| import AuthModule, { IState } from './auth'; | |
| jest.mock('@/services'); | |
| const localVue = createLocalVue(); | |
| localVue.use(Vuex); | |
| const storeOptions = { | |
| modules: { | |
| auth: AuthModule, | |
| }, | |
| }; | |
| const createStore = (storeOptions: any = {}): Store<{ auth: IState }> => new Vuex.Store({ ...storeOptions }); | |
| describe('AuthModule', () => { | |
| let store: Store<{ auth: IState }>; | |
| beforeEach(() => { | |
| store = createStore(storeOptions); | |
| }); | |
| describe('mutations', () => { | |
| // ... | |
| it('auth/setIsLoading', () => { | |
| expect(store.state.auth.isLoading).toBe(false); | |
| store.commit('auth/setIsLoading', true); | |
| expect(store.state.auth.isLoading).toBe(true); | |
| }); | |
| // ... | |
| }); | |
| describe('actions', () => { | |
| // ... | |
| it('register success', async () => { | |
| const registerData = { | |
| email: 'dummy@email.com', | |
| password: 'dummy', | |
| }; | |
| expect(store.state.auth.registrationInitiated).toBe(false); | |
| try { | |
| await store.dispatch('auth/register', registerData); | |
| expect(store.state.auth.registrationInitiated).toBe(true); | |
| } catch (error) {} | |
| }); | |
| // ... | |
| }); | |
| describe('mutation-actions', () => { | |
| // ... | |
| it('setEmail', async () => { | |
| const dummyEmail = 'dummy@email.com'; | |
| expect(store.state.auth.email).toBe(''); | |
| await store.dispatch('auth/setEmail', dummyEmail); | |
| expect(store.state.auth.email).toBe(dummyEmail); | |
| }); | |
| // ... | |
| }); | |
| describe('getters', () => { | |
| // ... | |
| it('auth/getError', () => { | |
| expect(store.state.auth.error).toBe(undefined); | |
| expect(store.getters['auth/getError']).toBe(undefined); | |
| (store.state.auth.error as any) = 'Demmo error'; | |
| expect(store.getters['auth/getError']).toBe('Demmo error'); | |
| }); | |
| // ... | |
| }); | |
| }); | |
| // services/auth | |
| export async function register(email: string, password: string, attr: any = {}): Promise<any> { | |
| try { | |
| return await Auth.signUp({ | |
| username: email, | |
| password, | |
| attributes: { | |
| ...attr, | |
| }, | |
| }); | |
| } catch (err: any) { | |
| return Promise.reject(createError(err, 'register')); | |
| } | |
| } | |
| // createError is just a util method for formatting the error message and wiring to the correct i18n label | |
| // services/__mock__/auth | |
| import { createError } from '../auth'; | |
| export const register = (registerData: { email: string; password: string }) => { | |
| try { | |
| if (!registerData) { | |
| throw new Error('dummy error'); | |
| } | |
| return new Promise((resolve) => resolve({ response: { user: registerData.email } })); | |
| } catch (err) { | |
| return Promise.reject(createError(err, 'register')); | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment