|
| 1 | +import type { EditorProps } from "@monaco-editor/react"; |
| 2 | +import { TooltipProvider } from "@radix-ui/react-tooltip"; |
| 3 | +import { render, screen, waitFor } from "@testing-library/react"; |
| 4 | +import type { FC } from "react"; |
| 5 | +import { createBrowserRouter, RouterProvider } from "react-router"; |
| 6 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 7 | +import { App } from "@/client/App"; |
| 8 | +import { EditorProvider } from "@/client/contexts/editor"; |
| 9 | +import { ThemeProvider } from "@/client/contexts/theme"; |
| 10 | +import { initWasm } from "@/utils/wasm"; |
| 11 | +import defaultExample from "@/examples/code/default.tf?raw"; |
| 12 | +import attachGPUExample from "@/examples/code/attach-gpu.tf?raw"; |
| 13 | + |
| 14 | +vi.mock("@/utils/wasm", async () => { |
| 15 | + return { |
| 16 | + initWasm: vi |
| 17 | + .fn<typeof initWasm>(async () => { |
| 18 | + window.go_preview = vi.fn(async () => { |
| 19 | + return JSON.stringify({}); |
| 20 | + }); |
| 21 | + |
| 22 | + return "loaded"; |
| 23 | + }) |
| 24 | + .mockResolvedValue("loaded"), |
| 25 | + getDynamicParametersOutput: vi.fn(async () => null), |
| 26 | + }; |
| 27 | +}); |
| 28 | + |
| 29 | +vi.mock("@monaco-editor/react", () => ({ |
| 30 | + Editor: ({ value, onChange, ...props }: EditorProps) => ( |
| 31 | + <textarea |
| 32 | + data-testid="monaco-editor" |
| 33 | + value={value} |
| 34 | + onChange={(e) => |
| 35 | + onChange?.(e.target.value, { |
| 36 | + changes: [], |
| 37 | + eol: "\n", |
| 38 | + versionId: 1, |
| 39 | + isUndoing: false, |
| 40 | + isRedoing: false, |
| 41 | + isFlush: false, |
| 42 | + isEolChange: false, |
| 43 | + }) |
| 44 | + } |
| 45 | + {...props} |
| 46 | + /> |
| 47 | + ), |
| 48 | +})); |
| 49 | + |
| 50 | +const router = createBrowserRouter([ |
| 51 | + { |
| 52 | + path: "*", |
| 53 | + Component: App, |
| 54 | + }, |
| 55 | +]); |
| 56 | + |
| 57 | +const TestApp: FC = () => { |
| 58 | + return ( |
| 59 | + <ThemeProvider> |
| 60 | + <TooltipProvider> |
| 61 | + <EditorProvider> |
| 62 | + <RouterProvider router={router} /> |
| 63 | + </EditorProvider> |
| 64 | + </TooltipProvider> |
| 65 | + </ThemeProvider> |
| 66 | + ); |
| 67 | +}; |
| 68 | + |
| 69 | +describe("App - Initial State Setup", () => { |
| 70 | + beforeEach(() => { |
| 71 | + delete window.CODE; |
| 72 | + delete window.USERS; |
| 73 | + delete window.go_preview; |
| 74 | + vi.clearAllMocks(); |
| 75 | + }); |
| 76 | + |
| 77 | + it("should show the loading modal while the wasm module is loading", async () => { |
| 78 | + render(<TestApp />); |
| 79 | + |
| 80 | + await waitFor(() => { |
| 81 | + expect(screen.getByText("Loading assets")); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + it("should call initWasm", async () => { |
| 86 | + render(<TestApp />); |
| 87 | + |
| 88 | + await waitFor(() => { |
| 89 | + expect(initWasm).toHaveBeenCalled(); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + it("should not show the loading modal after the wasm has been loaded", async () => { |
| 94 | + render(<TestApp />); |
| 95 | + |
| 96 | + await waitFor(async () => { |
| 97 | + expect(initWasm).toHaveBeenCalled(); |
| 98 | + }); |
| 99 | + |
| 100 | + expect(screen.queryByTestId("wasm-loading-modal")).toBeNull(); |
| 101 | + expect(screen.queryByTestId("wasm-error-modal")).toBeNull(); |
| 102 | + }); |
| 103 | + |
| 104 | + it("should use the default example if `window.CODE` is not defined", async () => { |
| 105 | + render(<TestApp />); |
| 106 | + |
| 107 | + expect(screen.findByDisplayValue(defaultExample)); |
| 108 | + }); |
| 109 | + |
| 110 | + it("should use the value of `window.CODE` if it is defined", async () => { |
| 111 | + window.CODE = attachGPUExample; |
| 112 | + render(<TestApp />); |
| 113 | + |
| 114 | + expect(screen.findByDisplayValue(attachGPUExample)); |
| 115 | + }); |
| 116 | +}); |
0 commit comments