Test store/auth#login

This commit is contained in:
Robert Schäfer 2018-12-18 15:03:46 +01:00
parent 01f5cfd481
commit d465db5906
2 changed files with 53 additions and 22 deletions

View File

@ -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 = {

View File

@ -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)
})
})
})
})