Open History to see the last commit
1import { act, createContext, type ReactNode } from "react";
2import { createRoot, type Root } from "react-dom/client";
3import { afterEach, expect, it, vi } from "vite-plus/test";
4
5vi.mock("react-native", () => ({
6 View: (props: {
7 readonly children?: ReactNode;
8 readonly importantForAccessibility?: string;
9 readonly accessibilityElementsHidden?: boolean;
10 readonly hitSlop?: number;
11 }) => (
12 <div
13 data-important={props.importantForAccessibility}
14 data-hidden={props.accessibilityElementsHidden ? "true" : undefined}
15 >
16 {props.children}
17 </div>
18 ),
19 Pressable: (props: {
20 readonly children?: ReactNode;
21 readonly accessibilityRole?: string;
22 readonly accessibilityLabel?: string;
23 readonly accessibilityState?: { readonly selected?: boolean; readonly disabled?: boolean };
24 readonly hitSlop?: number;
25 }) => (
26 <button
27 type="button"
28 data-role={props.accessibilityRole}
29 data-label={props.accessibilityLabel}
30 data-selected={props.accessibilityState?.selected ? "true" : undefined}
31 data-hitslop={props.hitSlop !== undefined ? String(props.hitSlop) : undefined}
32 >
33 {props.children}
34 </button>
35 ),
36 ScrollView: (props: { readonly children?: ReactNode }) => <div>{props.children}</div>,
37}));
38vi.mock("react-native-keyboard-controller", () => ({
39 useKeyboardState: () => false,
40}));
41vi.mock("../../lib/backHandler", () => ({
42 useHardwareBack: () => {},
43}));
44vi.mock("../../native/window-hinge-context", () => ({
45 WindowHingeContext: createContext(null),
46}));
47vi.mock("./OverlayPortal", () => ({
48 OverlayPortal: (props: { readonly children?: ReactNode }) => <>{props.children}</>,
49}));
50vi.mock("./useAndroidControlSizing", () => ({
51 useAndroidControlSizing: () => ({ scale: 1, menuWidth: 200, menuItemHeight: 48 }),
52}));
53vi.mock("./MenuPopup", () => ({
54 MenuPopup: () => null,
55}));
56
57import { Pressable, View } from "react-native";
58import { AndroidAnchoredMenu } from "./AndroidAnchoredMenu.android";
59
60const roots: Root[] = [];
61
62afterEach(() => {
63 for (const root of roots.splice(0)) act(() => root.unmount());
64 document.body.innerHTML = "";
65});
66
67it("lifts trigger accessibility props onto the single outer button and hides inner trigger descendants", () => {
68 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
69 const container = document.createElement("div");
70 document.body.append(container);
71 const root = createRoot(container);
72 roots.push(root);
73
74 act(() =>
75 root.render(
76 <AndroidAnchoredMenu actions={[{ id: "open", title: "Open" }]} title="Fallback">
77 <View>
78 <Pressable
79 accessibilityRole="button"
80 accessibilityLabel="Filter by state"
81 accessibilityState={{ selected: true }}
82 >
83 <span>Open</span>
84 </Pressable>
85 </View>
86 </AndroidAnchoredMenu>,
87 ),
88 );
89
90 const outer = container.firstElementChild as HTMLElement | null;
91 expect(outer?.getAttribute("data-role")).toBe("button");
92 expect(outer?.getAttribute("data-label")).toBe("Filter by state");
93 expect(outer?.getAttribute("data-selected")).toBe("true");
94 const innerHidden = outer?.firstElementChild as HTMLElement | null;
95 expect(innerHidden?.getAttribute("data-important")).toBe("no-hide-descendants");
96 expect(innerHidden?.getAttribute("data-hidden")).toBe("true");
97 const innerButton = innerHidden?.querySelector("button");
98 expect(innerButton?.getAttribute("data-role")).toBeNull();
99 expect(innerButton?.getAttribute("data-label")).toBeNull();
100});
101
102it("resolves missing accessibility fields past hitSlop wrappers and across sibling child arrays", () => {
103 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
104 const container = document.createElement("div");
105 document.body.append(container);
106 const root = createRoot(container);
107 roots.push(root);
108
109 act(() =>
110 root.render(
111 <AndroidAnchoredMenu actions={[{ id: "open", title: "Open" }]}>
112 <View hitSlop={8}>
113 <span>Icon</span>
114 <Pressable
115 accessibilityRole="button"
116 accessibilityLabel="Filter by state"
117 accessibilityState={{ selected: true }}
118 >
119 <span>Open</span>
120 </Pressable>
121 </View>
122 </AndroidAnchoredMenu>,
123 ),
124 );
125
126 const outer = container.firstElementChild as HTMLElement | null;
127 expect(outer?.getAttribute("data-label")).toBe("Filter by state");
128 expect(outer?.getAttribute("data-selected")).toBe("true");
129 expect(outer?.getAttribute("data-hitslop")).toBe("8");
130 const innerButton = outer?.firstElementChild?.querySelector("button");
131 expect(innerButton?.getAttribute("data-role")).toBeNull();
132 expect(innerButton?.getAttribute("data-label")).toBeNull();
133});