2022-02-24 18:25:27 +03:00
|
|
|
/*
|
2024-09-09 16:57:16 +03:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-02-24 18:25:27 +03:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 16:57:16 +03:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-02-24 18:25:27 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
import React from "react";
|
2023-03-06 15:13:17 +03:00
|
|
|
import { render } from "@testing-library/react";
|
2022-02-24 18:25:27 +03:00
|
|
|
|
2022-03-15 12:30:48 +03:00
|
|
|
import ContextMenu, { ChevronFace } from "../../../../src/components/structures/ContextMenu";
|
|
|
|
import UIStore from "../../../../src/stores/UIStore";
|
2022-11-09 18:33:09 +03:00
|
|
|
import Modal from "../../../../src/Modal";
|
|
|
|
import BaseDialog from "../../../../src/components/views/dialogs/BaseDialog";
|
2022-02-24 18:25:27 +03:00
|
|
|
|
|
|
|
describe("<ContextMenu />", () => {
|
|
|
|
// Hardcode window and menu dimensions
|
|
|
|
const windowSize = 300;
|
|
|
|
const menuSize = 200;
|
2022-12-12 14:24:14 +03:00
|
|
|
jest.spyOn(UIStore, "instance", "get").mockImplementation(
|
|
|
|
() =>
|
|
|
|
({
|
|
|
|
windowWidth: windowSize,
|
|
|
|
windowHeight: windowSize,
|
2024-01-02 21:56:39 +03:00
|
|
|
}) as unknown as UIStore,
|
2022-12-12 14:24:14 +03:00
|
|
|
);
|
2022-02-24 18:25:27 +03:00
|
|
|
window.Element.prototype.getBoundingClientRect = jest.fn().mockReturnValue({
|
|
|
|
width: menuSize,
|
|
|
|
height: menuSize,
|
|
|
|
});
|
|
|
|
|
|
|
|
const targetChevronOffset = 25;
|
|
|
|
|
2023-03-06 15:13:17 +03:00
|
|
|
it("near top edge of window", () => {
|
2022-02-25 20:49:57 +03:00
|
|
|
const targetY = -50;
|
2022-03-15 12:30:48 +03:00
|
|
|
const onFinished = jest.fn();
|
2022-02-25 20:49:57 +03:00
|
|
|
|
2023-03-06 15:13:17 +03:00
|
|
|
render(
|
2022-02-25 20:49:57 +03:00
|
|
|
<ContextMenu
|
|
|
|
bottom={windowSize - targetY - menuSize}
|
|
|
|
right={menuSize}
|
2022-03-15 12:30:48 +03:00
|
|
|
onFinished={onFinished}
|
2022-02-25 20:49:57 +03:00
|
|
|
chevronFace={ChevronFace.Left}
|
|
|
|
chevronOffset={targetChevronOffset}
|
2023-03-08 16:28:07 +03:00
|
|
|
>
|
|
|
|
<React.Fragment />
|
|
|
|
</ContextMenu>,
|
2022-02-25 20:49:57 +03:00
|
|
|
);
|
2023-03-06 15:13:17 +03:00
|
|
|
const chevron = document.querySelector<HTMLElement>(".mx_ContextualMenu_chevron_left")!;
|
2022-02-25 20:49:57 +03:00
|
|
|
|
2023-03-06 15:13:17 +03:00
|
|
|
const bottomStyle = parseInt(
|
|
|
|
document.querySelector<HTMLElement>(".mx_ContextualMenu_wrapper")!.style.getPropertyValue("bottom"),
|
|
|
|
);
|
2022-03-15 12:30:48 +03:00
|
|
|
const actualY = windowSize - bottomStyle - menuSize;
|
2023-03-06 15:13:17 +03:00
|
|
|
const actualChevronOffset = parseInt(chevron.style.getPropertyValue("top"));
|
|
|
|
|
|
|
|
// stays within the window
|
|
|
|
expect(actualY).toBeGreaterThanOrEqual(0);
|
|
|
|
// positions the chevron correctly
|
|
|
|
expect(actualChevronOffset).toEqual(targetChevronOffset + targetY - actualY);
|
2022-02-24 18:25:27 +03:00
|
|
|
});
|
2022-02-25 20:49:57 +03:00
|
|
|
|
2023-03-06 15:13:17 +03:00
|
|
|
it("near right edge of window", () => {
|
2022-02-25 20:49:57 +03:00
|
|
|
const targetX = windowSize - menuSize + 50;
|
2022-03-15 12:30:48 +03:00
|
|
|
const onFinished = jest.fn();
|
2022-02-25 20:49:57 +03:00
|
|
|
|
2023-03-06 15:13:17 +03:00
|
|
|
render(
|
2022-02-25 20:49:57 +03:00
|
|
|
<ContextMenu
|
|
|
|
bottom={0}
|
2022-03-15 12:30:48 +03:00
|
|
|
onFinished={onFinished}
|
2022-02-25 20:49:57 +03:00
|
|
|
left={targetX}
|
|
|
|
chevronFace={ChevronFace.Top}
|
|
|
|
chevronOffset={targetChevronOffset}
|
2023-03-08 16:28:07 +03:00
|
|
|
>
|
|
|
|
<React.Fragment />
|
|
|
|
</ContextMenu>,
|
2022-02-25 20:49:57 +03:00
|
|
|
);
|
2023-03-06 15:13:17 +03:00
|
|
|
const chevron = document.querySelector<HTMLElement>(".mx_ContextualMenu_chevron_top")!;
|
2022-02-25 20:49:57 +03:00
|
|
|
|
2023-03-06 15:13:17 +03:00
|
|
|
const actualX = parseInt(
|
|
|
|
document.querySelector<HTMLElement>(".mx_ContextualMenu_wrapper")!.style.getPropertyValue("left"),
|
|
|
|
);
|
|
|
|
const actualChevronOffset = parseInt(chevron.style.getPropertyValue("left"));
|
2022-02-25 20:49:57 +03:00
|
|
|
|
2023-03-06 15:13:17 +03:00
|
|
|
// stays within the window
|
|
|
|
expect(actualX + menuSize).toBeLessThanOrEqual(windowSize);
|
|
|
|
// positions the chevron correctly
|
|
|
|
expect(actualChevronOffset).toEqual(targetChevronOffset + targetX - actualX);
|
2022-02-25 20:49:57 +03:00
|
|
|
});
|
|
|
|
|
2023-03-06 15:13:17 +03:00
|
|
|
it("near bottom edge of window", () => {
|
2022-02-25 20:49:57 +03:00
|
|
|
const targetY = windowSize - menuSize + 50;
|
2022-03-15 12:30:48 +03:00
|
|
|
const onFinished = jest.fn();
|
2022-02-25 20:49:57 +03:00
|
|
|
|
2023-03-06 15:13:17 +03:00
|
|
|
render(
|
2022-02-25 20:49:57 +03:00
|
|
|
<ContextMenu
|
|
|
|
top={targetY}
|
|
|
|
left={0}
|
2022-03-15 12:30:48 +03:00
|
|
|
onFinished={onFinished}
|
2022-02-25 20:49:57 +03:00
|
|
|
chevronFace={ChevronFace.Right}
|
|
|
|
chevronOffset={targetChevronOffset}
|
2023-03-08 16:28:07 +03:00
|
|
|
>
|
|
|
|
<React.Fragment />
|
|
|
|
</ContextMenu>,
|
2022-02-25 20:49:57 +03:00
|
|
|
);
|
2023-03-06 15:13:17 +03:00
|
|
|
const chevron = document.querySelector<HTMLElement>(".mx_ContextualMenu_chevron_right")!;
|
2022-02-25 20:49:57 +03:00
|
|
|
|
2023-03-06 15:13:17 +03:00
|
|
|
const actualY = parseInt(
|
|
|
|
document.querySelector<HTMLElement>(".mx_ContextualMenu_wrapper")!.style.getPropertyValue("top"),
|
|
|
|
);
|
|
|
|
const actualChevronOffset = parseInt(chevron.style.getPropertyValue("top"));
|
2022-02-25 20:49:57 +03:00
|
|
|
|
2023-03-06 15:13:17 +03:00
|
|
|
// stays within the window
|
|
|
|
expect(actualY + menuSize).toBeLessThanOrEqual(windowSize);
|
|
|
|
// positions the chevron correctly
|
|
|
|
expect(actualChevronOffset).toEqual(targetChevronOffset + targetY - actualY);
|
2022-02-25 20:49:57 +03:00
|
|
|
});
|
|
|
|
|
2023-03-06 15:13:17 +03:00
|
|
|
it("near left edge of window", () => {
|
2022-02-25 20:49:57 +03:00
|
|
|
const targetX = -50;
|
2022-03-15 12:30:48 +03:00
|
|
|
const onFinished = jest.fn();
|
2022-02-25 20:49:57 +03:00
|
|
|
|
2023-03-06 15:13:17 +03:00
|
|
|
render(
|
2022-02-25 20:49:57 +03:00
|
|
|
<ContextMenu
|
|
|
|
top={0}
|
|
|
|
right={windowSize - targetX - menuSize}
|
|
|
|
chevronFace={ChevronFace.Bottom}
|
2022-03-15 12:30:48 +03:00
|
|
|
onFinished={onFinished}
|
2022-02-25 20:49:57 +03:00
|
|
|
chevronOffset={targetChevronOffset}
|
2023-03-08 16:28:07 +03:00
|
|
|
>
|
|
|
|
<React.Fragment />
|
|
|
|
</ContextMenu>,
|
2022-02-25 20:49:57 +03:00
|
|
|
);
|
2023-03-06 15:13:17 +03:00
|
|
|
const chevron = document.querySelector<HTMLElement>(".mx_ContextualMenu_chevron_bottom")!;
|
2022-02-25 20:49:57 +03:00
|
|
|
|
2023-03-06 15:13:17 +03:00
|
|
|
const rightStyle = parseInt(
|
|
|
|
document.querySelector<HTMLElement>(".mx_ContextualMenu_wrapper")!.style.getPropertyValue("right"),
|
|
|
|
);
|
2022-03-15 12:30:48 +03:00
|
|
|
const actualX = windowSize - rightStyle - menuSize;
|
2023-03-06 15:13:17 +03:00
|
|
|
const actualChevronOffset = parseInt(chevron.style.getPropertyValue("left"));
|
|
|
|
|
|
|
|
// stays within the window
|
|
|
|
expect(actualX).toBeGreaterThanOrEqual(0);
|
|
|
|
// positions the chevron correctly
|
|
|
|
expect(actualChevronOffset).toEqual(targetChevronOffset + targetX - actualX);
|
2022-02-24 18:25:27 +03:00
|
|
|
});
|
2022-11-09 18:33:09 +03:00
|
|
|
|
|
|
|
it("should automatically close when a modal is opened", () => {
|
|
|
|
const targetX = -50;
|
|
|
|
const onFinished = jest.fn();
|
|
|
|
|
2023-03-06 15:13:17 +03:00
|
|
|
render(
|
2022-11-09 18:33:09 +03:00
|
|
|
<ContextMenu
|
|
|
|
top={0}
|
|
|
|
right={windowSize - targetX - menuSize}
|
|
|
|
chevronFace={ChevronFace.Bottom}
|
|
|
|
onFinished={onFinished}
|
|
|
|
chevronOffset={targetChevronOffset}
|
2023-03-08 16:28:07 +03:00
|
|
|
>
|
|
|
|
<React.Fragment />
|
|
|
|
</ContextMenu>,
|
2022-11-09 18:33:09 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
expect(onFinished).not.toHaveBeenCalled();
|
|
|
|
Modal.createDialog(BaseDialog);
|
|
|
|
expect(onFinished).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not automatically close when a modal is opened under the existing one", () => {
|
|
|
|
const targetX = -50;
|
|
|
|
const onFinished = jest.fn();
|
|
|
|
|
|
|
|
Modal.createDialog(BaseDialog);
|
2023-03-06 15:13:17 +03:00
|
|
|
render(
|
2022-11-09 18:33:09 +03:00
|
|
|
<ContextMenu
|
|
|
|
top={0}
|
|
|
|
right={windowSize - targetX - menuSize}
|
|
|
|
chevronFace={ChevronFace.Bottom}
|
|
|
|
onFinished={onFinished}
|
|
|
|
chevronOffset={targetChevronOffset}
|
2023-03-08 16:28:07 +03:00
|
|
|
>
|
|
|
|
<React.Fragment />
|
|
|
|
</ContextMenu>,
|
2022-11-09 18:33:09 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
expect(onFinished).not.toHaveBeenCalled();
|
|
|
|
Modal.createDialog(BaseDialog, {}, "", false, true);
|
|
|
|
expect(onFinished).not.toHaveBeenCalled();
|
|
|
|
Modal.appendDialog(BaseDialog);
|
|
|
|
expect(onFinished).not.toHaveBeenCalled();
|
|
|
|
});
|
2022-02-24 18:25:27 +03:00
|
|
|
});
|