From 70b0ad91e88258201b1dff0ed9f509d6236a6d8f Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Wed, 16 Dec 2020 13:57:29 +0300 Subject: [PATCH 1/4] refactor: renamed the patch method name to deepPatch #6667 --- .../packages/core/src/lib/utils/internal-store-utils.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/npm/ng-packs/packages/core/src/lib/utils/internal-store-utils.ts b/npm/ng-packs/packages/core/src/lib/utils/internal-store-utils.ts index 87bc3f81fd..cc7aa01ce5 100644 --- a/npm/ng-packs/packages/core/src/lib/utils/internal-store-utils.ts +++ b/npm/ng-packs/packages/core/src/lib/utils/internal-store-utils.ts @@ -25,7 +25,8 @@ export class InternalStore { constructor(private initialState: State) {} - patch(state: DeepPartial) { + + deepPatch(state: DeepPartial) { this.state$.next(deepMerge(this.state, state)); this.update$.next(state); } From 033fa8cb6c8129ff111389e59bd650ac5151a88d Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Wed, 16 Dec 2020 13:57:47 +0300 Subject: [PATCH 2/4] feat: add a new method named patch to InternalStore class --- .../core/src/lib/utils/internal-store-utils.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/npm/ng-packs/packages/core/src/lib/utils/internal-store-utils.ts b/npm/ng-packs/packages/core/src/lib/utils/internal-store-utils.ts index cc7aa01ce5..ea4af9ddfb 100644 --- a/npm/ng-packs/packages/core/src/lib/utils/internal-store-utils.ts +++ b/npm/ng-packs/packages/core/src/lib/utils/internal-store-utils.ts @@ -25,6 +25,16 @@ export class InternalStore { constructor(private initialState: State) {} + patch(state: Partial) { + let patchedState = state as State; + + if (typeof state === 'object' && !Array.isArray(state)) { + patchedState = { ...this.state, ...state }; + } + + this.state$.next(patchedState); + this.update$.next(patchedState); + } deepPatch(state: DeepPartial) { this.state$.next(deepMerge(this.state, state)); From 905e86cffb0ec54ca63c5c088d0836d3704f2a4d Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Wed, 16 Dec 2020 13:58:12 +0300 Subject: [PATCH 3/4] test: use deepPatch method instead of the patch --- .../core/src/lib/tests/internal-store.spec.ts | 47 +++++++++---------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/npm/ng-packs/packages/core/src/lib/tests/internal-store.spec.ts b/npm/ng-packs/packages/core/src/lib/tests/internal-store.spec.ts index e0eed44eb6..c3e530f99f 100644 --- a/npm/ng-packs/packages/core/src/lib/tests/internal-store.spec.ts +++ b/npm/ng-packs/packages/core/src/lib/tests/internal-store.spec.ts @@ -17,25 +17,25 @@ const mockInitialState = { type MockState = typeof mockInitialState; -const patch1: DeepPartial = { foo: { bar: { baz: [() => {}] } } }; +const deepPatch1: DeepPartial = { foo: { bar: { baz: [() => {}] } } }; const expected1: MockState = clone(mockInitialState); -expected1.foo.bar.baz = patch1.foo.bar.baz; +expected1.foo.bar.baz = deepPatch1.foo.bar.baz; -const patch2: DeepPartial = { foo: { bar: { qux: Promise.resolve() } } }; +const deepPatch2: DeepPartial = { foo: { bar: { qux: Promise.resolve() } } }; const expected2: MockState = clone(mockInitialState); -expected2.foo.bar.qux = patch2.foo.bar.qux; +expected2.foo.bar.qux = deepPatch2.foo.bar.qux; -const patch3: DeepPartial = { foo: { n: 1 } }; +const deepPatch3: DeepPartial = { foo: { n: 1 } }; const expected3: MockState = clone(mockInitialState); -expected3.foo.n = patch3.foo.n; +expected3.foo.n = deepPatch3.foo.n; -const patch4: DeepPartial = { x: 'X' }; +const deepPatch4: DeepPartial = { x: 'X' }; const expected4: MockState = clone(mockInitialState); -expected4.x = patch4.x; +expected4.x = deepPatch4.x; -const patch5: DeepPartial = { a: true }; +const deepPatch5: DeepPartial = { a: true }; const expected5: MockState = clone(mockInitialState); -expected5.a = patch5.a; +expected5.a = deepPatch5.a; describe('Internal Store', () => { describe('sliceState', () => { @@ -52,10 +52,7 @@ describe('Internal Store', () => { async ({ selector, expected }) => { const store = new InternalStore(mockInitialState); - const value = await store - .sliceState(selector) - .pipe(take(1)) - .toPromise(); + const value = await store.sliceState(selector).pipe(take(1)).toPromise(); expect(value).toEqual(expected); }, @@ -64,16 +61,16 @@ describe('Internal Store', () => { describe('patchState', () => { test.each` - patch | expected - ${patch1} | ${expected1} - ${patch2} | ${expected2} - ${patch3} | ${expected3} - ${patch4} | ${expected4} - ${patch5} | ${expected5} + patch | expected + ${deepPatch1} | ${expected1} + ${deepPatch2} | ${expected2} + ${deepPatch3} | ${expected3} + ${deepPatch4} | ${expected4} + ${deepPatch5} | ${expected5} `('should set state as $expected when patch is $patch', ({ patch, expected }) => { const store = new InternalStore(mockInitialState); - store.patch(patch); + store.deepPatch(patch); expect(store.state).toEqual(expected); }); @@ -86,12 +83,12 @@ describe('Internal Store', () => { const onQux$ = store.sliceUpdate(state => state.foo.bar.qux); onQux$.pipe(take(1)).subscribe(value => { - expect(value).toEqual(patch2.foo.bar.qux); + expect(value).toEqual(deepPatch2.foo.bar.qux); done(); }); - store.patch(patch1); - store.patch(patch2); + store.deepPatch(deepPatch1); + store.deepPatch(deepPatch2); }); }); @@ -99,7 +96,7 @@ describe('Internal Store', () => { it('should reset state to initialState', () => { const store = new InternalStore(mockInitialState); - store.patch(patch1); + store.deepPatch(deepPatch1); store.reset(); expect(store.state).toEqual(mockInitialState); From de32bb9c95c9290175b40f3721050110808df079 Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Wed, 16 Dec 2020 14:18:44 +0300 Subject: [PATCH 4/4] test: add new tests for patch method of the InternalStore #6667 --- .../core/src/lib/tests/internal-store.spec.ts | 73 +++++++++++++++---- 1 file changed, 57 insertions(+), 16 deletions(-) diff --git a/npm/ng-packs/packages/core/src/lib/tests/internal-store.spec.ts b/npm/ng-packs/packages/core/src/lib/tests/internal-store.spec.ts index c3e530f99f..c0728db069 100644 --- a/npm/ng-packs/packages/core/src/lib/tests/internal-store.spec.ts +++ b/npm/ng-packs/packages/core/src/lib/tests/internal-store.spec.ts @@ -18,24 +18,48 @@ const mockInitialState = { type MockState = typeof mockInitialState; const deepPatch1: DeepPartial = { foo: { bar: { baz: [() => {}] } } }; -const expected1: MockState = clone(mockInitialState); -expected1.foo.bar.baz = deepPatch1.foo.bar.baz; +const deepPatchExpected1: MockState = clone(mockInitialState); +deepPatchExpected1.foo.bar.baz = deepPatch1.foo.bar.baz; const deepPatch2: DeepPartial = { foo: { bar: { qux: Promise.resolve() } } }; -const expected2: MockState = clone(mockInitialState); -expected2.foo.bar.qux = deepPatch2.foo.bar.qux; +const deepPatchExpected2: MockState = clone(mockInitialState); +deepPatchExpected2.foo.bar.qux = deepPatch2.foo.bar.qux; const deepPatch3: DeepPartial = { foo: { n: 1 } }; -const expected3: MockState = clone(mockInitialState); -expected3.foo.n = deepPatch3.foo.n; +const deepPatchExpected3: MockState = clone(mockInitialState); +deepPatchExpected3.foo.n = deepPatch3.foo.n; const deepPatch4: DeepPartial = { x: 'X' }; -const expected4: MockState = clone(mockInitialState); -expected4.x = deepPatch4.x; +const deepPatchExpected4: MockState = clone(mockInitialState); +deepPatchExpected4.x = deepPatch4.x; const deepPatch5: DeepPartial = { a: true }; -const expected5: MockState = clone(mockInitialState); -expected5.a = deepPatch5.a; +const deepPatchExpected5: MockState = clone(mockInitialState); +deepPatchExpected5.a = deepPatch5.a; + +const patch1: Partial = { + foo: { bar: { baz: [() => {}] } } as typeof mockInitialState.foo, +}; +const patchExpected1: MockState = clone(mockInitialState); +patchExpected1.foo = patch1.foo; + +const patch2: Partial = { + foo: { bar: { qux: Promise.resolve() } } as typeof mockInitialState.foo, +}; +const patchExpected2: MockState = clone(mockInitialState); +patchExpected2.foo = patch2.foo; + +const patch3: Partial = { foo: { n: 1 } as typeof mockInitialState.foo }; +const patchExpected3: MockState = clone(mockInitialState); +patchExpected3.foo = patch3.foo; + +const patch4: Partial = { x: 'X' }; +const patchExpected4: MockState = clone(mockInitialState); +patchExpected4.x = patch4.x; + +const patch5: Partial = { a: true }; +const patchExpected5: MockState = clone(mockInitialState); +patchExpected5.a = patch5.a; describe('Internal Store', () => { describe('sliceState', () => { @@ -59,14 +83,14 @@ describe('Internal Store', () => { ); }); - describe('patchState', () => { + describe('deepPatchState', () => { test.each` patch | expected - ${deepPatch1} | ${expected1} - ${deepPatch2} | ${expected2} - ${deepPatch3} | ${expected3} - ${deepPatch4} | ${expected4} - ${deepPatch5} | ${expected5} + ${deepPatch1} | ${deepPatchExpected1} + ${deepPatch2} | ${deepPatchExpected2} + ${deepPatch3} | ${deepPatchExpected3} + ${deepPatch4} | ${deepPatchExpected4} + ${deepPatch5} | ${deepPatchExpected5} `('should set state as $expected when patch is $patch', ({ patch, expected }) => { const store = new InternalStore(mockInitialState); @@ -76,6 +100,23 @@ describe('Internal Store', () => { }); }); + describe('patchState', () => { + test.each` + patch | expected + ${patch1} | ${patchExpected1} + ${patch2} | ${patchExpected2} + ${patch3} | ${patchExpected3} + ${patch4} | ${patchExpected4} + ${patch5} | ${patchExpected5} + `('should set state as $expected when patch is $patch', ({ patch, expected }) => { + const store = new InternalStore(mockInitialState); + + store.patch(patch); + + expect(store.state).toEqual(expected); + }); + }); + describe('sliceUpdate', () => { it('should return slice of update$ based on selector', done => { const store = new InternalStore(mockInitialState);