Skip to content

Commit 55ffa4b

Browse files
committed
chore: add testing for App.tsx
1 parent e868174 commit 55ffa4b

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

src/client/App.test.tsx

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

0 commit comments

Comments
 (0)