|
| 1 | +/* |
| 2 | +Copyright 2023 New Vector Ltd |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import { describe, it, expect, vi } from "vitest"; |
| 18 | +import { render, screen } from "@testing-library/react"; |
| 19 | +import React from "react"; |
| 20 | +import UserProfileIcon from "@vector-im/compound-design-tokens/icons/user-profile.svg"; |
| 21 | + |
| 22 | +import { Menu } from "./Menu"; |
| 23 | +import { MenuItem } from "./MenuItem"; |
| 24 | +import { Button } from "../Button/Button"; |
| 25 | +import userEvent from "@testing-library/user-event"; |
| 26 | +import { useTouchscreen } from "../../utils/useTouchscreen"; |
| 27 | + |
| 28 | +vi.mock("../../utils/useTouchscreen", () => ({ |
| 29 | + useTouchscreen: vi.fn(() => false), |
| 30 | +})); |
| 31 | + |
| 32 | +async function withTouchscreen(continuation: () => Promise<void>) { |
| 33 | + const mock = vi.mocked(useTouchscreen).mockReturnValue(true); |
| 34 | + try { |
| 35 | + await continuation(); |
| 36 | + } finally { |
| 37 | + mock.mockRestore(); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +describe("Menu", () => { |
| 42 | + it("opens", async () => { |
| 43 | + const onOpenChange = vi.fn(); |
| 44 | + render( |
| 45 | + <Menu |
| 46 | + title="Settings" |
| 47 | + open={false} |
| 48 | + onOpenChange={onOpenChange} |
| 49 | + trigger={<Button>Open menu</Button>} |
| 50 | + > |
| 51 | + <MenuItem Icon={UserProfileIcon} label="Profile" onSelect={() => {}} /> |
| 52 | + </Menu>, |
| 53 | + ); |
| 54 | + |
| 55 | + expect(screen.queryByRole("menu")).toBe(null); |
| 56 | + await userEvent.click(screen.getByRole("button")); |
| 57 | + expect(onOpenChange).toHaveBeenLastCalledWith(true); |
| 58 | + }); |
| 59 | + |
| 60 | + it("closes as a floating menu", async () => { |
| 61 | + const onOpenChange = vi.fn(); |
| 62 | + render( |
| 63 | + <Menu |
| 64 | + title="Settings" |
| 65 | + open={true} |
| 66 | + onOpenChange={onOpenChange} |
| 67 | + trigger={<Button>Open menu</Button>} |
| 68 | + > |
| 69 | + <MenuItem Icon={UserProfileIcon} label="Profile" onSelect={() => {}} /> |
| 70 | + </Menu>, |
| 71 | + ); |
| 72 | + |
| 73 | + // Floating menus have a heading |
| 74 | + screen.getByRole("menu"); |
| 75 | + screen.getByRole("heading", { name: "Settings" }); |
| 76 | + await userEvent.click(screen.getByRole("menuitem", { name: "Profile" })); |
| 77 | + expect(onOpenChange).toHaveBeenLastCalledWith(false); |
| 78 | + }); |
| 79 | + |
| 80 | + it("closes as a drawer menu", async () => { |
| 81 | + // Simulate a touchscreen so that the menu turns into a drawer |
| 82 | + await withTouchscreen(async () => { |
| 83 | + const onOpenChange = vi.fn(); |
| 84 | + render( |
| 85 | + <Menu |
| 86 | + title="Settings" |
| 87 | + open={true} |
| 88 | + onOpenChange={onOpenChange} |
| 89 | + trigger={<Button>Open menu</Button>} |
| 90 | + > |
| 91 | + <MenuItem |
| 92 | + Icon={UserProfileIcon} |
| 93 | + label="Profile" |
| 94 | + onSelect={() => {}} |
| 95 | + /> |
| 96 | + </Menu>, |
| 97 | + ); |
| 98 | + |
| 99 | + // Drawers don't have a heading |
| 100 | + screen.getByRole("menu"); |
| 101 | + expect(screen.queryByRole("heading", { name: "Settings" })).toBe(null); |
| 102 | + // Intentionally avoiding userEvent here, because that would trigger a |
| 103 | + // callback that calls Element.setPointerCapture, which apparently JSDOM |
| 104 | + // doesn't implement |
| 105 | + screen.getByRole("menuitem", { name: "Profile" }).click(); |
| 106 | + expect(onOpenChange).toHaveBeenLastCalledWith(false); |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + it("doesn't close if preventDefault is called", async () => { |
| 111 | + await withTouchscreen(async () => { |
| 112 | + const onOpenChange = vi.fn(); |
| 113 | + render( |
| 114 | + <Menu |
| 115 | + title="Settings" |
| 116 | + open={true} |
| 117 | + onOpenChange={onOpenChange} |
| 118 | + trigger={<Button>Open menu</Button>} |
| 119 | + > |
| 120 | + <MenuItem |
| 121 | + Icon={UserProfileIcon} |
| 122 | + label="Profile" |
| 123 | + onSelect={(e) => e.preventDefault()} |
| 124 | + /> |
| 125 | + </Menu>, |
| 126 | + ); |
| 127 | + |
| 128 | + screen.getByRole("menu"); |
| 129 | + expect(screen.queryByRole("heading", { name: "Settings" })).toBe(null); |
| 130 | + screen.getByRole("menuitem", { name: "Profile" }).click(); |
| 131 | + expect(onOpenChange).not.toHaveBeenCalledWith(false); |
| 132 | + }); |
| 133 | + }); |
| 134 | +}); |
0 commit comments