From d465db5906a52f7710a8e0eb0c9a6d55741c3535 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Sch=C3=A4fer?= Date: Tue, 18 Dec 2018 15:03:46 +0100 Subject: [PATCH] Test store/auth#login --- store/auth.js | 14 ----------- store/auth.test.js | 61 ++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 53 insertions(+), 22 deletions(-) diff --git a/store/auth.js b/store/auth.js index 6e8435c41..13ae2b65e 100644 --- a/store/auth.js +++ b/store/auth.js @@ -54,20 +54,6 @@ export const getters = { token(state) { return state.token } - // userSettings(state, getters, rootState, rootGetters) { - // const userSettings = (state.user && state.user.userSettings) ? state.user.userSettings : {} - // - // const defaultLanguage = (state.user && state.user.language) ? state.user.language : rootGetters['i18n/locale'] - // let contentLanguages = !isEmpty(userSettings.contentLanguages) ? userSettings.contentLanguages : [] - // if (isEmpty(contentLanguages)) { - // contentLanguages = userSettings.uiLanguage ? [userSettings.uiLanguage] : [defaultLanguage] - // } - // - // return Object.assign({ - // uiLanguage: defaultLanguage, - // contentLanguages: contentLanguages - // }, userSettings) - // } } export const actions = { diff --git a/store/auth.test.js b/store/auth.test.js index 9f53701bf..fd2663f83 100644 --- a/store/auth.test.js +++ b/store/auth.test.js @@ -1,15 +1,60 @@ import { getters, mutations, actions } from './auth.js' let state +let commit +const token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6InUxIiwic2x1ZyI6InBldGVyLWx1c3RpZyIsIm5hbWUiOiJQZXRlciBMdXN0aWciLCJhdmF0YXIiOiJodHRwczovL3MzLmFtYXpvbmF3cy5jb20vdWlmYWNlcy9mYWNlcy90d2l0dGVyL2FudG9ueXpvdG92LzEyOC5qcGciLCJlbWFpbCI6ImFkbWluQGV4YW1wbGUub3JnIiwicm9sZSI6ImFkbWluIiwiaWF0IjoxNTQ1MTQwMTQ5LCJleHAiOjE2MzE1NDAxNDksImF1ZCI6Imh0dHA6Ly9sb2NhbGhvc3Q6MzAwMCIsImlzcyI6Imh0dHA6Ly9sb2NhbGhvc3Q6NDAwMCIsInN1YiI6InUxIn0.t1nDgdRPNxXGbNzHHN6uSt5fmS4ofFNLjk_k5XnCoCs" -describe('isAuthenticated', () => { - describe('given JWT Bearer token', () => { - test('true', () => { - state = { - token: - 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6InUxIiwic2x1ZyI6InBldGVyLWx1c3RpZyIsIm5hbWUiOiJQZXRlciBMdXN0aWciLCJhdmF0YXIiOiJodHRwczovL3MzLmFtYXpvbmF3cy5jb20vdWlmYWNlcy9mYWNlcy90d2l0dGVyL25haXRhbmFtb3Jlbm8vMTI4LmpwZyIsImVtYWlsIjoiYWRtaW5AZXhhbXBsZS5vcmciLCJyb2xlIjoiYWRtaW4iLCJpYXQiOjE1NDUwNjMzODcsImV4cCI6MTYzMTQ2MzM4NywiYXVkIjoiaHR0cHM6Ly9uaXRyby1zdGFnaW5nLmh1bWFuLWNvbm5lY3Rpb24ub3JnIiwiaXNzIjoiaHR0cHM6Ly9hcGktbml0cm8tc3RhZ2luZy5odW1hbi1jb25uZWN0aW9uLm9yZyIsInN1YiI6InUxIn0.BQEoC3J6uRqMvIVfHYmMbmfMR2BudiG5Xvn8mfcc0Kk' - } - expect(getters.isAuthenticated(state)).toBe(true) +beforeEach(() => { + commit = jest.fn() +}) + +describe('getters', () => { + describe('isAuthenticated', () => { + describe('given JWT Bearer token', () => { + test('true', () => { + state = { token } + expect(getters.isAuthenticated(state)).toBe(true) + }) + }) + }) +}) + +describe('actions', () => { + let action + + describe('login', () => { + describe('given a successful response', () => { + let mutate + let onLogin + + beforeEach(() => { + mutate = jest.fn(() => Promise.resolve( { data: { login: { token } } })) + onLogin = jest.fn(() => Promise.resolve()) + const module = { + app: { + apolloProvider: { defaultClient: { mutate } }, + $apolloHelpers: { onLogin } + } + } + action = actions.login.bind(module) + }) + + afterEach(() => { + action = null + }) + + it('saves the JWT Bearer token', async () => { + await action({commit}, {email: 'doesnot@matter.org', password: '1234'}) + const expected = [ + ['SET_PENDING', true], + ['SET_USER', null], + ['SET_TOKEN', null], + ['SET_TOKEN', token], + ['SET_USER', { }], + ['SET_PENDING', false] + ] + expect(commit.mock.calls).toEqual(expected) + }) }) }) })