Files
wehub-resource-sync b3a7f98e5a
CI / E2E Cloudflare (4/8) (push) Failing after 0s
CI / E2E Cloudflare (8/8) (push) Failing after 1s
CI / Lint (push) Failing after 1s
Auto Extract / Extract (push) Failing after 4s
CI / Version Check (push) Failing after 9s
CI / Integration Tests (push) Failing after 1s
CI / E2E tests (1/8) (push) Failing after 1s
CI / E2E tests (2/8) (push) Failing after 2s
CI / E2E tests (3/8) (push) Failing after 2s
CI / Browser Tests (push) Failing after 1s
CI / E2E tests (5/8) (push) Failing after 1s
CI / E2E Cloudflare (2/8) (push) Failing after 2s
CI / Typecheck (push) Failing after 1s
CI / Changeset Validation (push) Failing after 2s
CI / E2E Cloudflare (5/8) (push) Failing after 1s
CI / E2E Cloudflare (6/8) (push) Failing after 1s
CI / E2E Cloudflare (7/8) (push) Failing after 1s
CodeQL / Analyze (javascript-typescript) (push) Failing after 1s
Format / Format (push) Failing after 0s
CodeQL / Analyze (actions) (push) Failing after 4s
CI / E2E tests (4/8) (push) Failing after 1s
CI / E2E tests (6/8) (push) Failing after 1s
CI / E2E tests (7/8) (push) Failing after 2s
CI / E2E tests (8/8) (push) Failing after 1s
CI / E2E Cloudflare (1/8) (push) Failing after 1s
CI / E2E Cloudflare (3/8) (push) Failing after 2s
Preview Releases / Publish Preview (push) Failing after 0s
zizmor / Run zizmor (push) Failing after 1s
Release / Release (push) Failing after 2s
CI / Smoke Tests (push) Failing after 5m36s
CI / Tests (push) Failing after 6m36s
Release / Sync Templates (push) Has been skipped
CI / E2E Tests (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:23:53 +08:00

97 lines
3.8 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { renderToolbar } from "../../../src/visual-editing/toolbar.js";
// Regex patterns for HTML validation
const EDIT_TOGGLE_CHECKED_REGEX = /id="emdash-edit-toggle"\s+checked/;
describe("renderToolbar", () => {
it("renders toolbar with edit mode off", () => {
const html = renderToolbar({ editMode: false, isPreview: false });
expect(html).toContain('id="emdash-toolbar"');
expect(html).toContain('data-edit-mode="false"');
expect(html).not.toMatch(EDIT_TOGGLE_CHECKED_REGEX);
});
it("renders toolbar with edit mode on", () => {
const html = renderToolbar({ editMode: true, isPreview: false });
expect(html).toContain('data-edit-mode="true"');
expect(html).toContain("checked");
});
it("stores preview state as data attribute", () => {
const html = renderToolbar({ editMode: false, isPreview: true });
expect(html).toContain('data-preview="true"');
});
it("includes toggle switch", () => {
const html = renderToolbar({ editMode: false, isPreview: false });
expect(html).toContain('id="emdash-edit-toggle"');
expect(html).toContain("emdash-tb-toggle");
});
it("includes publish button (hidden by default)", () => {
const html = renderToolbar({ editMode: true, isPreview: false });
expect(html).toContain('id="emdash-tb-publish"');
expect(html).toContain('style="display:none"');
});
it("includes save status element", () => {
const html = renderToolbar({ editMode: true, isPreview: false });
expect(html).toContain('id="emdash-tb-save-status"');
});
it("includes inline editing script with save state tracking", () => {
const html = renderToolbar({ editMode: true, isPreview: false });
expect(html).toContain("<script>");
expect(html).toContain("setSaveState");
expect(html).toContain("unsaved");
expect(html).toContain("contentEditable");
});
it("includes text cursor for editable hover", () => {
const html = renderToolbar({ editMode: true, isPreview: false });
expect(html).toContain("[data-emdash-ref]:hover");
expect(html).toContain("cursor: text");
});
it("includes manifest fetching for field type lookup", () => {
const html = renderToolbar({ editMode: true, isPreview: false });
expect(html).toContain("fetchManifest");
expect(html).toContain("/_emdash/api/manifest");
});
it("unwraps the { data } envelope returned by /_emdash/api/manifest", () => {
// Regression for #103 / #445: the manifest endpoint wraps the payload in
// { data: manifest } (ApiResponse shape), but getFieldKind reads
// manifest.collections directly. Without the unwrap, getFieldKind returns
// null for every field kind, and every click on an edit annotation opens
// the admin in a new tab instead of inline-editing.
const html = renderToolbar({ editMode: true, isPreview: false });
// The unwrap happens inside the fetchManifest .then() callback. Verify
// the generated HTML contains the conditional unwrap rather than
// assigning the raw response.
expect(html).toMatch(/manifestCache\s*=\s*m\s*&&\s*m\.data\s*\?\s*m\.data\s*:\s*m/);
});
it("skips toolbar interception for portableText (inline editor)", () => {
const html = renderToolbar({ editMode: true, isPreview: false });
expect(html).toContain("portableText");
});
it("includes entry status badge styles", () => {
const html = renderToolbar({ editMode: true, isPreview: false });
expect(html).toContain("emdash-tb-badge--draft");
expect(html).toContain("emdash-tb-badge--published");
expect(html).toContain("emdash-tb-badge--pending");
});
it("includes save state badge styles", () => {
const html = renderToolbar({ editMode: true, isPreview: false });
expect(html).toContain("emdash-tb-badge--unsaved");
expect(html).toContain("emdash-tb-badge--saving");
expect(html).toContain("emdash-tb-badge--saved");
expect(html).toContain("emdash-tb-badge--error");
});
});