Choice rows in the dialogue node editor now have up/down buttons so a choice can be moved up or down into the desired order, which is the order shown to the player. The swap lives in a pure reorderChoice helper (no-op at the edges, input not mutated) with a focused test.
32 lines
1.4 KiB
TypeScript
32 lines
1.4 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import { reorderChoice } from "@/lib/npc-dialogue/reorder";
|
|
import type { DialogueNext } from "@/lib/npc-dialogue/dialogue";
|
|
|
|
/** Build a choice with a unique target so reordering is observable. */
|
|
const choice = (label: string): DialogueNext => ({ label, target: `node-${label}` });
|
|
|
|
describe("reorderChoice (editor choice ordering)", () => {
|
|
const choices = [choice("a"), choice("b"), choice("c")];
|
|
|
|
it("swaps a choice with its neighbour", () => {
|
|
expect(reorderChoice(choices, 0, 1)).toEqual([choice("b"), choice("a"), choice("c")]);
|
|
// direction -1 from index 1 lands on the same swap
|
|
expect(reorderChoice(choices, 1, -1)).toEqual([choice("b"), choice("a"), choice("c")]);
|
|
expect(reorderChoice(choices, 2, -1)).toEqual([choice("a"), choice("c"), choice("b")]);
|
|
});
|
|
|
|
it("returns null at the edges so nothing moves", () => {
|
|
expect(reorderChoice(choices, 0, -1)).toBeNull();
|
|
expect(reorderChoice(choices, 2, 1)).toBeNull();
|
|
// A single-item list is an edge in every direction.
|
|
expect(reorderChoice([choice("only")], 0, 1)).toBeNull();
|
|
expect(reorderChoice([choice("only")], 0, -1)).toBeNull();
|
|
});
|
|
|
|
it("does not mutate the input array", () => {
|
|
const moved = reorderChoice(choices, 1, 1);
|
|
expect(moved).not.toBe(choices);
|
|
expect(choices).toEqual([choice("a"), choice("b"), choice("c")]);
|
|
});
|
|
});
|