mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
Merge pull request #270 from gradido/test-store
refactor: Test and Clean Up Store
This commit is contained in:
commit
1dad6dff06
2
.github/workflows/test.yml
vendored
2
.github/workflows/test.yml
vendored
@ -212,7 +212,7 @@ jobs:
|
|||||||
report_name: Coverage Frontend
|
report_name: Coverage Frontend
|
||||||
type: lcov
|
type: lcov
|
||||||
result_path: ./coverage/lcov.info
|
result_path: ./coverage/lcov.info
|
||||||
min_coverage: 13
|
min_coverage: 15
|
||||||
token: ${{ github.token }}
|
token: ${{ github.token }}
|
||||||
|
|
||||||
#test:
|
#test:
|
||||||
|
|||||||
@ -3,6 +3,30 @@ import Vuex from 'vuex'
|
|||||||
Vue.use(Vuex)
|
Vue.use(Vuex)
|
||||||
import createPersistedState from 'vuex-persistedstate'
|
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({
|
export const store = new Vuex.Store({
|
||||||
plugins: [
|
plugins: [
|
||||||
createPersistedState({
|
createPersistedState({
|
||||||
@ -13,49 +37,10 @@ export const store = new Vuex.Store({
|
|||||||
session_id: null,
|
session_id: null,
|
||||||
email: '',
|
email: '',
|
||||||
language: 'en',
|
language: 'en',
|
||||||
user: {
|
|
||||||
name: '',
|
|
||||||
balance: 0,
|
|
||||||
balance_gdt: 0,
|
|
||||||
},
|
|
||||||
modals: false,
|
modals: false,
|
||||||
},
|
},
|
||||||
getters: {},
|
getters: {},
|
||||||
// Syncronous mutation of the state
|
// Syncronous mutation of the state
|
||||||
mutations: {
|
mutations,
|
||||||
language: (state, language) => {
|
actions,
|
||||||
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()
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
|
|||||||
81
frontend/src/store/store.test.js
Normal file
81
frontend/src/store/store.test.js
Normal file
@ -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)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
@ -1,32 +1,22 @@
|
|||||||
import { mount } from '@vue/test-utils'
|
import { mount } from '@vue/test-utils'
|
||||||
import GddStatus from './GddStatus'
|
import GddStatus from './GddStatus'
|
||||||
import Vuex from 'vuex'
|
|
||||||
|
|
||||||
const localVue = global.localVue
|
const localVue = global.localVue
|
||||||
|
|
||||||
describe('GddStatus', () => {
|
describe('GddStatus', () => {
|
||||||
let wrapper
|
let wrapper
|
||||||
|
|
||||||
let state = {
|
|
||||||
user: {
|
|
||||||
balance_gdt: 9876,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
let store = new Vuex.Store({
|
|
||||||
state,
|
|
||||||
})
|
|
||||||
|
|
||||||
let mocks = {
|
let mocks = {
|
||||||
$n: jest.fn((n) => n),
|
$n: jest.fn((n) => n),
|
||||||
}
|
}
|
||||||
|
|
||||||
let propsData = {
|
let propsData = {
|
||||||
balance: 1234,
|
balance: 1234,
|
||||||
|
GdtBalance: 9876,
|
||||||
}
|
}
|
||||||
|
|
||||||
const Wrapper = () => {
|
const Wrapper = () => {
|
||||||
return mount(GddStatus, { localVue, store, mocks, propsData })
|
return mount(GddStatus, { localVue, mocks, propsData })
|
||||||
}
|
}
|
||||||
|
|
||||||
describe('mount', () => {
|
describe('mount', () => {
|
||||||
|
|||||||
@ -18,7 +18,7 @@
|
|||||||
class="mb-4 h1"
|
class="mb-4 h1"
|
||||||
style="background-color: #ebebeba3 !important"
|
style="background-color: #ebebeba3 !important"
|
||||||
>
|
>
|
||||||
{{ $n($store.state.user.balance_gdt) }} GDT
|
{{ $n(GdtBalance) }} GDT
|
||||||
</stats-card>
|
</stats-card>
|
||||||
</b-col>
|
</b-col>
|
||||||
</b-row>
|
</b-row>
|
||||||
@ -31,6 +31,7 @@ export default {
|
|||||||
props: {
|
props: {
|
||||||
showTransactionList: { type: Boolean, default: true },
|
showTransactionList: { type: Boolean, default: true },
|
||||||
balance: { type: Number, default: 0 },
|
balance: { type: Number, default: 0 },
|
||||||
|
GdtBalance: { type: Number, default: 0 },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@ -108,7 +108,6 @@ export default {
|
|||||||
const result = await communityAPI.transactions(this.$store.state.session_id)
|
const result = await communityAPI.transactions(this.$store.state.session_id)
|
||||||
|
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
this.$store.state.user.balance_gdt = result.result.data.gdtSum
|
|
||||||
this.items = result.result.data.transactions
|
this.items = result.result.data.transactions
|
||||||
this.count = result.result.data.count
|
this.count = result.result.data.count
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -28,7 +28,11 @@
|
|||||||
<div @click="$sidebar.displaySidebar(false)">
|
<div @click="$sidebar.displaySidebar(false)">
|
||||||
<fade-transition :duration="200" origin="center top" mode="out-in">
|
<fade-transition :duration="200" origin="center top" mode="out-in">
|
||||||
<!-- your content here -->
|
<!-- your content here -->
|
||||||
<router-view :balance="balance" @update-balance="updateBalance"></router-view>
|
<router-view
|
||||||
|
:balance="balance"
|
||||||
|
:gdt-balance="GdtBalance"
|
||||||
|
@update-balance="updateBalance"
|
||||||
|
></router-view>
|
||||||
</fade-transition>
|
</fade-transition>
|
||||||
</div>
|
</div>
|
||||||
<content-footer v-if="!$route.meta.hideFooter"></content-footer>
|
<content-footer v-if="!$route.meta.hideFooter"></content-footer>
|
||||||
@ -71,6 +75,7 @@ export default {
|
|||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
balance: 0,
|
balance: 0,
|
||||||
|
GdtBalance: 0,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
@ -94,6 +99,14 @@ export default {
|
|||||||
// what to do when loading balance fails?
|
// what to do when loading balance fails?
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
async loadGDTBalance() {
|
||||||
|
const result = await communityAPI.transactions(this.$store.state.session_id)
|
||||||
|
if (result.success) {
|
||||||
|
this.GdtBalance = result.result.data.gdtSum / 10000
|
||||||
|
} else {
|
||||||
|
// what to do when loading balance fails?
|
||||||
|
}
|
||||||
|
},
|
||||||
updateBalance(ammount) {
|
updateBalance(ammount) {
|
||||||
this.balance -= ammount
|
this.balance -= ammount
|
||||||
},
|
},
|
||||||
@ -103,6 +116,7 @@ export default {
|
|||||||
},
|
},
|
||||||
created() {
|
created() {
|
||||||
this.loadBalance()
|
this.loadBalance()
|
||||||
|
this.loadGDTBalance()
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@ -2,7 +2,11 @@
|
|||||||
<div>
|
<div>
|
||||||
<base-header class="pb-6 pb-8 pt-5 pt-md-8 bg-transparent"></base-header>
|
<base-header class="pb-6 pb-8 pt-5 pt-md-8 bg-transparent"></base-header>
|
||||||
<b-container fluid class="mt--7">
|
<b-container fluid class="mt--7">
|
||||||
<gdd-status :balance="balance" :show-transaction-list="showTransactionList" />
|
<gdd-status
|
||||||
|
:balance="balance"
|
||||||
|
:gdt-balance="GdtBalance"
|
||||||
|
:show-transaction-list="showTransactionList"
|
||||||
|
/>
|
||||||
<br />
|
<br />
|
||||||
<gdd-send
|
<gdd-send
|
||||||
:balance="balance"
|
:balance="balance"
|
||||||
@ -34,6 +38,7 @@ export default {
|
|||||||
},
|
},
|
||||||
props: {
|
props: {
|
||||||
balance: { type: Number, default: 0 },
|
balance: { type: Number, default: 0 },
|
||||||
|
GdtBalance: { type: Number, default: 0 },
|
||||||
},
|
},
|
||||||
components: {
|
components: {
|
||||||
GddStatus,
|
GddStatus,
|
||||||
|
|||||||
@ -174,7 +174,7 @@ export default {
|
|||||||
this.password,
|
this.password,
|
||||||
)
|
)
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
this.$store.dispatch('createUser', {
|
this.$store.dispatch('login', {
|
||||||
session_id: result.result.data.session_id,
|
session_id: result.result.data.session_id,
|
||||||
email: this.model.email,
|
email: this.model.email,
|
||||||
})
|
})
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user