| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- import { act, createElement, type ReactNode } from "react";
- import { createRoot, type Root } from "react-dom/client";
- import { afterEach, describe, expect, it, vi } from "vitest";
- vi.mock("@/components/ui/code-viewer.js", () => ({
- CodeViewer: () => createElement("pre", { "data-code-viewer": "" }),
- }));
- vi.mock("@/components/ai-elements/mermaid-block.js", () => ({
- MermaidBlock: () => createElement("div", { "data-mermaid-block": "" }),
- }));
- vi.mock("@/components/ai-elements/diagram-preview-dialog.js", () => ({
- DiagramPreviewDialog: () => null,
- }));
- vi.mock("@/ControlHintTooltip.js", () => ({
- ControlHintTooltip: ({ children }: { children?: ReactNode }) => children,
- }));
- vi.mock("@/components/ui/button.js", () => ({
- Button: ({ children, ...props }: { children?: ReactNode }) =>
- createElement("button", props, children),
- }));
- vi.mock("@/lib/fileDisplay.js", () => ({
- FileDisplayIcon: () => createElement("span", { "data-file-icon": "" }),
- resolveFileDisplayDescriptor: () => ({ fileIconSrc: "file.svg" }),
- }));
- vi.mock("@/i18n/IntlProvider.js", () => ({
- useZCodeIntl: () => ({
- intl: {
- formatMessage: ({ id }: { id: string }) => id,
- },
- }),
- }));
- vi.mock("@/logger.js", () => ({
- logger: {
- debug: vi.fn(),
- },
- }));
- function createMinimalElement(ownerDocument: Document, tagName = "div") {
- const element = {
- addEventListener: () => {},
- appendChild: (child: { parentNode?: unknown }) => {
- child.parentNode = element;
- return child;
- },
- childNodes: [] as unknown[],
- getAttribute: () => null,
- insertBefore: (child: { parentNode?: unknown }) => {
- child.parentNode = element;
- return child;
- },
- nodeName: tagName.toUpperCase(),
- nodeType: 1,
- ownerDocument,
- parentNode: null as unknown,
- removeAttribute: () => {},
- removeChild: (child: { parentNode?: unknown }) => {
- child.parentNode = null;
- return child;
- },
- removeEventListener: () => {},
- setAttribute: () => {},
- style: {},
- tagName: tagName.toUpperCase(),
- };
- return element as unknown as Element;
- }
- function installMinimalDom() {
- const addEventListener = vi.fn();
- const removeEventListener = vi.fn();
- const documentMock = {
- addEventListener,
- createElement: (tagName: string) =>
- createMinimalElement(documentMock as unknown as Document, tagName),
- createElementNS: (_namespace: string, tagName: string) =>
- createMinimalElement(documentMock as unknown as Document, tagName),
- createTextNode: (nodeValue: string) => ({
- nodeType: 3,
- nodeValue,
- ownerDocument: documentMock,
- parentNode: null,
- }),
- documentElement: {
- classList: {
- contains: () => false,
- },
- },
- hidden: false,
- nodeType: 9,
- removeEventListener,
- visibilityState: "visible",
- } as unknown as Document;
- const windowMock = {
- addEventListener: () => {},
- document: documentMock,
- HTMLIFrameElement: function HTMLIFrameElement() {},
- HTMLElement: function HTMLElement() {},
- Node: function Node() {},
- removeEventListener: () => {},
- };
- Object.defineProperty(globalThis, "document", {
- configurable: true,
- value: documentMock,
- });
- Object.defineProperty(globalThis, "window", {
- configurable: true,
- value: windowMock,
- });
- Object.defineProperty(globalThis, "IS_REACT_ACT_ENVIRONMENT", {
- configurable: true,
- value: true,
- });
- return {
- container: createMinimalElement(documentMock),
- addEventListener,
- removeEventListener,
- };
- }
- describe("CodeBlock visibility subscription", () => {
- afterEach(() => {
- delete (globalThis as { document?: unknown }).document;
- delete (globalThis as { window?: unknown }).window;
- delete (globalThis as { IS_REACT_ACT_ENVIRONMENT?: unknown }).IS_REACT_ACT_ENVIRONMENT;
- });
- it("does not subscribe non-mermaid code blocks to document visibility changes", async () => {
- const { CodeBlock } = await import("@/components/ai-elements/code-block.js");
- const { container, addEventListener } = installMinimalDom();
- const root: Root = createRoot(container);
- act(() => {
- root.render(
- createElement(CodeBlock, {
- code: "const answer = 42;",
- language: "ts",
- }),
- );
- });
- expect(
- addEventListener.mock.calls.filter(([eventName]) => eventName === "visibilitychange"),
- ).toHaveLength(0);
- act(() => {
- root.unmount();
- });
- });
- it("subscribes mermaid code blocks to document visibility changes", async () => {
- const { CodeBlock } = await import("@/components/ai-elements/code-block.js");
- const { container, addEventListener } = installMinimalDom();
- const root: Root = createRoot(container);
- act(() => {
- root.render(
- createElement(CodeBlock, {
- code: "graph TD\nA-->B",
- language: "mermaid",
- }),
- );
- });
- expect(
- addEventListener.mock.calls.filter(([eventName]) => eventName === "visibilitychange"),
- ).toHaveLength(1);
- act(() => {
- root.unmount();
- });
- });
- });
|