From 7d329a6a4ff3d7285e47082584cb9f29bf2e031d Mon Sep 17 00:00:00 2001 From: Jonathan Derrough Date: Thu, 17 Dec 2020 18:24:38 +0100 Subject: [PATCH 1/3] fix: rehydrating booleans at the root of the state --- src/lib/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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); } From 3fd2b0b37f1f5b22facd291cf5f244649fbde4e0 Mon Sep 17 00:00:00 2001 From: Jonathan Derrough Date: Thu, 17 Dec 2020 18:24:49 +0100 Subject: [PATCH 2/3] test: rehydrating booleans at the root of the state --- spec/index_spec.ts | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/spec/index_spec.ts b/spec/index_spec.ts index 3c37d7f..d54a20e 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,15 @@ 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 }; @@ -107,6 +108,8 @@ describe('ngrxLocalStorage', () => { const primitiveStr = 'string is not an object'; const initialStatePrimitiveStr = { state: primitiveStr }; + const primitiveBool = true; + const initialStatePrimitiveBool = { state: primitiveBool }; beforeEach(() => { localStorage.clear(); @@ -145,6 +148,19 @@ describe('ngrxLocalStorage', () => { expect(finalState.state).toEqual(primitiveStr); }); + it('simple boolean', () => { + 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 From 9cdc1c56911ec8cc2c582782f7e0a850166843db Mon Sep 17 00:00:00 2001 From: Benjamin Blackwood Date: Mon, 18 Jan 2021 09:08:52 +1100 Subject: [PATCH 3/3] test: rehydrating false booleans at the root of the state --- spec/index_spec.ts | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/spec/index_spec.ts b/spec/index_spec.ts index d54a20e..2b25146 100644 --- a/spec/index_spec.ts +++ b/spec/index_spec.ts @@ -94,8 +94,6 @@ describe('ngrxLocalStorage', () => { 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, aboolean: true }; let initialState = { state: t1 }; @@ -104,13 +102,6 @@ describe('ngrxLocalStorage', () => { let undefinedState = { state: undefined }; - let undefinedStateJson = JSON.stringify(undefinedState); - - const primitiveStr = 'string is not an object'; - const initialStatePrimitiveStr = { state: primitiveStr }; - const primitiveBool = true; - const initialStatePrimitiveBool = { state: primitiveBool }; - beforeEach(() => { localStorage.clear(); }); @@ -136,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; @@ -148,17 +142,22 @@ describe('ngrxLocalStorage', () => { expect(finalState.state).toEqual(primitiveStr); }); - it('simple boolean', () => { - const s = new MockStorage(); - const skr = mockStorageKeySerializer; + [true, false].forEach((bool) => { + it(`simple ${bool} boolean`, () => { + const primitiveBool = bool; + const initialStatePrimitiveBool = { state: primitiveBool }; - syncStateUpdate(initialStatePrimitiveBool, ['state'], s, skr, false); + const s = new MockStorage(); + const skr = mockStorageKeySerializer; - const raw = s.getItem('state'); - expect(JSON.parse(raw)).toEqual(primitiveBool); + syncStateUpdate(initialStatePrimitiveBool, ['state'], s, skr, false); - const finalState: any = rehydrateApplicationState(['state'], s, skr, true); - expect(finalState.state).toEqual(primitiveBool); + 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', () => {