Skip to content

Commit

Permalink
fix: prefix namespace to in-action call of dynamic nested module (#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktsn committed Aug 17, 2020
1 parent 482a83a commit cea46cc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export class Module<
this.namespace = namespace

const {
namespaced,
namespaced = true,
state,
getters,
mutations,
Expand All @@ -184,8 +184,7 @@ export class Module<
: Object.keys(modules).reduce(
(acc, key) => {
const m = modules[key]
const nextNamespaced =
m.options.namespaced === undefined ? true : m.options.namespaced
const nextNamespaced = m.options.namespaced ?? true

const nextNamespaceKey = nextNamespaced ? key + '/' : ''

Expand All @@ -211,7 +210,7 @@ export class Module<

return {
options: {
namespaced: namespaced === undefined ? true : namespaced,
namespaced,
state: () => (state ? new state() : {}),
getters: gettersInstance && gettersInstance.getters,
mutations: mutationsInstance && mutationsInstance.mutations,
Expand Down
21 changes: 21 additions & 0 deletions test/register.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,19 @@ class TestMutations extends Mutations<TestState> {
}
}

class TestActions extends Actions<TestState, never, TestMutations> {
inc() {
this.commit('inc')
}
}

let test: Module<TestState, never, TestMutations, any, any>

beforeEach(() => {
test = new Module({
state: TestState,
mutations: TestMutations,
actions: TestActions,
})
})

Expand All @@ -40,6 +47,20 @@ describe('registerModule', () => {
expect(store.state.test.count).toBe(1)
})

it('prefixes the namespace for calls via a nested module action', () => {
const store = new Vuex.Store<any>({})
const parent = new Module({
modules: {
test,
},
})
registerModule(store, 'parent', 'parent', parent)

expect(store.state.parent.test.count).toBe(0)
store.dispatch('parent/test/inc')
expect(store.state.parent.test.count).toBe(1)
})

it('passes module options of vuex', () => {
const store = new Vuex.Store<any>({})
store.replaceState({
Expand Down

0 comments on commit cea46cc

Please sign in to comment.