@@ -23,152 +23,157 @@ describe('Vuex', () => {
23
23
expect ( store . state . a ) . to . equal ( 3 )
24
24
} )
25
25
26
- it ( 'simple action' , function ( ) {
26
+ it ( 'injecting state and action to components ' , function ( ) {
27
27
const store = new Vuex . Store ( {
28
28
state : {
29
29
a : 1
30
30
} ,
31
- actions : {
32
- test : TEST
33
- } ,
34
- getters : {
35
- getA ( state ) {
36
- return state . a
37
- }
38
- } ,
39
31
mutations : {
40
32
[ TEST ] ( state , n ) {
41
33
state . a += n
42
34
}
43
35
}
44
36
} )
45
- store . actions . test ( 2 )
37
+ const vm = new Vue ( {
38
+ store,
39
+ vuex : {
40
+ state : {
41
+ a : state => state . a
42
+ } ,
43
+ actions : {
44
+ test : ( { dispatch } , n ) => dispatch ( TEST , n )
45
+ }
46
+ }
47
+ } )
48
+ vm . test ( 2 )
49
+ expect ( vm . a ) . to . equal ( 3 )
46
50
expect ( store . state . a ) . to . equal ( 3 )
47
- expect ( store . getters . getA ( ) ) . to . equal ( 3 )
48
51
} )
49
52
50
- it ( 'async action' , function ( done ) {
51
- const TEST = 'TEST'
53
+ it ( 'modules' , function ( ) {
54
+ const mutations = {
55
+ [ TEST ] ( state , n ) {
56
+ state . a += n
57
+ }
58
+ }
52
59
const store = new Vuex . Store ( {
53
60
state : {
54
- a : 1 ,
55
- timeout : 10
56
- } ,
57
- actions : {
58
- test : ( { dispatch, state } , n ) => {
59
- setTimeout ( ( ) => {
60
- dispatch ( TEST , n )
61
- } , state . timeout )
62
- }
61
+ a : 1
63
62
} ,
64
- mutations : {
65
- [ TEST ] ( state , n ) {
66
- state . a += n
63
+ mutations,
64
+ modules : {
65
+ one : {
66
+ state : { a : 2 } ,
67
+ mutations
68
+ } ,
69
+ two : {
70
+ state : { a : 3 } ,
71
+ mutations
67
72
}
68
73
}
69
74
} )
70
- store . actions . test ( 2 )
71
- setTimeout ( ( ) => {
72
- expect ( store . state . a ) . to . equal ( 3 )
73
- done ( )
74
- } , store . state . timeout )
75
+ store . dispatch ( TEST , 1 )
76
+ expect ( store . state . a ) . to . equal ( 2 )
77
+ expect ( store . state . one . a ) . to . equal ( 3 )
78
+ expect ( store . state . two . a ) . to . equal ( 4 )
75
79
} )
76
80
77
- it ( 'array option syntax' , function ( ) {
78
- const TEST2 = 'TEST2'
81
+ it ( 'hot reload' , function ( ) {
82
+ const mutations = {
83
+ [ TEST ] ( state , n ) {
84
+ state . a += n
85
+ }
86
+ }
79
87
const store = new Vuex . Store ( {
80
88
state : {
81
- a : 1 ,
82
- b : 1 ,
83
- c : 1
89
+ a : 1
84
90
} ,
85
- actions : [ { test : TEST } , { test2 : TEST2 } ] ,
86
- mutations : [
87
- {
88
- [ TEST ] ( state , n ) {
89
- state . a += n
90
- }
91
+ mutations,
92
+ modules : {
93
+ one : {
94
+ state : { a : 2 } ,
95
+ mutations
91
96
} ,
92
- // allow multiple handlers for the same mutation type
93
- {
94
- [ TEST ] ( state , n ) {
95
- state . b += n
96
- } ,
97
- [ TEST2 ] ( state , n ) {
98
- state . c += n
99
- }
100
- }
101
- ] ,
102
- getters : [
103
- {
104
- getA ( state ) {
105
- return state . a
106
- }
107
- } ,
108
- {
109
- getB ( state ) {
110
- return state . b
111
- } ,
112
-
113
- getC ( state ) {
114
- return state . c
115
- }
97
+ two : {
98
+ state : { a : 3 } ,
99
+ mutations
116
100
}
117
- ]
101
+ }
118
102
} )
119
- store . actions . test ( 2 )
120
- expect ( store . state . a ) . to . equal ( 3 )
121
- expect ( store . state . b ) . to . equal ( 3 )
122
- expect ( store . state . c ) . to . equal ( 1 )
123
- expect ( store . getters . getA ( ) ) . to . equal ( 3 )
124
- expect ( store . getters . getB ( ) ) . to . equal ( 3 )
125
- expect ( store . getters . getC ( ) ) . to . equal ( 1 )
126
- store . actions . test2 ( 2 )
127
- expect ( store . state . c ) . to . equal ( 3 )
128
- } )
103
+ store . dispatch ( TEST , 1 )
104
+ expect ( store . state . a ) . to . equal ( 2 )
105
+ expect ( store . state . one . a ) . to . equal ( 3 )
106
+ expect ( store . state . two . a ) . to . equal ( 4 )
129
107
130
- it ( 'hot reload' , function ( ) {
131
- const store = new Vuex . Store ( {
132
- state : {
133
- a : 1 ,
134
- b : 2
135
- } ,
136
- actions : {
137
- test : TEST
138
- } ,
108
+ // hot reload only root mutations
109
+ store . hotUpdate ( {
139
110
mutations : {
140
111
[ TEST ] ( state , n ) {
141
- state . a + = n
112
+ state . a = n
142
113
}
143
- } ,
144
- getters : {
145
- getA ( state ) {
146
- return state . b
114
+ }
115
+ } )
116
+ store . dispatch ( TEST , 1 )
117
+ expect ( store . state . a ) . to . equal ( 1 ) // only root mutation updated
118
+ expect ( store . state . one . a ) . to . equal ( 4 )
119
+ expect ( store . state . two . a ) . to . equal ( 5 )
120
+
121
+ // hot reload modules
122
+ store . hotUpdate ( {
123
+ modules : {
124
+ one : {
125
+ state : { a : 234 } ,
126
+ mutations : {
127
+ [ TEST ] ( state , n ) {
128
+ state . a += n
129
+ }
130
+ }
131
+ } ,
132
+ two : {
133
+ state : { a : 345 } ,
134
+ mutations : {
135
+ [ TEST ] ( state , n ) {
136
+ state . a -= n
137
+ }
138
+ }
147
139
}
148
140
}
149
141
} )
150
- const test = store . actions . test
151
- test ( 2 )
152
- expect ( store . state . a ) . to . equal ( 3 )
153
- expect ( store . getters . getA ( ) ) . to . equal ( 2 )
142
+ store . dispatch ( TEST , 2 )
143
+ expect ( store . state . a ) . to . equal ( 2 )
144
+ expect ( store . state . one . a ) . to . equal ( 6 ) // should not reload initial state
145
+ expect ( store . state . two . a ) . to . equal ( 3 ) // should not reload initial state
146
+
147
+ // hot reload all
154
148
store . hotUpdate ( {
155
- actions : {
156
- test : ( { dispatch } , n ) => dispatch ( TEST , n + 1 )
157
- } ,
158
149
mutations : {
159
150
[ TEST ] ( state , n ) {
160
- state . a = n
151
+ state . a - = n
161
152
}
162
153
} ,
163
- getters : {
164
- getA ( state ) {
165
- return state . a
154
+ modules : {
155
+ one : {
156
+ state : { a : 234 } ,
157
+ mutations : {
158
+ [ TEST ] ( state , n ) {
159
+ state . a = n
160
+ }
161
+ }
162
+ } ,
163
+ two : {
164
+ state : { a : 345 } ,
165
+ mutations : {
166
+ [ TEST ] ( state , n ) {
167
+ state . a = n
168
+ }
169
+ }
166
170
}
167
171
}
168
172
} )
169
- test ( 999 )
170
- expect ( store . state . a ) . to . equal ( 1000 )
171
- expect ( store . getters . getA ( ) ) . to . equal ( 1000 )
173
+ store . dispatch ( TEST , 3 )
174
+ expect ( store . state . a ) . to . equal ( - 1 )
175
+ expect ( store . state . one . a ) . to . equal ( 3 )
176
+ expect ( store . state . two . a ) . to . equal ( 3 )
172
177
} )
173
178
174
179
it ( 'middleware' , function ( ) {
0 commit comments