Skip to content

Commit a7a775b

Browse files
ktsnyyx990803
authored andcommitted
Add assertions of module assets
1 parent e0504cc commit a7a775b

File tree

2 files changed

+132
-14
lines changed

2 files changed

+132
-14
lines changed

src/module/module-collection.js

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
import Module from './module'
2-
import { forEachValue } from '../util'
2+
import { assert, forEachValue } from '../util'
33

44
export default class ModuleCollection {
55
constructor (rawRootModule) {
66
// register root module (Vuex.Store options)
7-
this.root = new Module(rawRootModule, false)
8-
9-
// register all nested modules
10-
if (rawRootModule.modules) {
11-
forEachValue(rawRootModule.modules, (rawModule, key) => {
12-
this.register([key], rawModule, false)
13-
})
14-
}
7+
this.register([], rawRootModule, false)
158
}
169

1710
get (path) {
@@ -29,13 +22,19 @@ export default class ModuleCollection {
2922
}
3023

3124
update (rawRootModule) {
32-
update(this.root, rawRootModule)
25+
update([], this.root, rawRootModule)
3326
}
3427

3528
register (path, rawModule, runtime = true) {
36-
const parent = this.get(path.slice(0, -1))
29+
assertRawModule(path, rawModule)
30+
3731
const newModule = new Module(rawModule, runtime)
38-
parent.addChild(path[path.length - 1], newModule)
32+
if (path.length === 0) {
33+
this.root = newModule
34+
} else {
35+
const parent = this.get(path.slice(0, -1))
36+
parent.addChild(path[path.length - 1], newModule)
37+
}
3938

4039
// register nested modules
4140
if (rawModule.modules) {
@@ -54,7 +53,9 @@ export default class ModuleCollection {
5453
}
5554
}
5655

57-
function update (targetModule, newModule) {
56+
function update (path, targetModule, newModule) {
57+
assertRawModule(path, newModule)
58+
5859
// update target module
5960
targetModule.update(newModule)
6061

@@ -68,7 +69,34 @@ function update (targetModule, newModule) {
6869
)
6970
return
7071
}
71-
update(targetModule.getChild(key), newModule.modules[key])
72+
update(
73+
path.concat(key),
74+
targetModule.getChild(key),
75+
newModule.modules[key]
76+
)
7277
}
7378
}
7479
}
80+
81+
function assertRawModule (path, rawModule) {
82+
['getters', 'actions', 'mutations'].forEach(key => {
83+
if (!rawModule[key]) return
84+
85+
forEachValue(rawModule[key], (value, type) => {
86+
assert(
87+
typeof value === 'function',
88+
makeAssertionMessage(path, key, type, value)
89+
)
90+
})
91+
})
92+
}
93+
94+
function makeAssertionMessage (path, key, type, value) {
95+
let buf = `${key} should be function but "${key}.${type}"`
96+
if (path.length > 0) {
97+
buf += ` in module "${path.join('.')}"`
98+
}
99+
buf += ` is ${JSON.stringify(value)}.`
100+
101+
return buf
102+
}

test/unit/modules.spec.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,4 +580,94 @@ describe('Modules', () => {
580580
expect(mutations[0].payload).toBe(2)
581581
})
582582
})
583+
584+
it('asserts a mutation should be a function', () => {
585+
expect(() => {
586+
new Vuex.Store({
587+
mutations: {
588+
test: null
589+
}
590+
})
591+
}).toThrowError(
592+
/mutations should be function but "mutations\.test" is null/
593+
)
594+
595+
expect(() => {
596+
new Vuex.Store({
597+
modules: {
598+
foo: {
599+
modules: {
600+
bar: {
601+
mutations: {
602+
test: 123
603+
}
604+
}
605+
}
606+
}
607+
}
608+
})
609+
}).toThrowError(
610+
/mutations should be function but "mutations\.test" in module "foo\.bar" is 123/
611+
)
612+
})
613+
614+
it('asserts an action should be a function', () => {
615+
expect(() => {
616+
new Vuex.Store({
617+
actions: {
618+
test: 'test'
619+
}
620+
})
621+
}).toThrowError(
622+
/actions should be function but "actions\.test" is "test"/
623+
)
624+
625+
expect(() => {
626+
new Vuex.Store({
627+
modules: {
628+
foo: {
629+
modules: {
630+
bar: {
631+
actions: {
632+
test: 'error'
633+
}
634+
}
635+
}
636+
}
637+
}
638+
})
639+
}).toThrowError(
640+
/actions should be function but "actions\.test" in module "foo\.bar" is "error"/
641+
)
642+
})
643+
644+
it('asserts a getter should be a function', () => {
645+
expect(() => {
646+
new Vuex.Store({
647+
getters: {
648+
test: undefined
649+
}
650+
})
651+
}).toThrowError(
652+
/getters should be function but "getters\.test" is undefined/
653+
)
654+
655+
expect(() => {
656+
new Vuex.Store({
657+
modules: {
658+
foo: {
659+
modules: {
660+
bar: {
661+
getters: {
662+
test: true
663+
}
664+
}
665+
}
666+
}
667+
}
668+
})
669+
}).toThrowError(
670+
/getters should be function but "getters\.test" in module "foo\.bar" is true/
671+
)
672+
})
583673
})

0 commit comments

Comments
 (0)