diff --git a/spec/index_spec.ts b/spec/index_spec.ts index 3c37d7f..2b25146 100644 --- a/spec/index_spec.ts +++ b/spec/index_spec.ts @@ -18,14 +18,14 @@ class TypeA { if (value.afield) { return new TypeB(value.afield); } else { - return new TypeA(value.astring, value.anumber, value.adate, value.aclass); + return new TypeA(value.astring, value.anumber, value.aboolean, value.adate, value.aclass); } } return dateReviver(key, value); } static replacer(key: string, value: any) { - if (key === 'anumber' || key === 'adate') { + if (key === 'anumber' || key === 'aboolean' || key === 'adate') { return undefined; } return value; @@ -36,12 +36,13 @@ class TypeA { } static deserialize(json: any): TypeA { - return new TypeA(json.astring, json.anumber, json.adate, new TypeB(json.aclass.afield)); + return new TypeA(json.astring, json.anumber, json.aboolean, json.adate, new TypeB(json.aclass.afield)); } constructor( public astring: string = undefined, public anumber: number = undefined, + public aboolean: boolean = undefined, public adate: Date = undefined, public aclass: TypeB = undefined ) {} @@ -87,15 +88,13 @@ function mockStorageKeySerializer(key) { } describe('ngrxLocalStorage', () => { - let t1 = new TypeA('Testing', 3.14159, new Date('1968-11-16T12:30:00Z'), new TypeB('Nested Class')); + let t1 = new TypeA('Testing', 3.14159, true, new Date('1968-11-16T12:30:00Z'), new TypeB('Nested Class')); let t1Json = JSON.stringify(t1); - let t1Filtered = new TypeA('Testing', undefined, undefined, new TypeB('Nested Class')); + let t1Filtered = new TypeA('Testing', undefined, undefined, undefined, new TypeB('Nested Class')); - let t1FilteredJson = JSON.stringify(t1Filtered); - - let t1Simple = { astring: 'Testing', adate: '1968-11-16T12:30:00.000Z', anumber: 3.14159 }; + let t1Simple = { astring: 'Testing', adate: '1968-11-16T12:30:00.000Z', anumber: 3.14159, aboolean: true }; let initialState = { state: t1 }; @@ -103,11 +102,6 @@ describe('ngrxLocalStorage', () => { let undefinedState = { state: undefined }; - let undefinedStateJson = JSON.stringify(undefinedState); - - const primitiveStr = 'string is not an object'; - const initialStatePrimitiveStr = { state: primitiveStr }; - beforeEach(() => { localStorage.clear(); }); @@ -133,6 +127,9 @@ describe('ngrxLocalStorage', () => { }); it('simple string', () => { + const primitiveStr = 'string is not an object'; + const initialStatePrimitiveStr = { state: primitiveStr }; + const s = new MockStorage(); const skr = mockStorageKeySerializer; @@ -145,6 +142,24 @@ describe('ngrxLocalStorage', () => { expect(finalState.state).toEqual(primitiveStr); }); + [true, false].forEach((bool) => { + it(`simple ${bool} boolean`, () => { + const primitiveBool = bool; + const initialStatePrimitiveBool = { state: primitiveBool }; + + const s = new MockStorage(); + const skr = mockStorageKeySerializer; + + syncStateUpdate(initialStatePrimitiveBool, ['state'], s, skr, false); + + const raw = s.getItem('state'); + expect(JSON.parse(raw)).toEqual(primitiveBool); + + const finalState: any = rehydrateApplicationState(['state'], s, skr, true); + expect(finalState.state).toEqual(primitiveBool); + }); + }); + it('filtered', () => { // Use the filter by field option to round-trip an object while // filtering out the anumber and adate filed diff --git a/src/lib/index.ts b/src/lib/index.ts index f4a8ca5..61e2724 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -86,7 +86,7 @@ export const rehydrateApplicationState = ( const isObjectRegex = new RegExp('{|\\['); let raw = stateSlice; - if (stateSlice === 'null' || isObjectRegex.test(stateSlice.charAt(0))) { + if (stateSlice === 'null' || stateSlice === 'true' || stateSlice === 'false' || isObjectRegex.test(stateSlice.charAt(0))) { raw = JSON.parse(stateSlice, reviver); }