2022-12-21 21:06:26 +08:00
|
|
|
/*
|
2024-09-09 21:57:16 +08:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-12-21 21:06:26 +08:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 21:57:16 +08: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-12-21 21:06:26 +08:00
|
|
|
*/
|
|
|
|
|
2023-02-01 20:55:10 +08:00
|
|
|
import React, { MouseEventHandler } from "react";
|
2024-10-15 00:11:58 +08:00
|
|
|
import { screen, render, RenderResult } from "jest-matrix-react";
|
2023-01-04 12:44:38 +08:00
|
|
|
import userEvent from "@testing-library/user-event";
|
2022-12-21 21:06:26 +08:00
|
|
|
|
2024-10-15 21:57:26 +08:00
|
|
|
import PictureInPictureDragger, {
|
|
|
|
CreatePipChildren,
|
|
|
|
} from "../../../../src/components/structures/PictureInPictureDragger";
|
2022-12-21 21:06:26 +08:00
|
|
|
|
|
|
|
describe("PictureInPictureDragger", () => {
|
|
|
|
let renderResult: RenderResult;
|
|
|
|
|
2022-12-28 21:43:44 +08:00
|
|
|
const mkContent1: Array<CreatePipChildren> = [
|
|
|
|
() => {
|
|
|
|
return <div>content 1</div>;
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const mkContent2: Array<CreatePipChildren> = [
|
|
|
|
() => {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
content 2<br />
|
|
|
|
content 2.2
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
];
|
2022-12-21 21:06:26 +08:00
|
|
|
|
|
|
|
describe("when rendering the dragger with PiP content 1", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
renderResult = render(<PictureInPictureDragger draggable={true}>{mkContent1}</PictureInPictureDragger>);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should render the PiP content", () => {
|
|
|
|
expect(renderResult.container).toMatchSnapshot("pip-content-1");
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and rerendering PiP content 1", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
renderResult.rerender(<PictureInPictureDragger draggable={true}>{mkContent1}</PictureInPictureDragger>);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not change the PiP content", () => {
|
|
|
|
expect(renderResult.container).toMatchSnapshot("pip-content-1");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and rendering PiP content 2", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
renderResult.rerender(<PictureInPictureDragger draggable={true}>{mkContent2}</PictureInPictureDragger>);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should update the PiP content", () => {
|
|
|
|
expect(renderResult.container).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-12-28 21:43:44 +08:00
|
|
|
|
|
|
|
describe("when rendering the dragger with PiP content 1 and 2", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
renderResult = render(
|
|
|
|
<PictureInPictureDragger draggable={true}>{[...mkContent1, ...mkContent2]}</PictureInPictureDragger>,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should render both contents", () => {
|
|
|
|
expect(renderResult.container).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
});
|
2023-01-04 12:44:38 +08:00
|
|
|
|
2023-02-01 20:55:10 +08:00
|
|
|
describe("when rendering the dragger", () => {
|
|
|
|
let clickSpy: jest.Mocked<MouseEventHandler>;
|
|
|
|
let target: HTMLElement;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
clickSpy = jest.fn();
|
|
|
|
render(
|
|
|
|
<PictureInPictureDragger draggable={true}>
|
|
|
|
{[
|
|
|
|
({ onStartMoving }) => (
|
2023-08-09 14:27:31 +08:00
|
|
|
// eslint-disable-next-line jsx-a11y/click-events-have-key-events
|
2023-02-01 20:55:10 +08:00
|
|
|
<div onMouseDown={onStartMoving} onClick={clickSpy}>
|
|
|
|
Hello
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
]}
|
|
|
|
</PictureInPictureDragger>,
|
|
|
|
);
|
|
|
|
target = screen.getByText("Hello");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("and clicking without a drag motion, it should pass the click to children", async () => {
|
|
|
|
await userEvent.pointer([{ keys: "[MouseLeft>]", target }, { keys: "[/MouseLeft]" }]);
|
|
|
|
expect(clickSpy).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("and clicking with a drag motion above the threshold of 5px, it should not pass the click to children", async () => {
|
|
|
|
await userEvent.pointer([{ keys: "[MouseLeft>]", target }, { coords: { x: 60, y: 2 } }, "[/MouseLeft]"]);
|
|
|
|
expect(clickSpy).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("and clickign with a drag motion below the threshold of 5px, it should pass the click to the children", async () => {
|
|
|
|
await userEvent.pointer([{ keys: "[MouseLeft>]", target }, { coords: { x: 4, y: 4 } }, "[/MouseLeft]"]);
|
|
|
|
expect(clickSpy).toHaveBeenCalled();
|
|
|
|
});
|
2023-01-04 12:44:38 +08:00
|
|
|
});
|
2022-12-21 21:06:26 +08:00
|
|
|
});
|