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