import { describe, it, expect, vi, beforeEach } from "vitest"; import { bootstrap_custom_element } from "./index"; const delay = (ms?: number) => new Promise((resolve) => setTimeout(resolve, ms)); describe("bootstrap_custom_element", () => { const create = vi.fn(); beforeEach(() => { document.body.innerHTML = ""; create.mockClear(); }); bootstrap_custom_element(create); it("parses a element that contains a string literal as a direct child", async () => { document.body.innerHTML = ` import gradio as gr `; await delay(); // Wait for the requestAnimationFrame to run expect(create).toHaveBeenCalledWith( expect.objectContaining({ code: expect.stringMatching(/import gradio as gr/), requirements: [], files: undefined }) ); }); it("parses a element that contains and elements", async () => { document.body.innerHTML = ` import gradio as gr numpy scipy `; await delay(); // Wait for the requestAnimationFrame to run expect(create).toHaveBeenCalledWith( expect.objectContaining({ code: expect.stringMatching(/import gradio as gr/), requirements: ["numpy", "scipy"], files: undefined }) ); }); it("parses a element that contains and elements", async () => { document.body.innerHTML = ` import gradio as gr from foo import foo def foo(): return "bar" numpy scipy `; await delay(); // Wait for the requestAnimationFrame to run expect(create).toHaveBeenCalledWith( expect.objectContaining({ files: { "app.py": { data: expect.stringMatching(/import gradio as gr/) }, "foo.py": { data: expect.stringMatching(/def foo\(\):/) } }, entrypoint: "app.py", code: undefined, requirements: ["numpy", "scipy"] }) ); }); it("dedents the code inside ", async () => { document.body.innerHTML = ` import gradio as gr `; await delay(); // Wait for the requestAnimationFrame to run expect(create).toHaveBeenCalledWith( expect.objectContaining({ code: expect.stringMatching(/^import gradio as gr/), requirements: [], files: undefined }) ); }); it("dedents the code inside ", async () => { document.body.innerHTML = ` import gradio as gr `; await delay(); // Wait for the requestAnimationFrame to run expect(create).toHaveBeenCalledWith( expect.objectContaining({ code: expect.stringMatching(/^import gradio as gr/), requirements: [], files: undefined }) ); }); it("dedents Python code but normal files inside ", async () => { document.body.innerHTML = ` import gradio as gr from foo import foo def foo(): return "bar" Hello, world! `; await delay(); // Wait for the requestAnimationFrame to run expect(create).toHaveBeenCalledWith( expect.objectContaining({ files: { "app.py": { data: expect.stringMatching(/^import gradio as gr/) }, "foo.py": { data: expect.stringMatching(/^def foo\(\):/) }, "data.txt": { data: expect.stringMatching(/Hello, world!/) } }, entrypoint: "app.py", code: undefined, requirements: [] }) ); }); });