|
| 1 | +import { |
| 2 | + cleanup, |
| 3 | + getByLabelText, |
| 4 | + queryAllByLabelText, |
| 5 | + render, |
| 6 | +} from "@testing-library/react"; |
| 7 | +import type { FC } from "react"; |
| 8 | +import { PanelGroup } from "react-resizable-panels"; |
| 9 | +import { createBrowserRouter, RouterProvider } from "react-router"; |
| 10 | +import { afterEach, describe, expect, it } from "vitest"; |
| 11 | +import { TooltipProvider } from "@/client/components/Tooltip"; |
| 12 | +import { EditorProvider } from "@/client/contexts/editor"; |
| 13 | +import { ThemeProvider } from "@/client/contexts/theme"; |
| 14 | +import { Preview } from "@/client/Preview"; |
| 15 | +import type { ParameterWithSource } from "@/gen/types"; |
| 16 | +import { |
| 17 | + defaultExampleParameters, |
| 18 | + formTypesExampleParameters, |
| 19 | +} from "@/test-data/preview"; |
| 20 | +import { mockUsers } from "@/user"; |
| 21 | + |
| 22 | +type TestAppProps = { |
| 23 | + parameters: ParameterWithSource[]; |
| 24 | +}; |
| 25 | + |
| 26 | +const TestPreview: FC<TestAppProps> = ({ parameters }) => { |
| 27 | + return ( |
| 28 | + <PanelGroup direction="horizontal"> |
| 29 | + <Preview |
| 30 | + wasmLoadState="loaded" |
| 31 | + isDebouncing={false} |
| 32 | + onDownloadOutput={() => null} |
| 33 | + parameterValues={{}} |
| 34 | + setParameterValues={() => null} |
| 35 | + output={null} |
| 36 | + parameters={parameters} |
| 37 | + onReset={() => null} |
| 38 | + users={mockUsers} |
| 39 | + currentUser={mockUsers[0]} |
| 40 | + setUsers={() => null} |
| 41 | + /> |
| 42 | + </PanelGroup> |
| 43 | + ); |
| 44 | +}; |
| 45 | + |
| 46 | +const router = (parameters: ParameterWithSource[]) => |
| 47 | + createBrowserRouter([ |
| 48 | + { |
| 49 | + path: "*", |
| 50 | + Component: () => <TestPreview parameters={parameters} />, |
| 51 | + }, |
| 52 | + ]); |
| 53 | + |
| 54 | +const TestApp: FC<TestAppProps> = ({ parameters }) => { |
| 55 | + return ( |
| 56 | + <ThemeProvider> |
| 57 | + <TooltipProvider> |
| 58 | + <EditorProvider> |
| 59 | + <RouterProvider router={router(parameters)} /> |
| 60 | + </EditorProvider> |
| 61 | + </TooltipProvider> |
| 62 | + </ThemeProvider> |
| 63 | + ); |
| 64 | +}; |
| 65 | + |
| 66 | +describe("Preview - Rendering", () => { |
| 67 | + afterEach(() => { |
| 68 | + cleanup(); |
| 69 | + }); |
| 70 | + |
| 71 | + it("should render the default example as expected", async () => { |
| 72 | + const page = render(<TestApp parameters={defaultExampleParameters} />); |
| 73 | + |
| 74 | + getByLabelText(page.container, "Pick your next parameter!*Required"); |
| 75 | + |
| 76 | + getByLabelText( |
| 77 | + page.container, |
| 78 | + "Use imaginary experimental features?Immutable", |
| 79 | + ); |
| 80 | + }); |
| 81 | + |
| 82 | + it("should render the form type example as expected", async () => { |
| 83 | + const page = render(<TestApp parameters={formTypesExampleParameters} />); |
| 84 | + |
| 85 | + const formTypeSelects = queryAllByLabelText( |
| 86 | + page.container, |
| 87 | + "How do you want to format the options of the next parameter?Immutable", |
| 88 | + ); |
| 89 | + expect(formTypeSelects).length(4); |
| 90 | + |
| 91 | + expect(formTypeSelects[0].innerText).toBe("Radio Selector"); |
| 92 | + |
| 93 | + const radioGroup = getByLabelText( |
| 94 | + page.container, |
| 95 | + "Selecting a single value from a list of options.Immutable", |
| 96 | + ); |
| 97 | + expect(getByLabelText(radioGroup, "Alpha").getAttribute("data-state")).toBe( |
| 98 | + "checked", |
| 99 | + ); |
| 100 | + expect(getByLabelText(radioGroup, "Bravo").getAttribute("data-state")).toBe( |
| 101 | + "unchecked", |
| 102 | + ); |
| 103 | + expect( |
| 104 | + getByLabelText(radioGroup, "Charlie").getAttribute("data-state"), |
| 105 | + ).toBe("unchecked"); |
| 106 | + |
| 107 | + expect(formTypeSelects[1].innerText).toBe("Raw input"); |
| 108 | + |
| 109 | + const numberInput = getByLabelText( |
| 110 | + page.container, |
| 111 | + "What is your favorite number?Immutable", |
| 112 | + ); |
| 113 | + expect(numberInput).toBeInstanceOf(HTMLInputElement); |
| 114 | + expect(numberInput.getAttribute("value")).toBe("7"); |
| 115 | + |
| 116 | + expect(formTypeSelects[2].innerText).toBe("Radio"); |
| 117 | + |
| 118 | + const doYouAgreeWithMeRadio = getByLabelText( |
| 119 | + page.container, |
| 120 | + "Do you agree with me?Immutable", |
| 121 | + ); |
| 122 | + expect( |
| 123 | + getByLabelText(doYouAgreeWithMeRadio, "Yes").getAttribute("data-state"), |
| 124 | + ).toBe("checked"); |
| 125 | + expect( |
| 126 | + getByLabelText(doYouAgreeWithMeRadio, "No").getAttribute("data-state"), |
| 127 | + ).toBe("unchecked"); |
| 128 | + |
| 129 | + expect(formTypeSelects[3].innerText).toBe("Multi-Select"); |
| 130 | + |
| 131 | + const checkbox = getByLabelText( |
| 132 | + page.container, |
| 133 | + "Did you like this demo?Immutable", |
| 134 | + ); |
| 135 | + expect(checkbox.getAttribute("data-state")).toBe("unchecked"); |
| 136 | + }); |
| 137 | +}); |
0 commit comments