|
| 1 | +// @vitest-environment jsdom |
| 2 | +/** |
| 3 | + * NoteReadingView.test.tsx — 标题栏扩展插槽 headerExtra(v0.13.7 触点②)。 |
| 4 | + * |
| 5 | + * @ai-context: 覆盖标题栏插槽契约——headerExtra 渲染于「编辑」与「删除」之间, |
| 6 | + * 且未传时不渲染(回归:既有标题栏按钮布局不受影响)。 |
| 7 | + */ |
| 8 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 9 | +import { cleanup, render, screen } from "@testing-library/react"; |
| 10 | +import type { Note } from "../types"; |
| 11 | +import NoteReadingView from "./NoteReadingView"; |
| 12 | + |
| 13 | +const baseNote = (): Note => ({ |
| 14 | + id: 1, |
| 15 | + title: "标题", |
| 16 | + content: "# 正文\n内容段落", |
| 17 | + source: "session", |
| 18 | + session_id: 42, |
| 19 | + rule_version: null, |
| 20 | + purify_stats: null, |
| 21 | + tags: "[]", |
| 22 | + properties: null, |
| 23 | + pin: 0, |
| 24 | + group_id: null, |
| 25 | + created_at: 1, |
| 26 | + updated_at: 2, |
| 27 | +}); |
| 28 | + |
| 29 | +const noop = vi.fn(); |
| 30 | + |
| 31 | +const renderView = (headerExtra?: React.ReactNode) => |
| 32 | + render( |
| 33 | + <NoteReadingView |
| 34 | + note={baseNote()} |
| 35 | + editing={false} |
| 36 | + headerExtra={headerExtra} |
| 37 | + onEdit={noop} |
| 38 | + onPinToggle={noop} |
| 39 | + onDelete={noop} |
| 40 | + onTagClick={noop} |
| 41 | + onOpenSession={noop} |
| 42 | + onTaskToggle={noop} |
| 43 | + onImageOpen={noop} |
| 44 | + />, |
| 45 | + ); |
| 46 | + |
| 47 | +afterEach(() => cleanup()); |
| 48 | + |
| 49 | +describe("NoteReadingView 标题栏扩展插槽", () => { |
| 50 | + it("headerExtra 渲染于「删除」按钮之前", () => { |
| 51 | + renderView(<span data-testid="header-extra">🧭 挂到体系</span>); |
| 52 | + const extra = screen.getByTestId("header-extra"); |
| 53 | + expect(extra).toBeTruthy(); |
| 54 | + // 位置契约:extra 在编辑按钮之后、删除按钮之前 |
| 55 | + const editBtn = screen.getByRole("button", { name: /编辑/ }); |
| 56 | + const delBtn = screen.getByRole("button", { name: "删除" }); |
| 57 | + const posOf = (a: Element, b: Element) => a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_FOLLOWING; |
| 58 | + expect(posOf(editBtn, extra)).toBeTruthy(); |
| 59 | + expect(posOf(extra, delBtn)).toBeTruthy(); |
| 60 | + }); |
| 61 | + |
| 62 | + it("未传 headerExtra 时标题栏正常渲染(回归)", () => { |
| 63 | + renderView(undefined); |
| 64 | + expect(screen.getByRole("button", { name: /编辑/ })).toBeTruthy(); |
| 65 | + expect(screen.getByRole("button", { name: "删除" })).toBeTruthy(); |
| 66 | + // 固定按钮内容为 📌(title=「固定」是可访问名称兜底) |
| 67 | + expect(screen.getByRole("button", { name: "📌" })).toBeTruthy(); |
| 68 | + }); |
| 69 | +}); |
0 commit comments