newsletterState in store

This commit is contained in:
Moriz Wahl 2021-09-16 11:14:22 +02:00
parent 11384e4060
commit 8b16e772c1
2 changed files with 39 additions and 3 deletions

View File

@ -26,6 +26,9 @@ export const mutations = {
token: (state, token) => {
state.token = token
},
newsletterState: (state, newsletterState) => {
state.newsletterState = newsletterState
},
}
export const actions = {
@ -36,6 +39,7 @@ export const actions = {
commit('firstName', data.firstName)
commit('lastName', data.lastName)
commit('description', data.description)
commit('newsletterState', data.klickTipp.newsletterState)
},
logout: ({ commit, state }) => {
commit('token', null)
@ -44,6 +48,7 @@ export const actions = {
commit('firstName', '')
commit('lastName', '')
commit('description', '')
commit('newsletterState', null)
localStorage.clear()
},
}
@ -62,6 +67,7 @@ export const store = new Vuex.Store({
username: '',
description: '',
token: null,
newsletterState: null,
},
getters: {},
// Syncronous mutation of the state

View File

@ -1,6 +1,15 @@
import { mutations, actions } from './store'
const { language, email, token, username, firstName, lastName, description } = mutations
const {
language,
email,
token,
username,
firstName,
lastName,
description,
newsletterState,
} = mutations
const { login, logout } = actions
describe('Vuex store', () => {
@ -60,6 +69,14 @@ describe('Vuex store', () => {
expect(state.description).toEqual('Nickelbrille')
})
})
describe('newsletterState', () => {
it('sets the state of newsletterState', () => {
const state = { newsletterState: null }
newsletterState(state, true)
expect(state.newsletterState).toEqual(true)
})
})
})
describe('actions', () => {
@ -73,11 +90,14 @@ describe('Vuex store', () => {
firstName: 'Peter',
lastName: 'Lustig',
description: 'Nickelbrille',
klickTipp: {
newsletterState: true,
},
}
it('calls seven commits', () => {
login({ commit, state }, commitedData)
expect(commit).toHaveBeenCalledTimes(6)
expect(commit).toHaveBeenCalledTimes(7)
})
it('commits email', () => {
@ -109,6 +129,11 @@ describe('Vuex store', () => {
login({ commit, state }, commitedData)
expect(commit).toHaveBeenNthCalledWith(6, 'description', 'Nickelbrille')
})
it('commits newsletterState', () => {
login({ commit, state }, commitedData)
expect(commit).toHaveBeenNthCalledWith(7, 'newsletterState', true)
})
})
describe('logout', () => {
@ -117,7 +142,7 @@ describe('Vuex store', () => {
it('calls six commits', () => {
logout({ commit, state })
expect(commit).toHaveBeenCalledTimes(6)
expect(commit).toHaveBeenCalledTimes(7)
})
it('commits token', () => {
@ -150,6 +175,11 @@ describe('Vuex store', () => {
expect(commit).toHaveBeenNthCalledWith(6, 'description', '')
})
it('commits newsletterState', () => {
logout({ commit, state })
expect(commit).toHaveBeenNthCalledWith(7, 'newsletterState', null)
})
// how to get this working?
it.skip('calls localStorage.clear()', () => {
const clearStorageMock = jest.fn()