|
1 | 1 | import type { NativeStackHeaderItem } from "@react-navigation/native-stack"; |
2 | | -import { act, createContext, type ReactElement } from "react"; |
| 2 | +import { act, createContext, useState, type ReactElement } from "react"; |
3 | 3 | import { createRoot, type Root } from "react-dom/client"; |
4 | 4 | import { afterEach, expect, it, vi } from "vite-plus/test"; |
5 | 5 |
|
@@ -61,6 +61,39 @@ function DocumentToolbar(props: { readonly path: string; readonly placement: Pla |
61 | 61 | ); |
62 | 62 | } |
63 | 63 |
|
| 64 | +function PairingForm(props: { |
| 65 | + readonly placement: Placement; |
| 66 | + readonly onPair: (host: string, code: string) => void; |
| 67 | +}) { |
| 68 | + const [host, setHost] = useState(""); |
| 69 | + const [code, setCode] = useState(""); |
| 70 | + const pair = () => props.onPair(host, code); |
| 71 | + return ( |
| 72 | + <> |
| 73 | + <input |
| 74 | + aria-label="Host" |
| 75 | + value={host} |
| 76 | + onInput={(event) => setHost(event.currentTarget.value)} |
| 77 | + /> |
| 78 | + <input |
| 79 | + aria-label="Code" |
| 80 | + value={code} |
| 81 | + onInput={(event) => setCode(event.currentTarget.value)} |
| 82 | + /> |
| 83 | + <NativeHeaderToolbar placement={props.placement}> |
| 84 | + <NativeHeaderToolbar.Button label="Pair" disabled={!host} onPress={pair} /> |
| 85 | + <NativeHeaderToolbar.Menu title="Pairing"> |
| 86 | + <NativeHeaderToolbar.Menu title="Actions"> |
| 87 | + <NativeHeaderToolbar.MenuAction disabled={!host} onPress={pair}> |
| 88 | + Pair |
| 89 | + </NativeHeaderToolbar.MenuAction> |
| 90 | + </NativeHeaderToolbar.Menu> |
| 91 | + </NativeHeaderToolbar.Menu> |
| 92 | + </NativeHeaderToolbar> |
| 93 | + </> |
| 94 | + ); |
| 95 | +} |
| 96 | + |
64 | 97 | function renderDocument(path: string, placement: Placement) { |
65 | 98 | vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
66 | 99 | harness.params = { path: path.split("/") }; |
@@ -135,6 +168,53 @@ it.each(["left", "right", "bottom"] as const)( |
135 | 168 | }, |
136 | 169 | ); |
137 | 170 |
|
| 171 | +it.each(["left", "right", "bottom"] as const)( |
| 172 | + "pairs with current form values through retained %s button and nested menu actions", |
| 173 | + (placement) => { |
| 174 | + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
| 175 | + const container = document.createElement("div"); |
| 176 | + root = createRoot(container); |
| 177 | + const onPair = vi.fn(); |
| 178 | + act(() => root!.render(<PairingForm placement={placement} onPair={onPair} />)); |
| 179 | + |
| 180 | + const edit = (label: string, value: string) => { |
| 181 | + const input = container.querySelector<HTMLInputElement>(`input[aria-label="${label}"]`)!; |
| 182 | + act(() => { |
| 183 | + input.value = value; |
| 184 | + input.dispatchEvent(new Event("input", { bubbles: true })); |
| 185 | + }); |
| 186 | + }; |
| 187 | + edit("Host", "h"); |
| 188 | + |
| 189 | + const button = harness.renderedItems[0]; |
| 190 | + if (button?.type !== "button") throw new Error("Expected the native Pair button"); |
| 191 | + expect(button.disabled).toBe(false); |
| 192 | + const menu = harness.renderedItems[1]; |
| 193 | + if (menu?.type !== "menu") throw new Error("Expected the native pairing menu"); |
| 194 | + const submenu = menu.menu.items[0]; |
| 195 | + if (submenu?.type !== "submenu") throw new Error("Expected the nested actions menu"); |
| 196 | + const action = submenu.items[0]; |
| 197 | + if (action?.type !== "action") throw new Error("Expected the native Pair action"); |
| 198 | + expect(action.disabled).toBe(false); |
| 199 | + const applied = harness.setOptionsCalls; |
| 200 | + const params = harness.params; |
| 201 | + |
| 202 | + edit("Host", "https://my-server.example"); |
| 203 | + edit("Code", "123456"); |
| 204 | + |
| 205 | + expect(harness.params).toBe(params); |
| 206 | + expect(harness.setOptionsCalls).toBe(applied); |
| 207 | + expect(harness.renderedItems[0]).toBe(button); |
| 208 | + expect(harness.renderedItems[1]).toBe(menu); |
| 209 | + button.onPress?.(); |
| 210 | + action.onPress?.(); |
| 211 | + expect(onPair.mock.calls).toEqual([ |
| 212 | + ["https://my-server.example", "123456"], |
| 213 | + ["https://my-server.example", "123456"], |
| 214 | + ]); |
| 215 | + }, |
| 216 | +); |
| 217 | + |
138 | 218 | it("applies a custom header element rendered inside a navigation container", () => { |
139 | 219 | renderConnecting("Connecting"); |
140 | 220 | expect(renderedCustomLabel()).toBe("Connecting"); |
|
0 commit comments