From 93ee1dbc2e8e2c62e3774a592106665d2117f62c Mon Sep 17 00:00:00 2001 From: Mike Drakoulelis Date: Sat, 29 Apr 2023 22:16:35 +0300 Subject: [PATCH] fix(data): make non-optimistic add command entity partial (#3859) --- .../spec/dispatchers/entity-dispatcher.spec.ts | 17 +++++++++++++++++ modules/data/src/dispatchers/entity-commands.ts | 14 +++++++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/modules/data/spec/dispatchers/entity-dispatcher.spec.ts b/modules/data/spec/dispatchers/entity-dispatcher.spec.ts index dd5fee919c..52a05af290 100644 --- a/modules/data/spec/dispatchers/entity-dispatcher.spec.ts +++ b/modules/data/spec/dispatchers/entity-dispatcher.spec.ts @@ -157,6 +157,23 @@ export function commandDispatchTest( expect(data).toBe(hero); }); + it('#add(hero) dispatches SAVE_ADD pessimistically with partial hero', () => { + const hero: Partial = { name: 'test' }; + dispatcher.add(hero); + const { entityOp, isOptimistic, data } = dispatchedAction().payload; + expect(entityOp).toBe(EntityOp.SAVE_ADD_ONE); + expect(isOptimistic).toBe(false); + expect(data).toBe(hero); + + testStore.dispatch.calls.reset(); + + dispatcher.add(hero, { isOptimistic: false }); + const specificallyPessimistic = dispatchedAction().payload; + expect(specificallyPessimistic.entityOp).toBe(EntityOp.SAVE_ADD_ONE); + expect(specificallyPessimistic.isOptimistic).toBe(false); + expect(specificallyPessimistic.data).toBe(hero); + }); + it('#delete(42) can dispatch SAVE_DELETE pessimistically for the id:42', () => { dispatcher.delete(42, { isOptimistic: false }); // optimistic by default const { entityOp, isOptimistic, data } = dispatchedAction().payload; diff --git a/modules/data/src/dispatchers/entity-commands.ts b/modules/data/src/dispatchers/entity-commands.ts index 4469fcd5b2..a031e4824b 100644 --- a/modules/data/src/dispatchers/entity-commands.ts +++ b/modules/data/src/dispatchers/entity-commands.ts @@ -11,6 +11,14 @@ export interface EntityServerCommands { * @returns A terminating Observable of the entity * after server reports successful save or the save error. */ + add( + entity: Partial, + options?: EntityActionOptions & { isOptimistic?: false } + ): Observable; + add( + entity: T, + options: EntityActionOptions & { isOptimistic: true } + ): Observable; add(entity: T, options?: EntityActionOptions): Observable; /** @@ -101,11 +109,11 @@ export interface EntityServerCommands { * after server reports successful query or the query error. * @see getWithQuery */ - loadWithQuery(queryParams: QueryParams | string, - options?: EntityActionOptions + loadWithQuery( + queryParams: QueryParams | string, + options?: EntityActionOptions ): Observable; - /** * Dispatch action to save the updated entity (or partial entity) in remote storage. * The update entity may be partial (but must have its key)