-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathActionsMenu.test.tsx
More file actions
148 lines (131 loc) · 4.05 KB
/
Copy pathActionsMenu.test.tsx
File metadata and controls
148 lines (131 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { fireEvent, render, screen } from "@testing-library/react";
import {
type HTMLProps,
type PropsWithChildren,
type ReactElement,
type MouseEvent as ReactMouseEvent,
} from "react";
import { ThemeProvider } from "styled-components";
import { theme } from "@web/common/styles/theme";
import { afterEach, describe, expect, it, mock } from "bun:test";
let floatingOptions: { onOpenChange?: (open: boolean) => void } | null = null;
mock.module("@floating-ui/react", () => ({
autoUpdate: mock(),
flip: mock(() => ({})),
FloatingFocusManager: ({
children,
closeOnFocusOut,
}: PropsWithChildren<{ closeOnFocusOut?: boolean }>) => (
<div
onFocusCapture={(event) => {
if (closeOnFocusOut === false) return;
if (event.target !== event.currentTarget) {
floatingOptions?.onOpenChange?.(false);
}
}}
>
{children}
</div>
),
FloatingPortal: ({ children }: PropsWithChildren) => <>{children}</>,
offset: mock(() => ({})),
shift: mock(() => ({})),
useClick: mock(() => ({})),
useDismiss: mock(() => ({})),
useFloating: (options: { onOpenChange?: (open: boolean) => void }) => {
floatingOptions = options;
return {
context: {
floatingStyles: {},
},
refs: {
setFloating: mock(),
setReference: mock(),
},
};
},
useInteractions: mock(
(
interactions: Array<{
getItemProps?: (
props?: HTMLProps<HTMLElement>,
) => HTMLProps<HTMLElement>;
}>,
) => ({
getFloatingProps: (props = {}) => props,
getItemProps: (props = {}) =>
interactions.reduce(
(itemProps, interaction) =>
interaction.getItemProps?.(itemProps) ?? itemProps,
props,
),
getReferenceProps: (
props: {
onClick?: (event: ReactMouseEvent<HTMLDivElement>) => void;
} = {},
) => ({
...props,
onClick: (event: ReactMouseEvent<HTMLDivElement>) => {
props.onClick?.(event);
floatingOptions?.onOpenChange?.(true);
},
}),
}),
),
useListNavigation: mock((_context, props) => {
const { focusItemOnHover } = props as { focusItemOnHover?: boolean };
return {
getItemProps: (userProps: HTMLProps<HTMLElement> = {}) => ({
...userProps,
onMouseMove: (event: ReactMouseEvent<HTMLElement>) => {
userProps.onMouseMove?.(event);
if (focusItemOnHover !== false) {
event.currentTarget.focus();
}
},
}),
};
}),
useRole: mock(() => ({})),
}));
mock.module("@web/common/hooks/useGridMaxZIndex", () => ({
useGridMaxZIndex: () => 0,
}));
const { ActionsMenu, useMenuContext } =
require("./ActionsMenu") as typeof import("./ActionsMenu");
const renderWithTheme = (ui: ReactElement) =>
render(<ThemeProvider theme={theme}>{ui}</ThemeProvider>);
const TestMenuItem = () => {
const menuContext = useMenuContext();
const itemProps = menuContext?.getItemProps() ?? {};
return (
<button type="button" role="menuitem" {...itemProps}>
Delete Event
</button>
);
};
describe("ActionsMenu", () => {
afterEach(() => {
floatingOptions = null;
});
it("keeps mouse hover from stealing focus from the editor action trigger", () => {
renderWithTheme(
<ActionsMenu bgColor="#fff">{() => <TestMenuItem />}</ActionsMenu>,
);
const trigger = screen.getByRole("button", { name: "Open actions menu" });
trigger.focus();
fireEvent.click(trigger);
fireEvent.mouseMove(screen.getByRole("menuitem", { name: "Delete Event" }));
expect(document.activeElement).toBe(trigger);
});
it("keeps the menu mounted when focus moves inside it", () => {
renderWithTheme(
<ActionsMenu bgColor="#fff">{() => <TestMenuItem />}</ActionsMenu>,
);
fireEvent.click(screen.getByRole("button", { name: "Open actions menu" }));
fireEvent.focus(screen.getByRole("menuitem", { name: "Delete Event" }));
expect(
screen.getByRole("menuitem", { name: "Delete Event" }),
).toBeVisible();
});
});