2025-06-24 14:26:03 +01:00
|
|
|
/*
|
|
|
|
|
Copyright 2025 New Vector Ltd.
|
|
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
|
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import React from "react";
|
|
|
|
|
import { render, fireEvent } from "jest-matrix-react";
|
2026-05-12 12:30:30 +01:00
|
|
|
import { useMockedViewModel } from "@element-hq/web-shared-components";
|
2025-06-24 14:26:03 +01:00
|
|
|
|
|
|
|
|
import FileDropTarget from "../../../../src/components/structures/FileDropTarget.tsx";
|
2026-05-18 22:41:38 +01:00
|
|
|
import { RoomUploadContext, type RoomUploadViewModel } from "../../../../src/viewmodels/room/RoomUploadViewModel.tsx";
|
2026-05-12 12:30:30 +01:00
|
|
|
|
|
|
|
|
function FileDropTargetWrapped({
|
|
|
|
|
element,
|
2026-05-18 22:41:38 +01:00
|
|
|
mayDragAndDropFile = false,
|
|
|
|
|
onFileDrop,
|
2026-05-12 12:30:30 +01:00
|
|
|
}: {
|
|
|
|
|
element: HTMLDivElement;
|
2026-05-18 22:41:38 +01:00
|
|
|
mayDragAndDropFile?: boolean;
|
|
|
|
|
onFileDrop: RoomUploadViewModel["initiateViaDataTransfer"];
|
2026-05-12 12:30:30 +01:00
|
|
|
}) {
|
2026-05-18 22:41:38 +01:00
|
|
|
const mockVm = useMockedViewModel<
|
|
|
|
|
{ mayDragAndDropFile: boolean },
|
|
|
|
|
Pick<RoomUploadViewModel, "initiateViaDataTransfer">
|
|
|
|
|
>({ mayDragAndDropFile }, { initiateViaDataTransfer: onFileDrop });
|
2026-05-12 12:30:30 +01:00
|
|
|
return (
|
|
|
|
|
<RoomUploadContext.Provider value={mockVm as RoomUploadViewModel}>
|
|
|
|
|
<FileDropTarget parent={element} />
|
|
|
|
|
</RoomUploadContext.Provider>
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-06-24 14:26:03 +01:00
|
|
|
|
|
|
|
|
describe("FileDropTarget", () => {
|
|
|
|
|
it("should render nothing when idle", () => {
|
|
|
|
|
const element = document.createElement("div");
|
|
|
|
|
const onFileDrop = jest.fn();
|
|
|
|
|
|
2026-05-12 12:30:30 +01:00
|
|
|
const { asFragment } = render(
|
2026-05-18 22:41:38 +01:00
|
|
|
<FileDropTargetWrapped element={element} mayDragAndDropFile onFileDrop={onFileDrop} />,
|
2026-05-12 12:30:30 +01:00
|
|
|
);
|
2025-06-24 14:26:03 +01:00
|
|
|
expect(asFragment()).toMatchSnapshot();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should render drop file prompt on mouse over with file if permissions allow", () => {
|
|
|
|
|
const element = document.createElement("div");
|
|
|
|
|
const onFileDrop = jest.fn();
|
2026-05-12 12:30:30 +01:00
|
|
|
const { asFragment } = render(
|
2026-05-18 22:41:38 +01:00
|
|
|
<FileDropTargetWrapped element={element} mayDragAndDropFile onFileDrop={onFileDrop} />,
|
2026-05-12 12:30:30 +01:00
|
|
|
);
|
2025-06-24 14:26:03 +01:00
|
|
|
fireEvent.dragEnter(element, {
|
|
|
|
|
dataTransfer: {
|
|
|
|
|
types: ["Files"],
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
expect(asFragment()).toMatchSnapshot();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should not render drop file prompt on mouse over with file if permissions do not allow", () => {
|
|
|
|
|
const element = document.createElement("div");
|
|
|
|
|
const onFileDrop = jest.fn();
|
2026-05-18 22:41:38 +01:00
|
|
|
const { asFragment } = render(<FileDropTargetWrapped element={element} onFileDrop={onFileDrop} />);
|
2025-06-24 14:26:03 +01:00
|
|
|
fireEvent.dragEnter(element, {
|
|
|
|
|
dataTransfer: {
|
|
|
|
|
types: ["Files"],
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
expect(asFragment()).toMatchSnapshot();
|
|
|
|
|
});
|
|
|
|
|
});
|