Open History to see the last commit
1// @vitest-environment jsdom
2import type { NativeStackHeaderItem } from "@react-navigation/native-stack";
3import { act } from "react";
4import { createRoot, type Root } from "react-dom/client";
5import { afterEach, expect, it, vi } from "vite-plus/test";
6
7const harness = vi.hoisted(() => ({
8 params: { path: ["first", "guide.pdf"] },
9 renderedItems: [] as NativeStackHeaderItem[],
10 setOptionsCalls: 0,
11 openedDocuments: [] as string[],
12}));
13
14// Native headers retain the actions materialized when navigation applies options.
15vi.mock("@react-navigation/native", () => {
16 const navigation = {
17 setOptions(options: {
18 unstable_headerLeftItems?: () => NativeStackHeaderItem[];
19 unstable_headerRightItems?: () => NativeStackHeaderItem[];
20 unstable_headerToolbarItems?: () => NativeStackHeaderItem[];
21 }) {
22 harness.setOptionsCalls += 1;
23 harness.renderedItems =
24 options.unstable_headerLeftItems?.() ??
25 options.unstable_headerRightItems?.() ??
26 options.unstable_headerToolbarItems?.() ??
27 [];
28 },
29 };
30 return {
31 useNavigation: () => navigation,
32 useRoute: () => ({ key: "file", name: "ThreadFile", params: harness.params }),
33 };
34});
35
36import { NativeHeaderToolbar } from "./NativeHeaderToolbar";
37
38let root: Root | null = null;
39type Placement = "left" | "right" | "bottom";
40
41function DocumentToolbar(props: { readonly path: string; readonly placement: Placement }) {
42 return (
43 <NativeHeaderToolbar placement={props.placement}>
44 <NativeHeaderToolbar.Menu title="File actions" icon="ellipsis">
45 <NativeHeaderToolbar.MenuAction onPress={() => harness.openedDocuments.push(props.path)}>
46 Open PDF
47 </NativeHeaderToolbar.MenuAction>
48 </NativeHeaderToolbar.Menu>
49 </NativeHeaderToolbar>
50 );
51}
52
53function renderDocument(path: string, placement: Placement) {
54 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
55 harness.params = { path: path.split("/") };
56 root ??= createRoot(document.createElement("div"));
57 act(() => root!.render(<DocumentToolbar path={path} placement={placement} />));
58}
59
60function openRenderedDocument() {
61 const menu = harness.renderedItems[0];
62 if (menu?.type !== "menu") throw new Error("Expected the native file menu");
63 const action = menu.menu.items[0];
64 if (action?.type !== "action") throw new Error("Expected the native Open PDF action");
65 action.onPress?.();
66}
67
68afterEach(() => {
69 act(() => root?.unmount());
70 root = null;
71 harness.renderedItems = [];
72 harness.setOptionsCalls = 0;
73 harness.openedDocuments = [];
74 vi.unstubAllGlobals();
75});
76
77it.each(["left", "right", "bottom"] as const)(
78 "updates the %s header action when switching between documents with the same name",
79 (placement) => {
80 renderDocument("first/guide.pdf", placement);
81 openRenderedDocument();
82 renderDocument("second/guide.pdf", placement);
83 openRenderedDocument();
84
85 expect(harness.openedDocuments).toEqual(["first/guide.pdf", "second/guide.pdf"]);
86 },
87);
88
89it.each(["left", "right", "bottom"] as const)(
90 "does not reapply %s header options for equivalent document renders",
91 (placement) => {
92 renderDocument("first/guide.pdf", placement);
93 const applied = harness.setOptionsCalls;
94 renderDocument("first/guide.pdf", placement);
95 renderDocument("first/guide.pdf", placement);
96
97 expect(harness.setOptionsCalls).toBe(applied);
98 openRenderedDocument();
99 expect(harness.openedDocuments).toEqual(["first/guide.pdf"]);
100 },
101);