Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

SearchField.test.tsx

89 lines3.1 KB

1import { act, useRef, useState } from "react";
2import { createRoot } from "react-dom/client";
3import { expect, it, vi } from "vite-plus/test";
4​
5const nativeInput = vi.hoisted(() => ({
6 text: "",
7 type: (_text: string) => {},
8}));
9// Mirrors React Native's TextInput: a commit whose `value` differs from the last native text
10// writes that value to the native field.
11vi.mock("react-native", async () => {
12 const { useLayoutEffect, useState } = await import("react");
13 return {
14 View: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
15 Pressable: ({ children, onPress }: { children: React.ReactNode; onPress: () => void }) => (
16 <button onClick={onPress}>{children}</button>
17 ),
18 TextInput: (props: { value: string; onChangeText: (text: string) => void }) => {
19 const [lastNativeText, setLastNativeText] = useState(props.value);
20 nativeInput.type = (text) => {
21 nativeInput.text = text;
22 props.onChangeText(text);
23 setLastNativeText(text);
24 };
25 useLayoutEffect(() => {
26 if (lastNativeText !== props.value) {
27 nativeInput.text = props.value;
28 setLastNativeText(props.value);
29 }
30 }, [lastNativeText, props.value]);
31 return null;
32 },
33 };
34});
35vi.mock("./AppSymbol", () => ({ SymbolView: () => null }));
36vi.mock("./useAndroidControlSizing", () => ({
37 useAndroidControlSizing: () => ({ scale: 1, mediumIconSize: 24 }),
38}));
39​
40import { SearchField } from "./SearchField";
41​
42it("keeps typed text while late query commits catch up, and applies outside changes", () => {
43 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
44 const emitted: string[] = [];
45 let commitQuery: (query: string) => void = () => {};
46 // The workspace header receives the query only after the slow screen commits.
47 function LaggingHeader() {
48 const [query, setQuery] = useState("");
49 commitQuery = setQuery;
50 const inputRef = useRef(null);
51 return (
52 <SearchField
53 inputRef={inputRef}
54 accessibilityLabel="Search threads"
55 clearAccessibilityLabel="Clear search"
56 placeholder="Search"
57 value={query}
58 onChangeText={(text) => emitted.push(text)}
59 />
60 );
61 }
62 const container = document.createElement("div");
63 const root = createRoot(container);
64 try {
65 act(() => root.render(<LaggingHeader />));
66 for (const text of ["t", "te", "tes", "test"]) act(() => nativeInput.type(text));
67 expect(nativeInput.text).toBe("test");
68 for (const query of ["t", "tes", "test"]) {
69 act(() => commitQuery(query));
70 expect(nativeInput.text).toBe("test");
71 }
72​
73 for (const text of ["tes", "te", "t", "", "x"]) act(() => nativeInput.type(text));
74 for (const query of ["tes", "t", ""]) {
75 act(() => commitQuery(query));
76 expect(nativeInput.text).toBe("x");
77 }
78 act(() => commitQuery("x"));
79 expect(nativeInput.text).toBe("x");
80​
81 act(() => commitQuery("saved"));
82 expect(nativeInput.text).toBe("saved");
83 act(() => container.querySelector("button")!.click());
84 expect(nativeInput.text).toBe("");
85 expect(emitted.at(-1)).toBe("");
86 } finally {
87 act(() => root.unmount());
88 }
89});