waveterm/frontend/layout/tests/layoutTree.test.ts

85 lines
3.9 KiB
TypeScript
Raw Normal View History

// Copyright 2024, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
import { assert, test } from "vitest";
2024-08-27 01:37:05 +02:00
import { newLayoutNode } from "../lib/layoutNode";
import { computeMoveNode, moveNode } from "../lib/layoutTree";
2024-08-27 01:35:35 +02:00
import {
DropDirection,
LayoutTreeActionType,
LayoutTreeComputeMoveNodeAction,
LayoutTreeMoveNodeAction,
2024-08-27 01:37:05 +02:00
} from "../lib/types";
import { newLayoutTreeState } from "./model";
test("layoutTreeStateReducer - compute move", () => {
2024-08-27 01:35:35 +02:00
let treeState = newLayoutTreeState(newLayoutNode(undefined, undefined, undefined, { blockId: "root" }));
assert(treeState.rootNode.data!.blockId === "root", "root should have no children and should have data");
let node1 = newLayoutNode(undefined, undefined, undefined, { blockId: "node1" });
let pendingAction = computeMoveNode(treeState, {
type: LayoutTreeActionType.ComputeMove,
nodeId: treeState.rootNode.id,
nodeToMoveId: node1.id,
direction: DropDirection.Bottom,
2024-08-27 01:35:35 +02:00
});
const insertOperation = pendingAction as LayoutTreeMoveNodeAction;
assert(insertOperation.node === node1, "insert operation node should equal node1");
assert(!insertOperation.parentId, "insert operation parent should not be defined");
assert(insertOperation.index === 1, "insert operation index should equal 1");
assert(insertOperation.insertAtRoot, "insert operation insertAtRoot should be true");
2024-08-27 01:35:35 +02:00
moveNode(treeState, insertOperation);
assert(
treeState.rootNode.data === undefined && treeState.rootNode.children!.length === 2,
"root node should now have no data and should have two children"
);
2024-08-27 01:35:35 +02:00
assert(treeState.rootNode.children![1].data!.blockId === "node1", "root's second child should be node1");
2024-08-27 01:35:35 +02:00
let node2 = newLayoutNode(undefined, undefined, undefined, { blockId: "node2" });
pendingAction = computeMoveNode(treeState, {
type: LayoutTreeActionType.ComputeMove,
nodeId: node1.id,
nodeToMoveId: node2.id,
direction: DropDirection.Bottom,
2024-08-27 01:35:35 +02:00
});
const insertOperation2 = pendingAction as LayoutTreeMoveNodeAction;
assert(insertOperation2.node === node2, "insert operation node should equal node2");
assert(insertOperation2.parentId === node1.id, "insert operation parent id should be node1 id");
assert(insertOperation2.index === 1, "insert operation index should equal 1");
assert(!insertOperation2.insertAtRoot, "insert operation insertAtRoot should be false");
2024-08-27 01:35:35 +02:00
moveNode(treeState, insertOperation2);
assert(
treeState.rootNode.data === undefined && treeState.rootNode.children!.length === 2,
"root node should still have three children"
);
assert(treeState.rootNode.children![1].children!.length === 2, "root's second child should now have two children");
});
2024-06-19 01:06:19 +02:00
test("computeMove - noop action", () => {
2024-08-27 01:35:35 +02:00
let nodeToMove = newLayoutNode(undefined, undefined, undefined, { blockId: "nodeToMove" });
let treeState = newLayoutTreeState(
2024-06-19 01:06:19 +02:00
newLayoutNode(undefined, undefined, [
nodeToMove,
2024-08-27 01:35:35 +02:00
newLayoutNode(undefined, undefined, undefined, { blockId: "otherNode" }),
2024-06-19 01:06:19 +02:00
])
);
2024-08-27 01:35:35 +02:00
let moveAction: LayoutTreeComputeMoveNodeAction = {
2024-06-19 01:06:19 +02:00
type: LayoutTreeActionType.ComputeMove,
nodeId: treeState.rootNode.id,
nodeToMoveId: nodeToMove.id,
2024-06-19 01:06:19 +02:00
direction: DropDirection.Left,
};
2024-08-27 01:35:35 +02:00
let pendingAction = computeMoveNode(treeState, moveAction);
assert(pendingAction === undefined, "inserting a node to the left of itself should not produce a pendingAction");
2024-06-19 01:06:19 +02:00
moveAction = {
type: LayoutTreeActionType.ComputeMove,
nodeId: treeState.rootNode.id,
nodeToMoveId: nodeToMove.id,
2024-06-19 01:06:19 +02:00
direction: DropDirection.Right,
};
2024-08-27 01:35:35 +02:00
pendingAction = computeMoveNode(treeState, moveAction);
assert(pendingAction === undefined, "inserting a node to the right of itself should not produce a pendingAction");
2024-06-19 01:06:19 +02:00
});