File tree Expand file tree Collapse file tree 2 files changed +25
-3
lines changed Expand file tree Collapse file tree 2 files changed +25
-3
lines changed Original file line number Diff line number Diff line change @@ -11,11 +11,17 @@ export class Store {
11
11
assert ( typeof Promise !== 'undefined' , `vuex requires a Promise polyfill in this browser.` )
12
12
13
13
const {
14
- state = { } ,
15
14
plugins = [ ] ,
16
15
strict = false
17
16
} = options
18
17
18
+ let {
19
+ state = { }
20
+ } = options
21
+ if ( typeof state === 'function' ) {
22
+ state = state ( )
23
+ }
24
+
19
25
// store internal state
20
26
this . _committing = false
21
27
this . _actions = Object . create ( null )
Original file line number Diff line number Diff line change @@ -247,7 +247,7 @@ describe('Store', () => {
247
247
expect ( child . $store ) . toBe ( store )
248
248
} )
249
249
250
- it ( 'should warn silent option depreciation' , function ( ) {
250
+ it ( 'should warn silent option depreciation' , ( ) => {
251
251
spyOn ( console , 'warn' )
252
252
253
253
const store = new Vuex . Store ( {
@@ -263,7 +263,7 @@ describe('Store', () => {
263
263
)
264
264
} )
265
265
266
- it ( 'strict mode: warn mutations outside of handlers' , function ( ) {
266
+ it ( 'strict mode: warn mutations outside of handlers' , ( ) => {
267
267
const store = new Vuex . Store ( {
268
268
state : {
269
269
a : 1
@@ -333,4 +333,20 @@ describe('Store', () => {
333
333
} )
334
334
} )
335
335
} )
336
+
337
+ it ( 'should accept state as function' , ( ) => {
338
+ const store = new Vuex . Store ( {
339
+ state : ( ) => ( {
340
+ a : 1
341
+ } ) ,
342
+ mutations : {
343
+ [ TEST ] ( state , n ) {
344
+ state . a += n
345
+ }
346
+ }
347
+ } )
348
+ expect ( store . state . a ) . toBe ( 1 )
349
+ store . commit ( TEST , 2 )
350
+ expect ( store . state . a ) . toBe ( 3 )
351
+ } )
336
352
} )
You can’t perform that action at this time.
0 commit comments