Skip to content

Commit

Permalink
Fixed starting pos bugs + added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Cankyre committed Apr 19, 2024
1 parent 4e20392 commit 0b52727
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/state/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
68 changes: 68 additions & 0 deletions src/utils/tests/store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down

0 comments on commit 0b52727

Please sign in to comment.