Skip to content

Commit 6c99768

Browse files
committed
chore: add test for preview with set values
1 parent a7135c1 commit 6c99768

File tree

4 files changed

+89
-16
lines changed

4 files changed

+89
-16
lines changed

src/client/App.test.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import type { EditorProps } from "@monaco-editor/react";
22
import { TooltipProvider } from "@radix-ui/react-tooltip";
3-
import {
4-
cleanup,
5-
render,
6-
screen,
7-
waitFor,
8-
} from "@testing-library/react";
3+
import { cleanup, render, screen, waitFor } from "@testing-library/react";
94
import type { FC } from "react";
105
import { createBrowserRouter, RouterProvider } from "react-router";
116
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

src/client/Preview.test.tsx

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,27 @@ import { Preview } from "@/client/Preview";
1616
import type { ParameterWithSource } from "@/gen/types";
1717
import {
1818
defaultExampleParameters,
19+
defaultExampleParamterValues,
1920
formTypesExampleParameters,
2021
} from "@/test-data/preview";
2122
import { mockUsers } from "@/user";
2223

2324
type TestAppProps = {
2425
parameters: ParameterWithSource[];
26+
parameterValues?: Record<string, string>;
2527
};
2628

27-
const TestPreview: FC<TestAppProps> = ({ parameters }) => {
29+
const TestPreview: FC<TestAppProps> = ({
30+
parameters,
31+
parameterValues = {},
32+
}) => {
2833
return (
2934
<PanelGroup direction="horizontal">
3035
<Preview
3136
wasmLoadState="loaded"
3237
isDebouncing={false}
3338
onDownloadOutput={() => null}
34-
parameterValues={{}}
39+
parameterValues={parameterValues}
3540
setParameterValues={() => null}
3641
output={null}
3742
parameters={parameters}
@@ -44,20 +49,25 @@ const TestPreview: FC<TestAppProps> = ({ parameters }) => {
4449
);
4550
};
4651

47-
const router = (parameters: ParameterWithSource[]) =>
52+
const router = (parameters: ParameterWithSource[], parameterValues = {}) =>
4853
createBrowserRouter([
4954
{
5055
path: "*",
51-
Component: () => <TestPreview parameters={parameters} />,
56+
Component: () => (
57+
<TestPreview
58+
parameters={parameters}
59+
parameterValues={parameterValues}
60+
/>
61+
),
5262
},
5363
]);
5464

55-
const TestApp: FC<TestAppProps> = ({ parameters }) => {
65+
const TestApp: FC<TestAppProps> = ({ parameters, parameterValues }) => {
5666
return (
5767
<ThemeProvider>
5868
<TooltipProvider>
5969
<EditorProvider>
60-
<RouterProvider router={router(parameters)} />
70+
<RouterProvider router={router(parameters, parameterValues)} />
6171
</EditorProvider>
6272
</TooltipProvider>
6373
</ThemeProvider>
@@ -73,8 +83,7 @@ describe("Preview - Rendering", () => {
7383
const page = render(<TestApp parameters={[]} />);
7484

7585
expect(findByTestId(page.container, "preview-empty-state"));
76-
77-
})
86+
});
7887

7988
it("should render the default example as expected", async () => {
8089
const page = render(<TestApp parameters={defaultExampleParameters} />);
@@ -87,7 +96,7 @@ describe("Preview - Rendering", () => {
8796
);
8897
});
8998

90-
it("should render the form type example as expected", async () => {
99+
it("should render the form type example as with the expected default values", async () => {
91100
const page = render(<TestApp parameters={formTypesExampleParameters} />);
92101

93102
const formTypeSelects = queryAllByLabelText(
@@ -142,4 +151,57 @@ describe("Preview - Rendering", () => {
142151
);
143152
expect(checkbox.getAttribute("data-state")).toBe("unchecked");
144153
});
154+
155+
it("should render form elements with set values", async () => {
156+
const page = render(
157+
<TestApp
158+
parameters={formTypesExampleParameters}
159+
parameterValues={defaultExampleParamterValues}
160+
/>
161+
);
162+
163+
const formTypeSelects = queryAllByLabelText(
164+
page.container,
165+
"How do you want to format the options of the next parameter?Immutable",
166+
);
167+
168+
expect(formTypeSelects[0].textContent).toBe("Radio Selector");
169+
170+
const singleRadioGroup = getByLabelText(
171+
page.container,
172+
"Selecting a single value from a list of options.Immutable"
173+
);
174+
expect(getByLabelText(singleRadioGroup, "Alpha").getAttribute("data-state")).toBe(
175+
"unchecked"
176+
);
177+
expect(getByLabelText(singleRadioGroup, "Bravo").getAttribute("data-state")).toBe(
178+
"checked"
179+
);
180+
expect(getByLabelText(singleRadioGroup, "Charlie").getAttribute("data-state")).toBe(
181+
"unchecked"
182+
);
183+
184+
const numberInput = getByLabelText(
185+
page.container,
186+
"What is your favorite number?Immutable"
187+
) as HTMLInputElement;
188+
expect(numberInput.value).toBe("48");
189+
190+
const booleanRadioGroup = getByLabelText(
191+
page.container,
192+
"Do you agree with me?Immutable"
193+
);
194+
expect(getByLabelText(booleanRadioGroup, "Yes").getAttribute("data-state")).toBe(
195+
"unchecked"
196+
);
197+
expect(getByLabelText(booleanRadioGroup, "No").getAttribute("data-state")).toBe(
198+
"checked"
199+
);
200+
201+
const likeItCheckbox = getByLabelText(
202+
page.container,
203+
"Did you like this demo?Immutable"
204+
);
205+
expect(likeItCheckbox.getAttribute("data-state")).toBe("checked");
206+
});
145207
});

src/client/Preview.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,10 @@ export const Preview: FC<PreviewProps> = ({
235235

236236
const PreviewEmptyState = () => {
237237
return (
238-
<div className="flex flex-col items-center justify-center gap-3" data-testid="preview-empty-state">
238+
<div
239+
className="flex flex-col items-center justify-center gap-3"
240+
data-testid="preview-empty-state"
241+
>
239242
<div className="flex items-center justify-center rounded-[6px] bg-highlight-sky p-2">
240243
<ActivityIcon className="text-content-invert" width={24} height={24} />
241244
</div>

src/test-data/preview.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,3 +833,16 @@ export const formTypesExampleParameters: ParameterWithSource[] = [
833833
},
834834
},
835835
];
836+
837+
export const defaultExampleParamterValues: Record<string, string> = {
838+
single_select: "radio",
839+
single: "bravo-value",
840+
number_format: "input",
841+
number: "48",
842+
boolean_format: "radio",
843+
boolean: "false",
844+
list_format: "radio",
845+
list: JSON.stringify(["red"]),
846+
like_it: "true",
847+
satisfaction: "38",
848+
};

0 commit comments

Comments
 (0)