Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions libs/ngrx-toolkit/src/lib/with-mutations.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,31 @@ function createTestSetup(flatteningOperator = concatOp) {
}

describe('withMutations with rxMutation', () => {
// fix: assign properties of mutations in withMutation https://github.com/angular-architects/ngrx-toolkit/pull/288
it("does not lose the runtime properties of a mutation's direct reference when assigning it to a component field", fakeAsync(() => {
const testSetup = createTestSetup();
const store = testSetup.store;

// Simulates `readonly myMutation = this.store.increment` in a component.
const myMutation = store.increment;

expect(myMutation.status()).toEqual('idle');
expect(typeof myMutation.isPending).toEqual('function');
expect(myMutation.isPending()).toEqual(false);

myMutation(2);
expect(myMutation.status()).toEqual('pending');
expect(myMutation.isPending()).toEqual(true);

tick(2000);

expect(myMutation.status()).toEqual('success');
expect(myMutation.isPending()).toEqual(false);
expect(myMutation.error()).toEqual(undefined);

expect(store.counter()).toEqual(7);
}));

it('should update the state', fakeAsync(() => {
const testSetup = createTestSetup();
const store = testSetup.store;
Expand Down
12 changes: 8 additions & 4 deletions libs/ngrx-toolkit/src/lib/with-mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,18 @@ function createMutationsFeature<Result extends MutationsDictionary>(
keys.reduce(
(acc, key) => ({
...acc,
[key]: async (params: never) => {
[key]: (() => {
const mutation = mutations[key];
if (!mutation) {
throw new Error(`Mutation ${key} not found`);
}
const result = await mutation(params);
return result;
},

return Object.assign(async (params: never) => {
const result = await mutation(params);

return result;
}, mutation);
})(),
}),
{} as MethodsDictionary,
),
Expand Down
Loading