Files
driver.js/tests/lifecycle.test.ts
Kamran Ahmed 7d6de08569 Add a jsdom test suite for the public API
Replaces the placeholder test with focused, maintainable suites that
encode the documented API behaviour as a regression net for the
upcoming API migration:

- lifecycle: activate/highlight/destroy, active element, modal popovers
- navigation: drive, moveNext/movePrevious/moveTo, first/last, getters
- popover: buttons, progress text, custom class, onPopoverRender
- interactions: next/prev/close clicks and onNextClick/onPrevClick overrides
- keyboard: escape + arrow keys, allowClose/allowKeyboardControl
- hooks: onHighlightStarted/onHighlighted/onDeselected/onDestroyed
- config: getConfig/setConfig/setSteps/getState/refresh

Shared setup lives in tests/utils.ts. Also add test:run and
test:coverage scripts.
2026-06-23 09:48:04 +01:00

52 lines
2.0 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { createDriver, popoverDescription, popoverEl, popoverTitle, useDriverHarness } from "./utils";
useDriverHarness();
describe("lifecycle", () => {
it("is inactive before anything is highlighted", () => {
const d = createDriver();
expect(d.isActive()).toBe(false);
expect(popoverEl()).toBeNull();
});
it("activates and renders a popover when highlighting an element", () => {
const d = createDriver({ animate: false });
d.highlight({ element: "#intro", popover: { title: "Intro", description: "The intro paragraph" } });
expect(d.isActive()).toBe(true);
expect(document.body.classList.contains("driver-active")).toBe(true);
expect(popoverTitle()).toBe("Intro");
expect(popoverDescription()).toBe("The intro paragraph");
});
it("marks the highlighted element as active and exposes it", () => {
const d = createDriver({ animate: false });
d.highlight({ element: "#intro", popover: { title: "Intro" } });
expect(document.querySelector("#intro")?.classList.contains("driver-active-element")).toBe(true);
expect(d.getActiveElement()).toBe(document.querySelector("#intro"));
});
it("supports element-less modal popovers via a dummy element", () => {
const d = createDriver({ animate: false });
d.highlight({ popover: { title: "Modal", description: "No element" } });
expect(d.isActive()).toBe(true);
expect(popoverTitle()).toBe("Modal");
expect(document.getElementById("driver-dummy-element")).not.toBeNull();
});
it("tears the DOM and state down on destroy", () => {
const d = createDriver({ animate: false });
d.highlight({ element: "#intro", popover: { title: "Intro" } });
d.destroy();
expect(d.isActive()).toBe(false);
expect(popoverEl()).toBeNull();
expect(document.body.classList.contains("driver-active")).toBe(false);
expect(document.querySelector(".driver-active-element")).toBeNull();
expect(d.getActiveIndex()).toBeUndefined();
});
});