11import Vue from 'vue' ;
2- import Vuex , { Store } from 'vuex' ;
2+ import Vuex , { Store , MutationPayload } from 'vuex' ;
33// @ts -ignore
44import Storage from 'dom-storage' ;
55
@@ -8,8 +8,8 @@ const newJSON = require('flatted');
88import { VuexRefeshStorage } from '../src/index' ;
99
1010Vue . use ( Vuex )
11-
1211const storage = new Storage ( ) ;
12+ const objStorage : any = { }
1313
1414describe ( "test: options" , ( ) => {
1515 it ( "test: options custome" , ( ) => {
@@ -28,7 +28,7 @@ describe("test: options", () => {
2828 expect ( vuexRefeshStorage . overwrite ) . toBe ( true ) ;
2929 expect ( vuexRefeshStorage . key ) . toBe ( 'custumer' ) ;
3030 expect ( storage . getItem ( 'custumer' ) ) . toBe ( null ) ;
31- } )
31+ } ) ;
3232
3333 it ( "test: options.initStorage: false or true " , ( ) => {
3434 // options.initStorage: true
@@ -82,7 +82,7 @@ describe("test: options", () => {
8282 expect ( storeTwo . state ) . toEqual ( {
8383 normal : 'default'
8484 } )
85- } )
85+ } ) ;
8686
8787 it ( "test: modules" , ( ) => {
8888 storage . clear ( ) ;
@@ -110,7 +110,7 @@ describe("test: options", () => {
110110
111111 store . commit ( 'custom/changeState' ) ;
112112 expect ( storage . getItem ( 'custom' ) ) . toBe ( JSON . stringify ( { custom : { change : "state" } } ) )
113- } )
113+ } ) ;
114114
115115 it ( "test: flatted to should not clone circular objects" , ( ) => {
116116 storage . clear ( ) ;
@@ -137,7 +137,7 @@ describe("test: options", () => {
137137 vuexRefeshStorage . install ( store ) ;
138138 store . commit ( 'updateState' )
139139 expect ( storage . getItem ( 'vuex' ) ) . toBe ( newJSON . stringify ( { circularState : initState } ) )
140- } )
140+ } ) ;
141141
142142 it ( "should not persist whole store if modules array is empty" , ( ) => {
143143 storage . clear ( ) ;
@@ -162,7 +162,7 @@ describe("test: options", () => {
162162 vuexRefeshStorage . install ( store ) ;
163163 store . commit ( 'updateState' ) ;
164164 expect ( storage . getItem ( 'common' ) ) . toBe ( JSON . stringify ( { } ) )
165- } )
165+ } ) ;
166166
167167 it ( "should not persist whole store if modules array" , ( ) => {
168168 storage . clear ( ) ;
@@ -190,7 +190,7 @@ describe("test: options", () => {
190190 expect ( storage . getItem ( 'common' ) ) . toBe ( JSON . stringify ( { circularState : {
191191 name : '12312'
192192 } } ) )
193- } )
193+ } ) ;
194194
195195 it ( "should not persist null values" , ( ) => {
196196 storage . clear ( ) ;
@@ -214,7 +214,7 @@ describe("test: options", () => {
214214 orignal : '222' ,
215215 default : null
216216 } ) )
217- } )
217+ } ) ;
218218 it ( "should not merge array values when rehydrating by default" , ( ) => {
219219 storage . clear ( ) ;
220220
@@ -234,43 +234,111 @@ describe("test: options", () => {
234234 persisted : [ "state" , "json" ]
235235 } ) ;
236236 expect ( store . subscribe ) . toBeCalled ( )
237- } )
237+ } ) ;
238238
239- it ( "should apply a custom arrayMerger function" , ( ) => {
240- storage . clear ( ) ;
239+ it ( "should apply a custom arrayMerger function" , ( ) => {
240+ storage . clear ( ) ;
241241
242- storage . setItem ( "vuex" , JSON . stringify ( { persisted : [ 1 , 2 ] } ) ) ;
242+ storage . setItem ( "vuex" , JSON . stringify ( { persisted : [ 1 , 2 ] } ) ) ;
243243
244- const store = new Vuex . Store ( { state : { persisted : [ 1 , 2 , 3 ] } } ) ;
245- store . replaceState = jest . fn ( ) ;
246- store . subscribe = jest . fn ( ) ;
244+ const store = new Vuex . Store ( { state : { persisted : [ 1 , 2 , 3 ] } } ) ;
245+ store . replaceState = jest . fn ( ) ;
246+ store . subscribe = jest . fn ( ) ;
247+
248+ const vuexRefeshStorage = new VuexRefeshStorage ( {
249+ storage,
250+ deepMergeOptions : {
251+ arrayMerge : function ( store :any , saved :any ) {
252+ return [ "hello!" ] ;
253+ }
254+ }
255+ } ) ;
256+ vuexRefeshStorage . install ( store )
257+ expect ( store . replaceState ) . toBeCalledWith ( {
258+ persisted : [ "hello!" ] ,
259+ } ) ;
260+ expect ( store . subscribe ) . toBeCalled ( ) ;
261+ } ) ;
262+
263+ // it("rehydrates store's state through the configured getter", () => {
264+ // storage.clear();
265+ // const store = new Vuex.Store({
266+ // state: {}
267+ // });
268+ // store.replaceState = jest.fn();
269+ // const vuexRefeshStorage = new VuexRefeshStorage({
270+ // storage,
271+ // getState: (key, storage) => () => ({ getter: "item" })
272+ // });
273+ // vuexRefeshStorage.install(store);
274+ // expect(store.replaceState).toBeCalledWith({ getter: "item" });
275+ // })
276+ it ( "persist the changed state back through the configured setter" , ( ) => {
277+ expect . assertions ( 1 ) ;
278+ storage . clear ( ) ;
247279
280+ const store = new Vuex . Store ( {
281+ state : {
282+ setter : ''
283+ } ,
284+ mutations : {
285+ setCommit ( state ) {
286+ state . setter = "item"
287+ }
288+ }
289+ } ) ;
248290 const vuexRefeshStorage = new VuexRefeshStorage ( {
249291 storage,
250- deepMergeOptions : {
251- arrayMerge : function ( store :any , saved :any ) {
252- return [ "hello!" ] ;
292+ setState : ( key , state ) => {
293+ expect ( state ) . toEqual ( { setter : "item" } )
294+ }
295+ } ) ;
296+
297+ vuexRefeshStorage . install ( store ) ;
298+ store . commit ( 'setCommit' )
299+ } ) ;
300+
301+ it ( "filters to specific mutations" , ( ) => {
302+ storage . clear ( ) ;
303+ const store = new Vuex . Store ( {
304+ state : {
305+ normal : ''
306+ } ,
307+ mutations : {
308+ defaultCommit ( state , newVal ) {
309+ state . normal = newVal
310+ } ,
311+ filter ( state , newVal ) {
312+ state . normal = newVal
253313 }
254314 }
255315 } ) ;
256- vuexRefeshStorage . install ( store )
257- expect ( store . replaceState ) . toBeCalledWith ( {
258- persisted : [ "hello!" ] ,
316+ const vuexRefeshStorage = new VuexRefeshStorage ( {
317+ storage ,
318+ filter : ( mutation ) => mutation . type === 'filter'
259319 } ) ;
260- expect ( store . subscribe ) . toBeCalled ( ) ;
261- } )
320+ vuexRefeshStorage . install ( store ) ;
321+ store . commit ( 'defaultCommit' , 'state' ) ;
322+ expect ( storage . getItem ( 'vuex' ) ) . toBeNull ( )
323+ store . commit ( 'filter' , 'state' ) ;
324+ expect ( storage . getItem ( 'vuex' ) ) . toBe ( JSON . stringify ( { normal : 'state' } ) )
325+ } )
262326
263- it ( "rehydrates store's state through the configured getter" , ( ) => {
264- storage . clear ( ) ;
265- const store = new Vuex . Store ( {
266- state : { }
267- } ) ;
268- store . replaceState = jest . fn ( ) ;
269- const vuexRefeshStorage = new VuexRefeshStorage ( {
270- storage,
271- getState : ( ) => ( { getter : "item" } )
272- } ) ;
273- vuexRefeshStorage . install ( store ) ;
274- expect ( store . replaceState ) . toBeCalledWith ( { getter : "item" } ) ;
275- } )
327+ it ( "should call rehydrated callback once the state is replaced" , ( ) => {
328+ storage . clear ( ) ;
329+
330+ storage . setItem ( 'vuex' , JSON . stringify ( { persisted : "json" } ) ) ;
331+ const store = new Vuex . Store ( {
332+ state : { original : "state" }
333+ } ) ;
334+ const initAfterFunction = jest . fn ( ) ;
335+
336+ const vuexRefeshStorage = new VuexRefeshStorage ( {
337+ storage,
338+ initAfterFunction
339+ } ) ;
340+ vuexRefeshStorage . install ( store ) ;
341+
342+ expect ( initAfterFunction ) . toBeCalledWith ( store ) ;
343+ } ) ;
276344} )
0 commit comments