2024-04-29 20:19:05 +03:00
|
|
|
/*
|
2024-09-09 16:57:16 +03:00
|
|
|
* Copyright 2024 New Vector Ltd.
|
2024-04-29 20:19:05 +03:00
|
|
|
* Copyright 2024 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.
|
2024-04-29 20:19:05 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
import React from "react";
|
2024-11-07 14:43:33 +03:00
|
|
|
import { mocked } from "jest-mock";
|
|
|
|
import { render, fireEvent, waitFor } from "jest-matrix-react";
|
|
|
|
import fetchMock from "fetch-mock-jest";
|
2024-04-29 20:19:05 +03:00
|
|
|
|
2024-10-15 16:57:26 +03:00
|
|
|
import ImageView from "../../../../../src/components/views/elements/ImageView";
|
2024-11-07 14:43:33 +03:00
|
|
|
import { FileDownloader } from "../../../../../src/utils/FileDownloader";
|
|
|
|
import Modal from "../../../../../src/Modal";
|
|
|
|
import ErrorDialog from "../../../../../src/components/views/dialogs/ErrorDialog";
|
|
|
|
|
|
|
|
jest.mock("../../../../../src/utils/FileDownloader");
|
2024-04-29 20:19:05 +03:00
|
|
|
|
|
|
|
describe("<ImageView />", () => {
|
2024-11-07 14:43:33 +03:00
|
|
|
beforeEach(() => {
|
|
|
|
jest.resetAllMocks();
|
|
|
|
fetchMock.reset();
|
|
|
|
});
|
|
|
|
|
2024-04-29 20:19:05 +03:00
|
|
|
it("renders correctly", () => {
|
|
|
|
const { container } = render(<ImageView src="https://example.com/image.png" onFinished={jest.fn()} />);
|
|
|
|
expect(container).toMatchSnapshot();
|
|
|
|
});
|
2024-11-07 14:43:33 +03:00
|
|
|
|
|
|
|
it("should download on click", async () => {
|
|
|
|
fetchMock.get("https://example.com/image.png", "TESTFILE");
|
|
|
|
const { getByRole } = render(
|
|
|
|
<ImageView src="https://example.com/image.png" name="filename.png" onFinished={jest.fn()} />,
|
|
|
|
);
|
|
|
|
fireEvent.click(getByRole("button", { name: "Download" }));
|
|
|
|
await waitFor(() =>
|
|
|
|
expect(mocked(FileDownloader).mock.instances[0].download).toHaveBeenCalledWith({
|
|
|
|
blob: expect.anything(),
|
|
|
|
name: "filename.png",
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
expect(fetchMock).toHaveFetched("https://example.com/image.png");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should handle download errors", async () => {
|
|
|
|
const modalSpy = jest.spyOn(Modal, "createDialog");
|
|
|
|
fetchMock.get("https://example.com/image.png", { status: 500 });
|
|
|
|
const { getByRole } = render(
|
|
|
|
<ImageView src="https://example.com/image.png" name="filename.png" onFinished={jest.fn()} />,
|
|
|
|
);
|
|
|
|
fireEvent.click(getByRole("button", { name: "Download" }));
|
|
|
|
await waitFor(() =>
|
|
|
|
expect(modalSpy).toHaveBeenCalledWith(
|
|
|
|
ErrorDialog,
|
|
|
|
expect.objectContaining({
|
|
|
|
title: "Download failed",
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
});
|
2024-04-29 20:19:05 +03:00
|
|
|
});
|