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

createV5StackNavigator.ios.test.tsx

95 lines3.3 KB

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";
36​
37const home = { key: "home", name: "Home" };
38const thread = { key: "thread", name: "Thread" };
39​
40function fire(key: string, event: "onWillAppear" | "onWillDisappear" | "onDidDisappear") {
41 const handler = screens.get(key)?.[event] as (() => void) | undefined;
42 if (!handler) throw new Error(`No ${event} for ${key}`);
43 act(() => handler());
45​
46it("forgets native Back when a full-screen presentation covers the top card", () => {
47 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
48 const state: StackNavigationState<ParamListBase> = {
49 key: "stack",
50 type: "stack",
51 stale: false,
52 index: 1,
53 routeNames: ["Home", "Thread"],
54 routes: [home, thread],
55 preloadedRoutes: [],
56 };
57 const navigation = { getState: () => state, emit: vi.fn(), dispatch: vi.fn() };
58 const descriptor = (route: typeof home) => ({
59 route,
60 navigation,
61 options: {},
62 render: () => null,
63 });
64 const onNativePopChange = vi.fn();
65 const root = createRoot(document.createElement("div"));
66 try {
67 act(() =>
68 root.render(
69 <V5CardStackView
70 {...({
71 state,
72 navigation,
73 descriptors: { home: descriptor(home), thread: descriptor(thread) },
74 describe: () => descriptor(home),
75 } as unknown as V5StackViewProps)}
76 onNativePopChange={onNativePopChange}
77 />,
78 ),
79 );
80​
81 // A presentation covers the thread: nothing in the stack appears beneath it.
82 fire("thread", "onWillDisappear");
83 fire("thread", "onDidDisappear");
84 expect(onNativePopChange).toHaveBeenLastCalledWith(null);
85​
86 // Native Back reveals Home, so the thread stays the card being popped.
87 fire("thread", "onWillDisappear");
88 fire("home", "onWillAppear");
89 fire("thread", "onDidDisappear");
90 expect(onNativePopChange).toHaveBeenLastCalledWith("thread");
91 } finally {
92 act(() => root.unmount());
93 vi.unstubAllGlobals();
94 }
95});