import { EditorState } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import { getMarkdownEditorReady, markdownEditorReady } from "@src/lib/markdown/editor/editor-ready";
import { afterEach, describe, expect, it, vi } from "vitest";

describe("markdownEditorReady", () => {
    afterEach(() => {
        vi.useRealTimers();
    });

    it("sets data-markdown-ready after the first animation frame", async () => {
        vi.useFakeTimers({ toFake: ["requestAnimationFrame"] });

        const parent = document.createElement("div");
        document.body.appendChild(parent);

        const view = new EditorView({
            state: EditorState.create({
                doc: "# Title",
                extensions: [markdownEditorReady()],
            }),
            parent,
        });

        expect(getMarkdownEditorReady(view.state)).toBe(false);
        expect(view.dom.getAttribute("data-markdown-ready")).toBeNull();

        await vi.runAllTimersAsync();

        expect(getMarkdownEditorReady(view.state)).toBe(true);
        expect(view.dom.getAttribute("data-markdown-ready")).toBe("true");

        view.destroy();
        parent.remove();
    });
});
