Skip to content

Commit

Permalink
fix(patches): fix patches issue
Browse files Browse the repository at this point in the history
  • Loading branch information
unadlib committed Mar 24, 2024
1 parent 08e79b3 commit 823ccb5
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export const useTravel = <S, F extends boolean, A extends boolean>(
inversePatches: allPatches.inversePatches.concat(),
};
mergedPatches.patches.push(tempPatches.patches.flat());
mergedPatches.inversePatches.push(tempPatches.inversePatches.flat());
mergedPatches.inversePatches.push(tempPatches.inversePatches.flat().reverse());
}
return mergedPatches;
}, [allPatches, tempPatches, shouldArchive]);
Expand Down Expand Up @@ -290,7 +290,7 @@ export const useTravel = <S, F extends boolean, A extends boolean>(
}
return cachedHistory;
},
patches: allPatches,
patches: shouldArchive ? _allPatches : allPatches,
back: (amount = 1) => {
go(position - amount);
},
Expand Down
179 changes: 174 additions & 5 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,9 @@ describe('useTravel', () => {
//@ts-expect-error
act(() => controls.archive());
[nextState, setState, controls] = result.current;
expect(fnWarning).toHaveBeenCalledWith(`Auto archive is enabled, no need to archive manually`);
expect(fnWarning).toHaveBeenCalledWith(
`Auto archive is enabled, no need to archive manually`
);
});

it('[useTravel] with normal init state and disable autoArchive', () => {
Expand Down Expand Up @@ -450,7 +452,7 @@ describe('useTravel', () => {
},
],
});
expect(controls.patches.patches.length).toBe(0);
expect(controls.patches.patches.length).toBe(1);
expect(controls.position).toBe(1);
expect(controls.getHistory()).toEqual([
{ todos: [] },
Expand All @@ -475,7 +477,7 @@ describe('useTravel', () => {
);
[nextState, setState, controls] = result.current;

expect(controls.patches.patches.length).toBe(0);
expect(controls.patches.patches.length).toBe(1);
expect(controls.position).toBe(1);
expect(controls.getHistory()).toEqual([
{ todos: [] },
Expand Down Expand Up @@ -1024,7 +1026,6 @@ describe('useTravel', () => {
expect(controls.position).toEqual(2);
expect(controls.getHistory()).toEqual([0, 1, 2]);


act(() => controls.archive());
[nextState, setState, controls] = result.current;
expect(nextState).toEqual(2);
Expand All @@ -1037,7 +1038,6 @@ describe('useTravel', () => {
expect(controls.position).toEqual(3);
expect(controls.getHistory()).toEqual([0, 1, 2, 3]);


act(() => controls.archive());
[nextState, setState, controls] = result.current;
expect(nextState).toEqual(3);
Expand Down Expand Up @@ -1196,6 +1196,56 @@ describe('useTravel', () => {
expect(controls.getHistory()).toEqual([3, 4, 5, 6]);
expect(controls.canBack()).toBe(true);
expect(controls.canForward()).toBe(true);
expect(controls.patches).toMatchInlineSnapshot(`
{
"inversePatches": [
[
{
"op": "replace",
"path": [],
"value": 3,
},
],
[
{
"op": "replace",
"path": [],
"value": 4,
},
],
[
{
"op": "replace",
"path": [],
"value": 5,
},
],
],
"patches": [
[
{
"op": "replace",
"path": [],
"value": 4,
},
],
[
{
"op": "replace",
"path": [],
"value": 5,
},
],
[
{
"op": "replace",
"path": [],
"value": 6,
},
],
],
}
`);

act(() => setState(() => 7));
[nextState, setState, controls] = result.current;
Expand All @@ -1205,5 +1255,124 @@ describe('useTravel', () => {
expect(controls.getHistory()).toEqual([3, 4, 5, 7]);
expect(controls.canBack()).toBe(true);
expect(controls.canForward()).toBe(false);
expect(controls.patches).toMatchInlineSnapshot(`
{
"inversePatches": [
[
{
"op": "replace",
"path": [],
"value": 3,
},
],
[
{
"op": "replace",
"path": [],
"value": 4,
},
],
[
{
"op": "replace",
"path": [],
"value": 5,
},
],
],
"patches": [
[
{
"op": "replace",
"path": [],
"value": 4,
},
],
[
{
"op": "replace",
"path": [],
"value": 5,
},
],
[
{
"op": "replace",
"path": [],
"value": 7,
},
],
],
}
`);

act(() => setState(() => 8));
[nextState, setState, controls] = result.current;

expect(nextState).toEqual(8);
expect(controls.position).toEqual(3);
expect(controls.getHistory()).toEqual([3, 4, 5, 8]);
expect(controls.canBack()).toBe(true);
expect(controls.canForward()).toBe(false);
expect(controls.patches).toMatchInlineSnapshot(`
{
"inversePatches": [
[
{
"op": "replace",
"path": [],
"value": 3,
},
],
[
{
"op": "replace",
"path": [],
"value": 4,
},
],
[
{
"op": "replace",
"path": [],
"value": 7,
},
{
"op": "replace",
"path": [],
"value": 5,
},
],
],
"patches": [
[
{
"op": "replace",
"path": [],
"value": 4,
},
],
[
{
"op": "replace",
"path": [],
"value": 5,
},
],
[
{
"op": "replace",
"path": [],
"value": 7,
},
{
"op": "replace",
"path": [],
"value": 8,
},
],
],
}
`);
});
});

0 comments on commit 823ccb5

Please sign in to comment.