diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d14e385a3..f9f98748e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -212,7 +212,7 @@ jobs: report_name: Coverage Frontend type: lcov result_path: ./coverage/lcov.info - min_coverage: 13 + min_coverage: 15 token: ${{ github.token }} #test: diff --git a/frontend/src/store/store.js b/frontend/src/store/store.js index 0b0941401..5c9e1bc1e 100644 --- a/frontend/src/store/store.js +++ b/frontend/src/store/store.js @@ -3,6 +3,30 @@ import Vuex from 'vuex' Vue.use(Vuex) import createPersistedState from 'vuex-persistedstate' +export const mutations = { + language: (state, language) => { + state.language = language + }, + email: (state, email) => { + state.email = email + }, + session_id: (state, session_id) => { + state.session_id = session_id + }, +} + +export const actions = { + login: ({ dispatch, commit }, data) => { + commit('session_id', data.session_id) + commit('email', data.email) + }, + logout: ({ commit, state }) => { + commit('session_id', null) + commit('email', null) + sessionStorage.clear() + }, +} + export const store = new Vuex.Store({ plugins: [ createPersistedState({ @@ -13,49 +37,10 @@ export const store = new Vuex.Store({ session_id: null, email: '', language: 'en', - user: { - name: '', - balance: 0, - balance_gdt: 0, - }, modals: false, }, getters: {}, // Syncronous mutation of the state - mutations: { - language: (state, language) => { - state.language = language - }, - email: (state, email) => { - state.email = email - }, - session_id: (state, session_id) => { - state.session_id = session_id - }, - user_balance: (state, balance) => { - state.user.balance = balance / 10000 - }, - user_balance_gdt: (state, balance) => { - state.user.balance_gdt = balance / 10000 - }, - }, - actions: { - login: ({ dispatch, commit }, data) => { - commit('session_id', data.session_id) - commit('email', data.email) - }, - passwordReset: (data) => {}, - schoepfen: (data) => { - // http://localhost/transaction-creations/ajaxCreate - }, - createUser: ({ commit, dispatch }, data) => { - commit('session_id', data.session_id) - commit('email', data.email) - }, - logout: ({ commit, state }) => { - commit('session_id', null) - commit('email', null) - sessionStorage.clear() - }, - }, + mutations, + actions, }) diff --git a/frontend/src/store/store.test.js b/frontend/src/store/store.test.js new file mode 100644 index 000000000..ccf59dfbf --- /dev/null +++ b/frontend/src/store/store.test.js @@ -0,0 +1,81 @@ +import { mutations, actions } from './store' + +const { language, email, session_id } = mutations +const { login, logout } = actions + +describe('Vuex store', () => { + describe('mutations', () => { + describe('language', () => { + it('sets the state of language', () => { + const state = { language: 'en' } + language(state, 'de') + expect(state.language).toEqual('de') + }) + }) + + describe('email', () => { + it('sets the state of email', () => { + const state = { email: 'nobody@knows.tv' } + email(state, 'someone@there.is') + expect(state.email).toEqual('someone@there.is') + }) + }) + + describe('session_id', () => { + it('sets the state of session_id', () => { + const state = { session_id: null } + session_id(state, '1234') + expect(state.session_id).toEqual('1234') + }) + }) + }) + + describe('actions', () => { + describe('login', () => { + const commit = jest.fn() + const state = {} + + it('calls two commits', () => { + login({ commit, state }, { session_id: 1234, email: 'someone@there.is' }) + expect(commit).toHaveBeenCalledTimes(2) + }) + + it('commits session_id', () => { + login({ commit, state }, { session_id: 1234, email: 'someone@there.is' }) + expect(commit).toHaveBeenNthCalledWith(1, 'session_id', 1234) + }) + + it('commits email', () => { + login({ commit, state }, { session_id: 1234, email: 'someone@there.is' }) + expect(commit).toHaveBeenNthCalledWith(2, 'email', 'someone@there.is') + }) + }) + + describe('logout', () => { + const commit = jest.fn() + const state = {} + + it('calls two commits', () => { + logout({ commit, state }) + expect(commit).toHaveBeenCalledTimes(2) + }) + + it('commits session_id', () => { + logout({ commit, state }) + expect(commit).toHaveBeenNthCalledWith(1, 'session_id', null) + }) + + it('commits email', () => { + logout({ commit, state }) + expect(commit).toHaveBeenNthCalledWith(2, 'email', null) + }) + + // how can I get this working? + it.skip('calls sessionStorage.clear()', () => { + logout({ commit, state }) + const spy = jest.spyOn(sessionStorage, 'clear') + expect(spy).toHaveBeenCalledTimes(1) + }) + }) + }) +}) diff --git a/frontend/src/views/KontoOverview/GddStatus.spec.js b/frontend/src/views/KontoOverview/GddStatus.spec.js index c61b5f3ca..83b306ace 100644 --- a/frontend/src/views/KontoOverview/GddStatus.spec.js +++ b/frontend/src/views/KontoOverview/GddStatus.spec.js @@ -1,32 +1,22 @@ import { mount } from '@vue/test-utils' import GddStatus from './GddStatus' -import Vuex from 'vuex' const localVue = global.localVue describe('GddStatus', () => { let wrapper - let state = { - user: { - balance_gdt: 9876, - }, - } - - let store = new Vuex.Store({ - state, - }) - let mocks = { $n: jest.fn((n) => n), } let propsData = { balance: 1234, + GdtBalance: 9876, } const Wrapper = () => { - return mount(GddStatus, { localVue, store, mocks, propsData }) + return mount(GddStatus, { localVue, mocks, propsData }) } describe('mount', () => { diff --git a/frontend/src/views/KontoOverview/GddStatus.vue b/frontend/src/views/KontoOverview/GddStatus.vue index cc3ec880a..87a06dfb6 100644 --- a/frontend/src/views/KontoOverview/GddStatus.vue +++ b/frontend/src/views/KontoOverview/GddStatus.vue @@ -18,7 +18,7 @@ class="mb-4 h1" style="background-color: #ebebeba3 !important" > - {{ $n($store.state.user.balance_gdt) }} GDT + {{ $n(GdtBalance) }} GDT @@ -31,6 +31,7 @@ export default { props: { showTransactionList: { type: Boolean, default: true }, balance: { type: Number, default: 0 }, + GdtBalance: { type: Number, default: 0 }, }, } diff --git a/frontend/src/views/KontoOverview/GddTable.vue b/frontend/src/views/KontoOverview/GddTable.vue index 937a4a0e5..e8e1f3cd3 100644 --- a/frontend/src/views/KontoOverview/GddTable.vue +++ b/frontend/src/views/KontoOverview/GddTable.vue @@ -108,7 +108,6 @@ export default { const result = await communityAPI.transactions(this.$store.state.session_id) if (result.success) { - this.$store.state.user.balance_gdt = result.result.data.gdtSum this.items = result.result.data.transactions this.count = result.result.data.count } else { diff --git a/frontend/src/views/Layout/DashboardLayout_gdd.vue b/frontend/src/views/Layout/DashboardLayout_gdd.vue index 5fa3ed0e8..79270493a 100755 --- a/frontend/src/views/Layout/DashboardLayout_gdd.vue +++ b/frontend/src/views/Layout/DashboardLayout_gdd.vue @@ -28,7 +28,11 @@