Open History to see the last commit
1import type { ParamListBase, StackNavigationState } from "@react-navigation/native";
2import { act, createContext, type ComponentProps, type ReactNode } from "react";
3import { createRoot } from "react-dom/client";
4import type { Stack as NativeStack } from "react-native-screens";
5import { expect, it, vi } from "vite-plus/test";
6
7type ScreenProps = ComponentProps<typeof NativeStack.Screen>;
8const screens = vi.hoisted(() => new Map<string, ScreenProps>());
9
10vi.mock("react-native", () => ({
11 StyleSheet: { flatten: (style: unknown) => style },
12 View: ({ children }: { children?: ReactNode }) => children,
13}));
14vi.mock("react-native-screens", () => ({
15 Stack: {
16 Host: ({ children }: { children?: ReactNode }) => children,
17 Screen: (props: ScreenProps) => {
18 screens.set(props.screenKey, props);
19 return null;
20 },
21 },
22}));
23vi.mock("@react-navigation/native", () => ({
24 NavigationContext: createContext(undefined),
25 NavigationRouteContext: createContext(undefined),
26 usePreventRemoveContext: () => ({ preventedRoutes: {} }),
27}));
28vi.mock("@react-navigation/native-stack", () => ({ NativeStackView: () => null }));
29vi.mock("./V5StackHeader.ios", () => ({ V5StackHeader: () => null }));
30vi.mock("./NativeColumnContent.ios", () => ({ NativeColumnContent: () => null }));
31vi.mock("./NativeScrollViewMarker", () => ({
32 NativeScrollEdgeEffectsContext: createContext(undefined),
33}));
34
35import { V5CardStackView, type V5StackViewProps } from "./createV5StackNavigator.ios";
36import { NativeWorkspaceTabActivityContext } from "./v5-workspace-context";
37
38const home = { key: "home", name: "Home" };
39const thread = { key: "thread", name: "Thread" };
40
41function fire(
42 key: string,
43 event: "onWillAppear" | "onDidAppear" | "onWillDisappear" | "onDidDisappear",
44) {
45 const handler = screens.get(key)?.[event] as (() => void) | undefined;
46 if (!handler) throw new Error(`No ${event} for ${key}`);
47 act(() => handler());
48}
49
50function renderThreadStack(tabActive?: boolean) {
51 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
52 screens.clear();
53 const state: StackNavigationState<ParamListBase> = {
54 key: "stack",
55 type: "stack",
56 stale: false,
57 index: 1,
58 routeNames: ["Home", "Thread"],
59 routes: [home, thread],
60 preloadedRoutes: [],
61 };
62 const navigation = { getState: () => state, emit: vi.fn(), dispatch: vi.fn() };
63 const descriptor = (route: typeof home) => ({
64 route,
65 navigation,
66 options: {},
67 render: () => null,
68 });
69 const onRootVisibilityChange = vi.fn();
70 const onNativePopChange = vi.fn();
71 const stack = (
72 <V5CardStackView
73 {...({
74 state,
75 navigation,
76 descriptors: { home: descriptor(home), thread: descriptor(thread) },
77 describe: () => descriptor(home),
78 } as unknown as V5StackViewProps)}
79 onRootVisibilityChange={onRootVisibilityChange}
80 onNativePopChange={onNativePopChange}
81 />
82 );
83 const root = createRoot(document.createElement("div"));
84 act(() =>
85 root.render(
86 tabActive === undefined ? (
87 stack
88 ) : (
89 <NativeWorkspaceTabActivityContext value={{ active: tabActive, isActive: () => tabActive }}>
90 {stack}
91 </NativeWorkspaceTabActivityContext>
92 ),
93 ),
94 );
95 return {
96 onRootVisibilityChange,
97 onNativePopChange,
98 [Symbol.dispose]() {
99 act(() => root.unmount());
100 vi.unstubAllGlobals();
101 },
102 };
103}
104
105it("forgets native Back when a full-screen presentation covers the top card", () => {
106 using stack = renderThreadStack();
107
108 // A presentation covers the thread: nothing in the stack appears beneath it.
109 fire("thread", "onWillDisappear");
110 fire("thread", "onDidDisappear");
111 expect(stack.onNativePopChange).toHaveBeenLastCalledWith(null);
112
113 // Native Back reveals Home, so the thread stays the card being popped.
114 fire("thread", "onWillDisappear");
115 fire("home", "onWillAppear");
116 fire("thread", "onDidDisappear");
117 expect(stack.onNativePopChange).toHaveBeenLastCalledWith("thread");
118});
119
120it("hides the root when a detail card appears, including after a cancelled back swipe", () => {
121 using stack = renderThreadStack(true);
122
123 // Cold-start deep link: Home's initial willAppear arrives before the thread's.
124 fire("home", "onWillAppear");
125 fire("thread", "onWillAppear");
126 expect(stack.onRootVisibilityChange).toHaveBeenLastCalledWith(false);
127
128 fire("home", "onWillAppear");
129 fire("thread", "onDidAppear");
130 expect(stack.onRootVisibilityChange).toHaveBeenLastCalledWith(false);
131});
132
133it("ignores card events from an inactive workspace tab", () => {
134 using stack = renderThreadStack(false);
135
136 fire("home", "onWillAppear");
137 fire("thread", "onWillDisappear");
138 expect(stack.onRootVisibilityChange).not.toHaveBeenCalled();
139 expect(stack.onNativePopChange).not.toHaveBeenCalled();
140});