Skip to content

Commit 769e2ed

Browse files
Merge pull request #70 from AlbertoRota/auth-module-do-not-register-custom-getters-#69
Solved #69 by registering the custom getters.
2 parents f873413 + c593e6d commit 769e2ed

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

src/auth-module/auth-module.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import setupState from './state'
2+
import setupGetters from './getters'
23
import setupMutations from './mutations'
34
import setupActions from './actions'
45

@@ -24,6 +25,7 @@ export default function authPluginInit (feathersClient) {
2425
}
2526

2627
const defaultState = setupState(options)
28+
const defaultGetters = setupGetters()
2729
const defaultMutations = setupMutations(feathersClient)
2830
const defaultActions = setupActions(feathersClient)
2931

@@ -33,6 +35,7 @@ export default function authPluginInit (feathersClient) {
3335
store.registerModule(namespace, {
3436
namespaced: true,
3537
state: Object.assign({}, defaultState, options.state),
38+
getters: Object.assign({}, defaultGetters, options.getters),
3639
mutations: Object.assign({}, defaultMutations, options.mutations),
3740
actions: Object.assign({}, defaultActions, options.actions)
3841
})

src/auth-module/getters.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default function makeAuthGetters () {
2+
return {
3+
// Auth specific "Getters" will go here
4+
}
5+
}

test/auth-module/auth-module.test.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,81 @@ describe('Auth Module', () => {
3636
assert(store.state.authentication, 'the custom namespace was used')
3737
})
3838
})
39+
40+
describe('Customizing Auth Store', function () {
41+
it('allows adding custom state', function () {
42+
const customState = {
43+
test: true,
44+
test2: {
45+
test: true
46+
}
47+
}
48+
const store = new Vuex.Store({
49+
plugins: [
50+
auth({ state: customState })
51+
]
52+
})
53+
54+
assert(store.state.auth.test === true, 'added custom state')
55+
assert(store.state.auth.test2.test === true, 'added custom state')
56+
})
57+
58+
it('allows custom mutations', function () {
59+
const state = { test: true }
60+
const customMutations = {
61+
setTestToFalse (state) {
62+
state.test = false
63+
}
64+
}
65+
const store = new Vuex.Store({
66+
plugins: [
67+
auth({ state, mutations: customMutations })
68+
]
69+
})
70+
71+
store.commit('auth/setTestToFalse')
72+
assert(store.state.auth.test === false, 'the custom state was modified by the custom mutation')
73+
})
74+
75+
it('allows custom getters', function () {
76+
const customGetters = {
77+
oneTwoThree (state) {
78+
return 123
79+
}
80+
}
81+
const store = new Vuex.Store({
82+
plugins: [
83+
auth({ getters: customGetters })
84+
]
85+
})
86+
87+
assert(store.getters['auth/oneTwoThree'] === 123, 'the custom getter was available')
88+
})
89+
90+
it('allows adding custom actions', function () {
91+
const config = {
92+
state: {
93+
isTrue: false
94+
},
95+
mutations: {
96+
setToTrue (state) {
97+
state.isTrue = true
98+
}
99+
},
100+
actions: {
101+
trigger (context) {
102+
context.commit('setToTrue')
103+
}
104+
}
105+
}
106+
const store = new Vuex.Store({
107+
plugins: [
108+
auth(config)
109+
]
110+
})
111+
112+
store.dispatch('auth/trigger')
113+
assert(store.state.auth.isTrue === true, 'the custom action was run')
114+
})
115+
})
39116
})

0 commit comments

Comments
 (0)