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

Dialog.android.test.tsx

67 lines2.2 KB

1import { act, type ReactNode } from "react";
2import { createRoot, type Root } from "react-dom/client";
3import { afterEach, expect, it, vi } from "vite-plus/test";
4​
5vi.mock("@expo/ui/jetpack-compose", () => {
6 const Slot = (props: { readonly children?: ReactNode }) => <div>{props.children}</div>;
7 const Button = (props: { readonly children?: ReactNode; readonly onClick?: () => void }) => (
8 <button onClick={props.onClick}>{props.children}</button>
9 );
10 return {
11 AlertDialog: Object.assign(Slot, {
12 Title: Slot,
13 Text: Slot,
14 DismissButton: Slot,
15 ConfirmButton: Slot,
16 }),
17 Button,
18 OutlinedButton: Button,
19 Column: Slot,
20 FlowRow: Slot,
21 Icon: () => null,
22 OutlinedTextField: Object.assign(Slot, { Label: Slot, Placeholder: Slot }),
23 Text: Slot,
24 useNativeState: (value: unknown) => ({ get: () => value }),
25 };
26});
27vi.mock("./NativeThemeHost.android", () => ({
28 NativeThemeHost: (props: { readonly children?: ReactNode }) => <div>{props.children}</div>,
29}));
30vi.mock("../../features/settings/appearance/AppearancePreferencesProvider", () => ({
31 useAppearancePreferences: () => ({ themeVariables: {} }),
32}));
33vi.mock("../../features/settings/appearance/useScaledTextRole", () => ({
34 useScaledTextRole: () => ({}),
35}));
36vi.mock("../../lib/mobileTheme", () => ({ themeColorWithContrast: () => "#ffffff" }));
37​
38import * as Dialog from "./Dialog.android";
39​
40const roots: Root[] = [];
41​
42function mountHost() {
43 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
44 const container = document.createElement("div");
45 document.body.append(container);
46 const root = createRoot(container);
47 roots.push(root);
48 act(() => root.render(<Dialog.Host />));
49 return { root, container };
51​
52afterEach(() => {
53 for (const root of roots.splice(0)) act(() => root.unmount());
54 document.body.innerHTML = "";
55});
56​
57it("keeps presenting dialogs after an older root's Host unmounts", () => {
58 // Android activity replacement mounts the new root before the old one stops.
59 const previous = mountHost();
60 const current = mountHost();
61 roots.splice(roots.indexOf(previous.root), 1);
62 act(() => previous.root.unmount());
63​
64 act(() => Dialog.alert("Saved", undefined, [{ text: "OK" }]));
65​
66 expect(current.container.textContent).toContain("Saved");
67});