Skip to content

Commit

Permalink
fix(data): make non-optimistic add command entity partial (#3859)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nosfistis authored Apr 29, 2023
1 parent 07ba3fa commit 93ee1db
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
17 changes: 17 additions & 0 deletions modules/data/spec/dispatchers/entity-dispatcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,23 @@ export function commandDispatchTest(
expect(data).toBe(hero);
});

it('#add(hero) dispatches SAVE_ADD pessimistically with partial hero', () => {
const hero: Partial<Hero> = { 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;
Expand Down
14 changes: 11 additions & 3 deletions modules/data/src/dispatchers/entity-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ export interface EntityServerCommands<T> {
* @returns A terminating Observable of the entity
* after server reports successful save or the save error.
*/
add(
entity: Partial<T>,
options?: EntityActionOptions & { isOptimistic?: false }
): Observable<T>;
add(
entity: T,
options: EntityActionOptions & { isOptimistic: true }
): Observable<T>;
add(entity: T, options?: EntityActionOptions): Observable<T>;

/**
Expand Down Expand Up @@ -101,11 +109,11 @@ export interface EntityServerCommands<T> {
* after server reports successful query or the query error.
* @see getWithQuery
*/
loadWithQuery(queryParams: QueryParams | string,
options?: EntityActionOptions
loadWithQuery(
queryParams: QueryParams | string,
options?: EntityActionOptions
): Observable<T[]>;


/**
* Dispatch action to save the updated entity (or partial entity) in remote storage.
* The update entity may be partial (but must have its key)
Expand Down

0 comments on commit 93ee1db

Please sign in to comment.