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

NativeHeaderToolbar.test.tsx

135 lines4.5 KB

1import type { NativeStackHeaderItem } from "@react-navigation/native-stack";
2import { act, createContext, type ReactElement } from "react";
3import { createRoot, type Root } from "react-dom/client";
4import { afterEach, expect, it, vi } from "vite-plus/test";
5​
6const harness = vi.hoisted(() => ({
7 params: { path: ["first", "guide.pdf"] },
8 renderedItems: [] as NativeStackHeaderItem[],
9 setOptionsCalls: 0,
10 openedDocuments: [] as string[],
11}));
12​
13// Native headers retain the actions materialized when navigation applies options.
14vi.mock("@react-navigation/native", () => {
15 const navigation = {
16 setOptions(options: {
17 unstable_headerLeftItems?: () => NativeStackHeaderItem[];
18 unstable_headerRightItems?: () => NativeStackHeaderItem[];
19 unstable_headerToolbarItems?: () => NativeStackHeaderItem[];
20 }) {
21 harness.setOptionsCalls += 1;
22 harness.renderedItems =
23 options.unstable_headerLeftItems?.() ??
24 options.unstable_headerRightItems?.() ??
25 options.unstable_headerToolbarItems?.() ??
26 [];
27 },
28 };
29 return {
30 useNavigation: () => navigation,
31 useRoute: () => ({ key: "file", name: "ThreadFile", params: harness.params }),
32 };
33});
34​
35import { NativeHeaderToolbar } from "./NativeHeaderToolbar";
36​
37let root: Root | null = null;
38type Placement = "left" | "right" | "bottom";
39​
40function DocumentToolbar(props: { readonly path: string; readonly placement: Placement }) {
41 return (
42 <NativeHeaderToolbar placement={props.placement}>
43 <NativeHeaderToolbar.Menu title="File actions" icon="ellipsis">
44 <NativeHeaderToolbar.MenuAction onPress={() => harness.openedDocuments.push(props.path)}>
45 Open PDF
46 </NativeHeaderToolbar.MenuAction>
47 </NativeHeaderToolbar.Menu>
48 </NativeHeaderToolbar>
49 );
51​
52function renderDocument(path: string, placement: Placement) {
53 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
54 harness.params = { path: path.split("/") };
55 root ??= createRoot(document.createElement("div"));
56 act(() => root!.render(<DocumentToolbar path={path} placement={placement} />));
58​
59// Like React Navigation's NavigationStateContext, whose default value throws when
60// read. React restores a context's default outside render, so layout effects see it.
61const ContainerStateContext = createContext<object>({
62 get getIsInitial(): never {
63 throw new Error("Couldn't find a navigation context.");
64 },
65});
66const containerState = {};
67​
68function ConnectingToolbar(props: { readonly label: string }) {
69 return (
70 <NativeHeaderToolbar placement="right">
71 <NativeHeaderToolbar.Custom>
72 <span aria-label={props.label} />
73 </NativeHeaderToolbar.Custom>
74 </NativeHeaderToolbar>
75 );
77​
78function renderConnecting(label: string) {
79 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
80 root ??= createRoot(document.createElement("div"));
81 act(() =>
82 root!.render(
83 <ContainerStateContext value={containerState}>
84 <ConnectingToolbar label={label} />
85 </ContainerStateContext>,
86 ),
87 );
89​
90function renderedCustomLabel() {
91 const item = harness.renderedItems[0];
92 if (item?.type !== "custom") throw new Error("Expected the custom header element");
93 return (item.element as ReactElement<{ "aria-label": string }>).props["aria-label"];
95​
96function openRenderedDocument() {
97 const menu = harness.renderedItems[0];
98 if (menu?.type !== "menu") throw new Error("Expected the native file menu");
99 const action = menu.menu.items[0];
100 if (action?.type !== "action") throw new Error("Expected the native Open PDF action");
101 action.onPress?.();
103​
104afterEach(() => {
105 act(() => root?.unmount());
106 root = null;
107 harness.renderedItems = [];
108 harness.setOptionsCalls = 0;
109 harness.openedDocuments = [];
110 vi.unstubAllGlobals();
111});
112​
113it.each(["left", "right", "bottom"] as const)(
114 "updates the %s header action when switching between documents with the same name",
115 (placement) => {
116 renderDocument("first/guide.pdf", placement);
117 openRenderedDocument();
118 renderDocument("second/guide.pdf", placement);
119 openRenderedDocument();
120​
121 expect(harness.openedDocuments).toEqual(["first/guide.pdf", "second/guide.pdf"]);
122 },
123);
124​
125it("applies a custom header element rendered inside a navigation container", () => {
126 renderConnecting("Connecting");
127 expect(renderedCustomLabel()).toBe("Connecting");
128​
129 const applied = harness.setOptionsCalls;
130 renderConnecting("Connecting");
131 expect(harness.setOptionsCalls).toBe(applied);
132​
133 renderConnecting("Reconnecting");
134 expect(renderedCustomLabel()).toBe("Reconnecting");
135});