diff --git a/src/client/App.test.tsx b/src/client/App.test.tsx index 466a63a..4eef0e7 100644 --- a/src/client/App.test.tsx +++ b/src/client/App.test.tsx @@ -1,11 +1,6 @@ import type { EditorProps } from "@monaco-editor/react"; import { TooltipProvider } from "@radix-ui/react-tooltip"; -import { - cleanup, - render, - screen, - waitFor, -} from "@testing-library/react"; +import { cleanup, render, screen, waitFor } from "@testing-library/react"; import type { FC } from "react"; import { createBrowserRouter, RouterProvider } from "react-router"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; diff --git a/src/client/Preview.test.tsx b/src/client/Preview.test.tsx new file mode 100644 index 0000000..37a6673 --- /dev/null +++ b/src/client/Preview.test.tsx @@ -0,0 +1,207 @@ +import { + cleanup, + findByTestId, + getByLabelText, + queryAllByLabelText, + render, +} from "@testing-library/react"; +import type { FC } from "react"; +import { PanelGroup } from "react-resizable-panels"; +import { createBrowserRouter, RouterProvider } from "react-router"; +import { afterEach, describe, expect, it } from "vitest"; +import { TooltipProvider } from "@/client/components/Tooltip"; +import { EditorProvider } from "@/client/contexts/editor"; +import { ThemeProvider } from "@/client/contexts/theme"; +import { Preview } from "@/client/Preview"; +import type { ParameterWithSource } from "@/gen/types"; +import { + defaultExampleParameters, + defaultExampleParamterValues, + formTypesExampleParameters, +} from "@/test-data/preview"; +import { mockUsers } from "@/user"; + +type TestAppProps = { + parameters: ParameterWithSource[]; + parameterValues?: Record; +}; + +const TestPreview: FC = ({ + parameters, + parameterValues = {}, +}) => { + return ( + + null} + parameterValues={parameterValues} + setParameterValues={() => null} + output={null} + parameters={parameters} + onReset={() => null} + users={mockUsers} + currentUser={mockUsers[0]} + setUsers={() => null} + /> + + ); +}; + +const router = (parameters: ParameterWithSource[], parameterValues = {}) => + createBrowserRouter([ + { + path: "*", + Component: () => ( + + ), + }, + ]); + +const TestApp: FC = ({ parameters, parameterValues }) => { + return ( + + + + + + + + ); +}; + +describe("Preview - Rendering", () => { + afterEach(() => { + cleanup(); + }); + + it("should render the empty state when no parameters are present", async () => { + const page = render(); + + expect(findByTestId(page.container, "preview-empty-state")); + }); + + it("should render the default example as expected", async () => { + const page = render(); + + getByLabelText(page.container, "Pick your next parameter!*Required"); + + getByLabelText( + page.container, + "Use imaginary experimental features?Immutable", + ); + }); + + it("should render the form type example as with the expected default values", async () => { + const page = render(); + + const formTypeSelects = queryAllByLabelText( + page.container, + "How do you want to format the options of the next parameter?Immutable", + ); + expect(formTypeSelects).toHaveLength(4); + + expect(formTypeSelects[0].innerText).toBe("Radio Selector"); + + const radioGroup = getByLabelText( + page.container, + "Selecting a single value from a list of options.Immutable", + ); + expect(getByLabelText(radioGroup, "Alpha").getAttribute("data-state")).toBe( + "checked", + ); + expect(getByLabelText(radioGroup, "Bravo").getAttribute("data-state")).toBe( + "unchecked", + ); + expect( + getByLabelText(radioGroup, "Charlie").getAttribute("data-state"), + ).toBe("unchecked"); + + expect(formTypeSelects[1].innerText).toBe("Raw input"); + + const numberInput = getByLabelText( + page.container, + "What is your favorite number?Immutable", + ); + expect(numberInput).toBeInstanceOf(HTMLInputElement); + expect(numberInput.getAttribute("value")).toBe("7"); + + expect(formTypeSelects[2].innerText).toBe("Radio"); + + const doYouAgreeWithMeRadio = getByLabelText( + page.container, + "Do you agree with me?Immutable", + ); + expect( + getByLabelText(doYouAgreeWithMeRadio, "Yes").getAttribute("data-state"), + ).toBe("checked"); + expect( + getByLabelText(doYouAgreeWithMeRadio, "No").getAttribute("data-state"), + ).toBe("unchecked"); + + expect(formTypeSelects[3].innerText).toBe("Multi-Select"); + + const checkbox = getByLabelText( + page.container, + "Did you like this demo?Immutable", + ); + expect(checkbox.getAttribute("data-state")).toBe("unchecked"); + }); + + it("should render form elements with set values", async () => { + const page = render( + + ); + + const formTypeSelects = queryAllByLabelText( + page.container, + "How do you want to format the options of the next parameter?Immutable", + ); + + expect(formTypeSelects[0].textContent).toBe("Radio Selector"); + + const singleRadioGroup = getByLabelText( + page.container, + "Selecting a single value from a list of options.Immutable" + ); + expect(getByLabelText(singleRadioGroup, "Alpha").getAttribute("data-state")).toBe( + "unchecked" + ); + expect(getByLabelText(singleRadioGroup, "Bravo").getAttribute("data-state")).toBe( + "checked" + ); + expect(getByLabelText(singleRadioGroup, "Charlie").getAttribute("data-state")).toBe( + "unchecked" + ); + + const numberInput = getByLabelText( + page.container, + "What is your favorite number?Immutable" + ) as HTMLInputElement; + expect(numberInput.value).toBe("48"); + + const booleanRadioGroup = getByLabelText( + page.container, + "Do you agree with me?Immutable" + ); + expect(getByLabelText(booleanRadioGroup, "Yes").getAttribute("data-state")).toBe( + "unchecked" + ); + expect(getByLabelText(booleanRadioGroup, "No").getAttribute("data-state")).toBe( + "checked" + ); + + const likeItCheckbox = getByLabelText( + page.container, + "Did you like this demo?Immutable" + ); + expect(likeItCheckbox.getAttribute("data-state")).toBe("checked"); + }); +}); diff --git a/src/client/Preview.tsx b/src/client/Preview.tsx index 99e5c88..e88bfe5 100644 --- a/src/client/Preview.tsx +++ b/src/client/Preview.tsx @@ -235,7 +235,10 @@ export const Preview: FC = ({ const PreviewEmptyState = () => { return ( -
+
diff --git a/src/client/components/DynamicParameter.tsx b/src/client/components/DynamicParameter.tsx index bbc030d..f02f717 100644 --- a/src/client/components/DynamicParameter.tsx +++ b/src/client/components/DynamicParameter.tsx @@ -151,15 +151,18 @@ const ParameterLabel: FC = ({