Skip to content

Commit b1100ce

Browse files
committed
chore: add tests for init
1 parent 9deb9a0 commit b1100ce

File tree

5 files changed

+153
-22
lines changed

5 files changed

+153
-22
lines changed

src/__mocks__/monaco-editor.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { vi } from "vitest";
2+
3+
const editor = {
4+
defineTheme: vi.fn(),
5+
create: vi.fn(() => ({
6+
dispose: vi.fn(),
7+
getValue: vi.fn(() => ''),
8+
setValue: vi.fn(),
9+
onDidChangeModelContent: vi.fn(),
10+
getModel: vi.fn(),
11+
setModel: vi.fn(),
12+
layout: vi.fn(),
13+
})),
14+
};
15+
16+
const monaco = {
17+
editor,
18+
languages: {
19+
register: vi.fn(),
20+
setMonarchTokensProvider: vi.fn(),
21+
setLanguageConfiguration: vi.fn(),
22+
},
23+
};
24+
25+
export default monaco;
26+

src/client/App.test.tsx

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
});

src/client/Preview.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,10 @@ const ErrorBlock: FC<ErroBlockPorps> = ({ diagnostic }) => {
376376

377377
const WasmLoading: FC = () => {
378378
return (
379-
<div className="flex w-full max-w-xs flex-col items-center justify-center gap-2 rounded-xl border border-[#38BDF8] bg-surface-tertiary p-4">
379+
<div
380+
className="flex w-full max-w-xs flex-col items-center justify-center gap-2 rounded-xl border border-[#38BDF8] bg-surface-tertiary p-4"
381+
data-testid="wasm-loading-modal"
382+
>
380383
<LoaderIcon className="animate-spin text-content-primary" />
381384
<div className="text-center">
382385
<p className="font-semibold text-content-primary text-xl">
@@ -392,7 +395,10 @@ const WasmLoading: FC = () => {
392395

393396
const WasmError: FC = () => {
394397
return (
395-
<div className="flex w-full max-w-xs flex-col items-center justify-center gap-2 rounded-xl border border-border-destructive bg-surface-tertiary p-4 text-center">
398+
<div
399+
className="flex w-full max-w-xs flex-col items-center justify-center gap-2 rounded-xl border border-border-destructive bg-surface-tertiary p-4 text-center"
400+
data-testid="wasm-error-modal"
401+
>
396402
<p className="font-semibold text-content-primary text-xl">
397403
Unable to load assets{" "}
398404
</p>

src/utils/wasm.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -92,22 +92,4 @@ export const getDynamicParametersOutput = async (
9292
const output = JSON.parse(rawOutput) as PreviewOutput;
9393

9494
return output;
95-
// if (e instanceof Error) {
96-
// const diagnostic: InternalDiagnostic = {
97-
// severity: "error",
98-
// summary: e.name,
99-
// detail: e.message,
100-
// kind: "internal",
101-
// };
102-
// $setError([diagnostic]);
103-
// } else {
104-
// const diagnostic: InternalDiagnostic = {
105-
// severity: "error",
106-
// summary: "Error",
107-
// detail: "Something went wrong",
108-
// kind: "internal",
109-
// };
110-
111-
// $setError([diagnostic]);
112-
// }
11395
};

vitest.config.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default defineConfig({
88
test: {
99
globals: true,
1010
environment: 'happy-dom',
11-
setupFiles: ['./src/test/setup.ts'],
11+
// setupFiles: ['./src/test/setup.ts'],
1212
css: true,
1313
// Include coverage configuration
1414
coverage: {
@@ -32,7 +32,8 @@ export default defineConfig({
3232
},
3333
resolve: {
3434
alias: {
35-
'@': path.resolve(__dirname, './src')
35+
'@': path.resolve(__dirname, './src'),
36+
"monaco-editor": "./src/__mocks__/monaco-editor.ts"
3637
}
3738
}
3839
})

0 commit comments

Comments
 (0)