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

NativeHeaderToolbar.test.tsx

100 lines3.4 KB

1import type { NativeStackHeaderItem } from "@react-navigation/native-stack";
2import { act } 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​
59function openRenderedDocument() {
60 const menu = harness.renderedItems[0];
61 if (menu?.type !== "menu") throw new Error("Expected the native file menu");
62 const action = menu.menu.items[0];
63 if (action?.type !== "action") throw new Error("Expected the native Open PDF action");
64 action.onPress?.();
66​
67afterEach(() => {
68 act(() => root?.unmount());
69 root = null;
70 harness.renderedItems = [];
71 harness.setOptionsCalls = 0;
72 harness.openedDocuments = [];
73 vi.unstubAllGlobals();
74});
75​
76it.each(["left", "right", "bottom"] as const)(
77 "updates the %s header action when switching between documents with the same name",
78 (placement) => {
79 renderDocument("first/guide.pdf", placement);
80 openRenderedDocument();
81 renderDocument("second/guide.pdf", placement);
82 openRenderedDocument();
83​
84 expect(harness.openedDocuments).toEqual(["first/guide.pdf", "second/guide.pdf"]);
85 },
86);
87​
88it.each(["left", "right", "bottom"] as const)(
89 "does not reapply %s header options for equivalent document renders",
90 (placement) => {
91 renderDocument("first/guide.pdf", placement);
92 const applied = harness.setOptionsCalls;
93 renderDocument("first/guide.pdf", placement);
94 renderDocument("first/guide.pdf", placement);
95​
96 expect(harness.setOptionsCalls).toBe(applied);
97 openRenderedDocument();
98 expect(harness.openedDocuments).toEqual(["first/guide.pdf"]);
99 },
100);