Skip to content

Commit e3ae423

Browse files
lanzhihengktsn
authored andcommitted
Add some test and comments for module.js (#1113)
* Add some test and comments for module.js * Fix some feedback from test case
1 parent 479bd3e commit e3ae423

File tree

2 files changed

+153
-1
lines changed

2 files changed

+153
-1
lines changed

src/module/module.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import { forEachValue } from '../util'
22

3+
// Base data struct for store's module, package with some attribute and method
34
export default class Module {
45
constructor (rawModule, runtime) {
56
this.runtime = runtime
7+
// Store some children item
68
this._children = Object.create(null)
9+
// Store the origin module object which passed by programmer
710
this._rawModule = rawModule
811
const rawState = rawModule.state
12+
13+
// Store the origin module's state
914
this.state = (typeof rawState === 'function' ? rawState() : rawState) || {}
1015
}
1116

test/unit/module/module.spec.js

Lines changed: 148 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,156 @@ describe('Module', () => {
2121
})
2222

2323
it('get namespacer: namespace option is true', () => {
24-
const module = new Module({
24+
let module = new Module({
2525
namespaced: true
2626
})
2727
expect(module.namespaced).toBe(true)
28+
29+
module = new Module({
30+
namespaced: 100
31+
})
32+
expect(module.namespaced).toBe(true)
33+
})
34+
35+
it('add child method', () => {
36+
const module = new Module({})
37+
38+
module.addChild('v1', new Module({}))
39+
module.addChild('v2', new Module({}))
40+
expect(Object.keys(module._children)).toEqual(['v1', 'v2'])
41+
})
42+
43+
it('remove child method', () => {
44+
const module = new Module({})
45+
46+
module.addChild('v1', new Module({}))
47+
module.addChild('v2', new Module({}))
48+
expect(Object.keys(module._children)).toEqual(['v1', 'v2'])
49+
module.removeChild('v2')
50+
module.removeChild('abc')
51+
expect(Object.keys(module._children)).toEqual(['v1'])
52+
})
53+
54+
it('get child method', () => {
55+
const module = new Module({})
56+
57+
const subModule1 = new Module({ state: { name: 'v1' }})
58+
const subModule2 = new Module({ state: { name: 'v2' }})
59+
module.addChild('v1', subModule1)
60+
module.addChild('v2', subModule2)
61+
expect(module.getChild('v2')).toEqual(subModule2)
62+
expect(module.getChild('v1')).toEqual(subModule1)
63+
})
64+
65+
it('update method', () => {
66+
const originObject = {
67+
state: {
68+
name: 'vuex',
69+
version: '2.x.x'
70+
},
71+
namespaced: true,
72+
actions: {
73+
a1: () => {},
74+
a2: () => {}
75+
},
76+
mutations: {
77+
m1: () => {},
78+
m2: () => {}
79+
},
80+
getters: {
81+
g1: () => {},
82+
g2: () => {}
83+
}
84+
}
85+
const newObject = {
86+
actions: {
87+
a3: () => {},
88+
a4: () => {}
89+
},
90+
mutations: {
91+
m3: () => {},
92+
m2: () => {}
93+
},
94+
getters: {
95+
g1: () => {}
96+
},
97+
namespaced: false,
98+
state: {
99+
name: 'vuex',
100+
version: '3.x.x'
101+
}
102+
}
103+
const module = new Module(originObject)
104+
105+
expect(module._rawModule).toEqual(originObject)
106+
107+
module.update(newObject)
108+
expect(module._rawModule.actions).toEqual(newObject.actions)
109+
expect(module._rawModule.mutations).toEqual(newObject.mutations)
110+
expect(module._rawModule.getters).toEqual(newObject.getters)
111+
expect(module._rawModule.namespaced).toEqual(newObject.namespaced)
112+
expect(module._rawModule.state).toEqual(originObject.state)
113+
})
114+
115+
it('forEachChild method', () => {
116+
const module = new Module({})
117+
const module1 = new Module({})
118+
const module2 = new Module({})
119+
120+
module.addChild('v1', module1)
121+
module.addChild('v2', module2)
122+
123+
const collections = []
124+
module.forEachChild((item) => { collections.push(item) })
125+
expect(collections.length).toEqual(2)
126+
expect(collections).toEqual([module2, module1])
127+
})
128+
129+
it('forEachAction method', () => {
130+
const action1 = () => {}
131+
const action2 = () => {}
132+
133+
const module = new Module({
134+
actions: {
135+
action1, action2
136+
}
137+
})
138+
139+
const collections = []
140+
module.forEachAction((item) => { collections.push(item) })
141+
expect(collections.length).toEqual(2)
142+
expect(collections).toEqual([action1, action2])
143+
})
144+
145+
it('forEachGetter method', () => {
146+
const getter1 = () => {}
147+
const getter2 = () => {}
148+
149+
const module = new Module({
150+
getters: {
151+
getter1, getter2
152+
}
153+
})
154+
155+
const collections = []
156+
module.forEachGetter((item) => { collections.push(item) })
157+
expect(collections.length).toEqual(2)
158+
expect(collections).toEqual([getter1, getter2])
159+
})
160+
161+
it('forEachMutation method', () => {
162+
const mutation1 = () => {}
163+
const mutation2 = () => {}
164+
165+
const module = new Module({
166+
mutations: {
167+
mutation1, mutation2
168+
}
169+
})
170+
171+
const collections = []
172+
module.forEachMutation((item) => { collections.push(item) })
173+
expect(collections.length).toEqual(2)
174+
expect(collections).toEqual([mutation1, mutation2])
28175
})
29176
})

0 commit comments

Comments
 (0)