Files
driver.js/packages/driver/tests/keyboard.test.ts
Kamran Ahmed 75edd1b4bc Restructure as a Turborepo monorepo
packages/driver is the library (still published as driver.js, with
repository.directory set); playground and docs move to apps/. One root
pnpm workspace and lockfile replace the three standalone projects, with
turbo orchestrating build/test/lint:package (apps depend on the library
build; local + CI caching via .turbo).

Apps keep importing the library from source for HMR — the aliases now
point at ../../packages/driver/src and each app gains a workspace:*
dependency so the task graph knows the edge. The dev-server fs scope
widens to the repo root accordingly.

Releases run from packages/driver (root `pnpm release` delegates). The
changelog still lives in the docs app, so the release scripts reach it
via ../../../apps/docs and the after:bump hook stages it explicitly —
release-it no longer sits above both directories.

The merged workspace settings keep sharp unbuilt on purpose (prebuilt
binaries; building from source needs node-gyp and fails) — the docs app
had this right and the playground setting was the accident. postcss and
postcss-scss were unreferenced devDependencies and are dropped; prettier
moves to the root as the only shared tool.
2026-07-16 19:45:23 +01:00

109 lines
3.2 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import { createDriver, nextFrame, popoverTitle, pressKey, SAMPLE_STEPS, useDriverHarness } from "./utils";
useDriverHarness();
describe("keyboard control", () => {
it("closes the tour when Escape is pressed", () => {
const d = createDriver({ animate: false, steps: SAMPLE_STEPS });
d.drive();
pressKey("Escape");
expect(d.isActive()).toBe(false);
});
it("ignores Escape when allowClose is false", () => {
const d = createDriver({ animate: false, allowClose: false, steps: SAMPLE_STEPS });
d.drive();
pressKey("Escape");
expect(d.isActive()).toBe(true);
});
it("navigates with the arrow keys", async () => {
const d = createDriver({ animate: false, steps: SAMPLE_STEPS });
d.drive();
// Arrow handlers no-op mid-transition, so let the highlight settle first.
await nextFrame();
pressKey("ArrowRight");
await nextFrame();
expect(d.getActiveIndex()).toBe(1);
expect(popoverTitle()).toBe("Step 2");
pressKey("ArrowLeft");
await nextFrame();
expect(d.getActiveIndex()).toBe(0);
});
it("does not close the tour when ArrowLeft is pressed on the first step", async () => {
const d = createDriver({ animate: false, allowClose: false, steps: SAMPLE_STEPS });
d.drive();
await nextFrame();
pressKey("ArrowLeft");
await nextFrame();
expect(d.isActive()).toBe(true);
expect(d.getActiveIndex()).toBe(0);
});
it("runs onDoneClick when ArrowRight is pressed on the final step", async () => {
const onDoneClick = vi.fn();
const onNextClick = vi.fn();
const d = createDriver({ animate: false, steps: SAMPLE_STEPS, onDoneClick, onNextClick });
d.drive(SAMPLE_STEPS.length - 1);
await nextFrame();
pressKey("ArrowRight");
expect(onDoneClick).toHaveBeenCalledTimes(1);
expect(onNextClick).not.toHaveBeenCalled();
expect(d.isActive()).toBe(true);
});
it("ignores keys when allowKeyboardControl is false", () => {
const d = createDriver({ animate: false, allowKeyboardControl: false, steps: SAMPLE_STEPS });
d.drive();
pressKey("ArrowRight");
expect(d.getActiveIndex()).toBe(0);
pressKey("Escape");
expect(d.isActive()).toBe(true);
});
it("runs onPrevClick instead of moving back when ArrowLeft is pressed", async () => {
const onPrevClick = vi.fn();
const d = createDriver({ animate: false, steps: SAMPLE_STEPS, onPrevClick });
d.drive(1);
await nextFrame();
pressKey("ArrowLeft");
expect(onPrevClick).toHaveBeenCalledTimes(1);
expect(d.getActiveIndex()).toBe(1);
});
it("runs onNextClick instead of advancing when ArrowRight is pressed", async () => {
const onNextClick = vi.fn();
const d = createDriver({ animate: false, steps: SAMPLE_STEPS, onNextClick });
d.drive();
await nextFrame();
pressKey("ArrowRight");
expect(onNextClick).toHaveBeenCalledTimes(1);
expect(d.getActiveIndex()).toBe(0);
});
it("ignores arrow keys while a highlight transition is in flight", () => {
const d = createDriver({ animate: true, steps: SAMPLE_STEPS });
d.drive();
// No frame awaited: the transition callback is still set, so arrows no-op.
pressKey("ArrowRight");
expect(d.getActiveIndex()).toBe(0);
});
});