Skip to content

Commit 93ee1db

Browse files
authored
fix(data): make non-optimistic add command entity partial (#3859)
1 parent 07ba3fa commit 93ee1db

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

modules/data/spec/dispatchers/entity-dispatcher.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,23 @@ export function commandDispatchTest(
157157
expect(data).toBe(hero);
158158
});
159159

160+
it('#add(hero) dispatches SAVE_ADD pessimistically with partial hero', () => {
161+
const hero: Partial<Hero> = { name: 'test' };
162+
dispatcher.add(hero);
163+
const { entityOp, isOptimistic, data } = dispatchedAction().payload;
164+
expect(entityOp).toBe(EntityOp.SAVE_ADD_ONE);
165+
expect(isOptimistic).toBe(false);
166+
expect(data).toBe(hero);
167+
168+
testStore.dispatch.calls.reset();
169+
170+
dispatcher.add(hero, { isOptimistic: false });
171+
const specificallyPessimistic = dispatchedAction().payload;
172+
expect(specificallyPessimistic.entityOp).toBe(EntityOp.SAVE_ADD_ONE);
173+
expect(specificallyPessimistic.isOptimistic).toBe(false);
174+
expect(specificallyPessimistic.data).toBe(hero);
175+
});
176+
160177
it('#delete(42) can dispatch SAVE_DELETE pessimistically for the id:42', () => {
161178
dispatcher.delete(42, { isOptimistic: false }); // optimistic by default
162179
const { entityOp, isOptimistic, data } = dispatchedAction().payload;

modules/data/src/dispatchers/entity-commands.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ export interface EntityServerCommands<T> {
1111
* @returns A terminating Observable of the entity
1212
* after server reports successful save or the save error.
1313
*/
14+
add(
15+
entity: Partial<T>,
16+
options?: EntityActionOptions & { isOptimistic?: false }
17+
): Observable<T>;
18+
add(
19+
entity: T,
20+
options: EntityActionOptions & { isOptimistic: true }
21+
): Observable<T>;
1422
add(entity: T, options?: EntityActionOptions): Observable<T>;
1523

1624
/**
@@ -101,11 +109,11 @@ export interface EntityServerCommands<T> {
101109
* after server reports successful query or the query error.
102110
* @see getWithQuery
103111
*/
104-
loadWithQuery(queryParams: QueryParams | string,
105-
options?: EntityActionOptions
112+
loadWithQuery(
113+
queryParams: QueryParams | string,
114+
options?: EntityActionOptions
106115
): Observable<T[]>;
107116

108-
109117
/**
110118
* Dispatch action to save the updated entity (or partial entity) in remote storage.
111119
* The update entity may be partial (but must have its key)

0 commit comments

Comments
 (0)