Skip to content

Commit

Permalink
test(test): update testing
Browse files Browse the repository at this point in the history
  • Loading branch information
unadlib committed Aug 21, 2024
1 parent b54b01b commit 3c3dbf3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ export { apply } from './apply';
* Transactional updates to the base state with the recipe.
*/
export const mutate = <T>(baseState: T, recipe: (state: T) => void) => {
const [, patches] = create(baseState, recipe, {
const [, patches, inversePatches] = create(baseState, recipe, {
enablePatches: true,
});
apply(baseState, patches);
return patches;
return { inversePatches, patches };
};
22 changes: 20 additions & 2 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
import { mutate } from '../src';
import { mutate, apply } from '../src';

test('base - mutate', () => {
const baseState = {
a: {
c: 1,
},
};
mutate(baseState, (draft) => {
const { patches, inversePatches } = mutate(baseState, (draft) => {
draft.a.c = 2;
});
expect(baseState).toEqual({ a: { c: 2 } });
expect({ patches, inversePatches }).toEqual({
patches: [
{
op: 'replace',
path: ['a', 'c'],
value: 2,
},
],
inversePatches: [
{
op: 'replace',
path: ['a', 'c'],
value: 1,
},
],
});
apply(baseState, inversePatches);
expect(baseState).toEqual({ a: { c: 1 } });
});

test('base - mutate with error', () => {
Expand Down

0 comments on commit 3c3dbf3

Please sign in to comment.