/
module-collection.spec.js
117 lines (107 loc) · 3.05 KB
/
module-collection.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import ModuleCollection from '@/module/module-collection'
describe('ModuleCollection', () => {
it('get', () => {
const collection = new ModuleCollection({
state: { value: 1 },
modules: {
a: {
state: { value: 2 }
},
b: {
state: { value: 3 },
modules: {
c: {
state: { value: 4 }
}
}
}
}
})
expect(collection.get([]).state.value).toBe(1)
expect(collection.get(['a']).state.value).toBe(2)
expect(collection.get(['b']).state.value).toBe(3)
expect(collection.get(['b', 'c']).state.value).toBe(4)
})
it('getNamespace', () => {
const module = (namespaced, children) => {
return {
namespaced,
modules: children
}
}
const collection = new ModuleCollection({
namespace: 'ignore/', // root module namespace should be ignored
modules: {
a: module(true, {
b: module(false, {
c: module(true)
}),
d: module(true)
})
}
})
const check = (path, expected) => {
const type = 'test'
const namespace = collection.getNamespace(path)
expect(namespace + type).toBe(expected)
}
check(['a'], 'a/test')
check(['a', 'b'], 'a/test')
check(['a', 'b', 'c'], 'a/c/test')
check(['a', 'd'], 'a/d/test')
})
it('register', () => {
const collection = new ModuleCollection({})
collection.register(['a'], {
state: { value: 1 }
})
collection.register(['b'], {
state: { value: 2 }
})
collection.register(['a', 'b'], {
state: { value: 3 }
})
expect(collection.get(['a']).state.value).toBe(1)
expect(collection.get(['b']).state.value).toBe(2)
expect(collection.get(['a', 'b']).state.value).toBe(3)
})
it('unregister', () => {
const collection = new ModuleCollection({})
collection.register(['a'], {
state: { value: true }
})
expect(collection.get(['a']).state.value).toBe(true)
collection.unregister(['a'])
expect(collection.get(['a'])).toBe(undefined)
})
it('does not unregister initial modules', () => {
const collection = new ModuleCollection({
modules: {
a: {
state: { value: true }
}
}
})
collection.unregister(['a'])
expect(collection.get(['a']).state.value).toBe(true)
})
it('warns when unregistering non existing module', () => {
const spy = jest.spyOn(console, 'warn').mockImplementation()
const collection = new ModuleCollection({})
collection.unregister(['a'])
expect(spy).toHaveBeenCalled()
})
it('isRegistered', () => {
const collection = new ModuleCollection({})
collection.register(['a'], {
state: { value: true }
})
collection.register(['a', 'b'], {
state: { value: false }
})
expect(collection.isRegistered(['a'])).toBe(true)
expect(collection.isRegistered(['a', 'b'])).toBe(true)
expect(collection.isRegistered(['c'])).toBe(false)
expect(collection.isRegistered(['c', 'd'])).toBe(false)
})
})