diff --git a/src/state/store.ts b/src/state/store.ts index 31b7929b..97bd8552 100644 --- a/src/state/store.ts +++ b/src/state/store.ts @@ -284,6 +284,8 @@ export const createTreeStore = (id?: string, initialTree?: TreeState) => { nextBranch: () => set( produce((state) => { + if (state.position.length === 0) return state; + let branchIndex = state.position[state.position.length - 1]; let branchCount = getNodeAtPath(state.root, state.position).children .length; @@ -302,6 +304,8 @@ export const createTreeStore = (id?: string, initialTree?: TreeState) => { previousBranch: () => set( produce((state) => { + if (state.position.length === 0) return state; + let branchIndex = state.position[state.position.length - 1]; let branchCount = getNodeAtPath(state.root, state.position).children .length; diff --git a/src/utils/tests/store.test.ts b/src/utils/tests/store.test.ts index 553be727..82c61855 100644 --- a/src/utils/tests/store.test.ts +++ b/src/utils/tests/store.test.ts @@ -248,6 +248,74 @@ test("should handle goToPrevious", () => { }); }); +test("should handle nextBranch", () => { + store.setState({ ...treeE4D5Nf3(), position: [] }); + store.getState().nextBranch(); + + expect(getNewState()).toStrictEqual({ + ...treeE4D5Nf3(), + position: [], + }); + + store.setState({ ...treeE4D5Nf3(), position: [0, 0] }); + + store.getState().nextBranch(); + expect(getNewState()).toStrictEqual({ + ...treeE4D5Nf3(), + position: [1], + }); + + store.getState().nextBranch(); + + expect(getNewState()).toStrictEqual({ + ...treeE4D5Nf3(), + position: [0], + }); +}); + +test("should handle previousBranch", () => { + store.setState({ ...treeE4D5Nf3(), position: [] }); + store.getState().previousBranch(); + + expect(getNewState()).toStrictEqual({ + ...treeE4D5Nf3(), + position: [], + }); + + store.setState({ ...treeE4D5Nf3(), position: [0, 0] }); + store.getState().previousBranch(); + expect(getNewState()).toStrictEqual({ + ...treeE4D5Nf3(), + position: [1], + }); + + store.getState().previousBranch(); + expect(getNewState()).toStrictEqual({ + ...treeE4D5Nf3(), + position: [0], + }); +}); + +test("should handle goToBranchEnd", () => { + store.setState({ ...treeE4D5(), position: [] }); + store.getState().goToBranchEnd(); + + expect(getNewState()).toStrictEqual({ + ...treeE4D5(), + position: [0, 0], + }); +}); + +test("should handle goToBranchStart", () => { + store.setState({ ...treeE4D5(), position: [0, 0] }); + store.getState().goToBranchStart(); + + expect(getNewState()).toStrictEqual({ + ...treeE4D5(), + position: [], + }); +}); + test("should handle goToMove", () => { store.setState({ ...treeE4D5(), position: [] }); store.getState().goToMove([0]);