2022-01-27 08:55:08 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-01-27 08:55:08 +00:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 13:57:16 +00: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-01-27 08:55:08 +00:00
|
|
|
*/
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
import React from "react";
|
2023-03-06 12:13:17 +00:00
|
|
|
import { fireEvent, render, RenderResult, waitFor } from "@testing-library/react";
|
2022-12-12 11:24:14 +00:00
|
|
|
import { mocked } from "jest-mock";
|
|
|
|
import { Room } from "matrix-js-sdk/src/matrix";
|
2022-01-27 08:55:08 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
import ExportDialog from "../../../../src/components/views/dialogs/ExportDialog";
|
|
|
|
import { ExportType, ExportFormat } from "../../../../src/utils/exportUtils/exportUtils";
|
|
|
|
import { createTestClient, mkStubRoom } from "../../../test-utils";
|
|
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
2022-01-27 08:55:08 +00:00
|
|
|
import HTMLExporter from "../../../../src/utils/exportUtils/HtmlExport";
|
2022-12-12 11:24:14 +00:00
|
|
|
import ChatExport from "../../../../src/customisations/ChatExport";
|
|
|
|
import PlainTextExporter from "../../../../src/utils/exportUtils/PlainTextExport";
|
2022-01-27 08:55:08 +00:00
|
|
|
|
|
|
|
jest.useFakeTimers();
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
const htmlExporterInstance = {
|
2022-01-31 11:54:14 +00:00
|
|
|
export: jest.fn().mockResolvedValue({}),
|
2022-12-12 11:24:14 +00:00
|
|
|
};
|
|
|
|
const plainTextExporterInstance = {
|
2022-01-27 08:55:08 +00:00
|
|
|
export: jest.fn().mockResolvedValue({}),
|
2022-12-12 11:24:14 +00:00
|
|
|
};
|
2022-01-27 08:55:08 +00:00
|
|
|
jest.mock("../../../../src/utils/exportUtils/HtmlExport", () => jest.fn());
|
2022-01-31 11:54:14 +00:00
|
|
|
jest.mock("../../../../src/utils/exportUtils/PlainTextExport", () => jest.fn());
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.mock("../../../../src/customisations/ChatExport", () => ({
|
2022-01-31 11:54:14 +00:00
|
|
|
getForceChatExportParameters: jest.fn().mockReturnValue({}),
|
|
|
|
}));
|
|
|
|
|
|
|
|
const ChatExportMock = mocked(ChatExport);
|
|
|
|
const HTMLExporterMock = mocked(HTMLExporter);
|
|
|
|
const PlainTextExporterMock = mocked(PlainTextExporter);
|
2022-01-27 08:55:08 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("<ExportDialog />", () => {
|
2022-01-27 08:55:08 +00:00
|
|
|
const mockClient = createTestClient();
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.spyOn(MatrixClientPeg, "get").mockReturnValue(mockClient);
|
2022-01-27 08:55:08 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
const roomId = "test:test.org";
|
2022-01-27 08:55:08 +00:00
|
|
|
const defaultProps = {
|
2022-12-12 11:24:14 +00:00
|
|
|
room: mkStubRoom(roomId, "test", mockClient) as unknown as Room,
|
2022-01-27 08:55:08 +00:00
|
|
|
onFinished: jest.fn(),
|
|
|
|
};
|
|
|
|
|
2023-03-06 12:13:17 +00:00
|
|
|
const getComponent = (props = {}) => render(<ExportDialog {...defaultProps} {...props} />);
|
|
|
|
|
|
|
|
const getSizeInput = ({ container }: RenderResult) => container.querySelector('input[id="size-limit"]')!;
|
|
|
|
const getExportTypeInput = ({ container }: RenderResult) => container.querySelector('select[id="export-type"]')!;
|
|
|
|
const getAttachmentsCheckbox = ({ container }: RenderResult) =>
|
|
|
|
container.querySelector('input[id="include-attachments"]')!;
|
|
|
|
const getMessageCountInput = ({ container }: RenderResult) => container.querySelector('input[id="message-count"]')!;
|
|
|
|
const getExportFormatInput = ({ container }: RenderResult, format: ExportFormat) =>
|
|
|
|
container.querySelector(`input[id="exportFormat-${format}"]`)!;
|
|
|
|
const getPrimaryButton = ({ getByTestId }: RenderResult) => getByTestId("dialog-primary-button")!;
|
|
|
|
const getSecondaryButton = ({ getByTestId }: RenderResult) => getByTestId("dialog-cancel-button")!;
|
|
|
|
|
|
|
|
const submitForm = async (component: RenderResult) => fireEvent.click(getPrimaryButton(component));
|
|
|
|
const selectExportFormat = async (component: RenderResult, format: ExportFormat) =>
|
|
|
|
fireEvent.click(getExportFormatInput(component, format));
|
|
|
|
const selectExportType = async (component: RenderResult, type: ExportType) =>
|
|
|
|
fireEvent.change(getExportTypeInput(component), { target: { value: type } });
|
|
|
|
const setMessageCount = async (component: RenderResult, count: number) =>
|
|
|
|
fireEvent.change(getMessageCountInput(component), { target: { value: count } });
|
|
|
|
|
|
|
|
const setSizeLimit = async (component: RenderResult, limit: number) =>
|
|
|
|
fireEvent.change(getSizeInput(component), { target: { value: limit } });
|
2022-01-27 08:55:08 +00:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-01-31 11:54:14 +00:00
|
|
|
HTMLExporterMock.mockClear().mockImplementation(jest.fn().mockReturnValue(htmlExporterInstance));
|
|
|
|
PlainTextExporterMock.mockClear().mockImplementation(jest.fn().mockReturnValue(plainTextExporterInstance));
|
|
|
|
htmlExporterInstance.export.mockClear();
|
|
|
|
plainTextExporterInstance.export.mockClear();
|
|
|
|
|
|
|
|
// default setting value
|
2023-02-16 17:21:44 +00:00
|
|
|
mocked(ChatExportMock.getForceChatExportParameters!).mockClear().mockReturnValue({});
|
2022-01-27 08:55:08 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders export dialog", () => {
|
2022-01-27 08:55:08 +00:00
|
|
|
const component = getComponent();
|
2023-03-06 12:13:17 +00:00
|
|
|
expect(component.container.querySelector(".mx_ExportDialog")).toMatchSnapshot();
|
2022-01-27 08:55:08 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("calls onFinished when cancel button is clicked", () => {
|
2022-01-27 08:55:08 +00:00
|
|
|
const onFinished = jest.fn();
|
|
|
|
const component = getComponent({ onFinished });
|
2023-03-06 12:13:17 +00:00
|
|
|
fireEvent.click(getSecondaryButton(component));
|
2022-01-27 08:55:08 +00:00
|
|
|
expect(onFinished).toHaveBeenCalledWith(false);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("exports room on submit", async () => {
|
2022-01-27 08:55:08 +00:00
|
|
|
const component = getComponent();
|
|
|
|
await submitForm(component);
|
|
|
|
|
2023-03-06 12:13:17 +00:00
|
|
|
await waitFor(() => {
|
|
|
|
// 4th arg is an component function
|
|
|
|
const exportConstructorProps = HTMLExporterMock.mock.calls[0].slice(0, 3);
|
|
|
|
expect(exportConstructorProps).toEqual([
|
|
|
|
defaultProps.room,
|
|
|
|
ExportType.Timeline,
|
|
|
|
{
|
|
|
|
attachmentsIncluded: false,
|
|
|
|
maxSize: 8388608, // 8MB to bytes
|
|
|
|
numberOfMessages: 100,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|
2022-01-31 11:54:14 +00:00
|
|
|
expect(htmlExporterInstance.export).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("exports room using values set from ForceRoomExportParameters", async () => {
|
2023-02-16 17:21:44 +00:00
|
|
|
mocked(ChatExportMock.getForceChatExportParameters!).mockReturnValue({
|
2022-01-31 11:54:14 +00:00
|
|
|
format: ExportFormat.PlainText,
|
|
|
|
range: ExportType.Beginning,
|
|
|
|
sizeMb: 7000,
|
|
|
|
numberOfMessages: 30,
|
|
|
|
includeAttachments: true,
|
|
|
|
});
|
|
|
|
const component = getComponent();
|
|
|
|
await submitForm(component);
|
|
|
|
|
|
|
|
// 4th arg is an component function
|
|
|
|
const exportConstructorProps = PlainTextExporterMock.mock.calls[0].slice(0, 3);
|
|
|
|
expect(exportConstructorProps).toEqual([
|
|
|
|
defaultProps.room,
|
|
|
|
ExportType.Beginning,
|
|
|
|
{
|
|
|
|
attachmentsIncluded: true,
|
|
|
|
maxSize: 7000 * 1024 * 1024,
|
|
|
|
numberOfMessages: 30,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
expect(plainTextExporterInstance.export).toHaveBeenCalled();
|
2022-01-27 08:55:08 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders success screen when export is finished", async () => {
|
2022-01-27 08:55:08 +00:00
|
|
|
const component = getComponent();
|
|
|
|
await submitForm(component);
|
|
|
|
|
|
|
|
jest.runAllTimers();
|
|
|
|
|
2023-03-06 12:13:17 +00:00
|
|
|
expect(component.container.querySelector(".mx_InfoDialog .mx_Dialog_content")).toMatchSnapshot();
|
2022-01-27 08:55:08 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("export format", () => {
|
|
|
|
it("renders export format with html selected by default", () => {
|
2022-01-27 08:55:08 +00:00
|
|
|
const component = getComponent();
|
2023-03-06 12:13:17 +00:00
|
|
|
expect(getExportFormatInput(component, ExportFormat.Html)).toBeChecked();
|
2022-01-27 08:55:08 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("sets export format on radio button click", async () => {
|
2022-01-27 08:55:08 +00:00
|
|
|
const component = getComponent();
|
|
|
|
await selectExportFormat(component, ExportFormat.PlainText);
|
2023-03-06 12:13:17 +00:00
|
|
|
expect(getExportFormatInput(component, ExportFormat.PlainText)).toBeChecked();
|
|
|
|
expect(getExportFormatInput(component, ExportFormat.Html)).not.toBeChecked();
|
2022-01-27 08:55:08 +00:00
|
|
|
});
|
2022-01-31 11:54:14 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("hides export format input when format is valid in ForceRoomExportParameters", () => {
|
2022-01-31 11:54:14 +00:00
|
|
|
const component = getComponent();
|
2023-03-06 12:13:17 +00:00
|
|
|
expect(getExportFormatInput(component, ExportFormat.Html)).toBeChecked();
|
2022-01-31 11:54:14 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("does not render export format when set in ForceRoomExportParameters", () => {
|
2023-02-16 17:21:44 +00:00
|
|
|
mocked(ChatExportMock.getForceChatExportParameters!).mockReturnValue({
|
2022-01-31 11:54:14 +00:00
|
|
|
format: ExportFormat.PlainText,
|
|
|
|
});
|
|
|
|
const component = getComponent();
|
2023-03-06 12:13:17 +00:00
|
|
|
expect(getExportFormatInput(component, ExportFormat.Html)).toBeFalsy();
|
2022-01-31 11:54:14 +00:00
|
|
|
});
|
2022-01-27 08:55:08 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("export type", () => {
|
|
|
|
it("renders export type with timeline selected by default", () => {
|
2022-01-27 08:55:08 +00:00
|
|
|
const component = getComponent();
|
2023-03-06 12:13:17 +00:00
|
|
|
expect(getExportTypeInput(component)).toHaveValue(ExportType.Timeline);
|
2022-01-27 08:55:08 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("sets export type on change", async () => {
|
2022-01-27 08:55:08 +00:00
|
|
|
const component = getComponent();
|
|
|
|
await selectExportType(component, ExportType.Beginning);
|
2023-03-06 12:13:17 +00:00
|
|
|
expect(getExportTypeInput(component)).toHaveValue(ExportType.Beginning);
|
2022-01-27 08:55:08 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("does not render export type when set in ForceRoomExportParameters", () => {
|
2023-02-16 17:21:44 +00:00
|
|
|
mocked(ChatExportMock.getForceChatExportParameters!).mockReturnValue({
|
2022-01-31 11:54:14 +00:00
|
|
|
range: ExportType.Beginning,
|
|
|
|
});
|
|
|
|
const component = getComponent();
|
2023-03-06 12:13:17 +00:00
|
|
|
expect(getExportTypeInput(component)).toBeFalsy();
|
2022-01-31 11:54:14 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("does not render message count input", async () => {
|
2022-01-27 08:55:08 +00:00
|
|
|
const component = getComponent();
|
2023-03-06 12:13:17 +00:00
|
|
|
expect(getMessageCountInput(component)).toBeFalsy();
|
2022-01-27 08:55:08 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders message count input with default value 100 when export type is lastNMessages", async () => {
|
2022-01-27 08:55:08 +00:00
|
|
|
const component = getComponent();
|
|
|
|
await selectExportType(component, ExportType.LastNMessages);
|
2023-03-06 12:13:17 +00:00
|
|
|
expect(getMessageCountInput(component)).toHaveValue(100);
|
2022-01-27 08:55:08 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("sets message count on change", async () => {
|
2022-01-27 08:55:08 +00:00
|
|
|
const component = getComponent();
|
|
|
|
await selectExportType(component, ExportType.LastNMessages);
|
|
|
|
await setMessageCount(component, 10);
|
2023-03-06 12:13:17 +00:00
|
|
|
expect(getMessageCountInput(component)).toHaveValue(10);
|
2022-01-27 08:55:08 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("does not export when export type is lastNMessages and message count is falsy", async () => {
|
2022-01-27 08:55:08 +00:00
|
|
|
const component = getComponent();
|
|
|
|
await selectExportType(component, ExportType.LastNMessages);
|
|
|
|
await setMessageCount(component, 0);
|
|
|
|
await submitForm(component);
|
|
|
|
|
2022-01-31 11:54:14 +00:00
|
|
|
expect(htmlExporterInstance.export).not.toHaveBeenCalled();
|
2022-01-27 08:55:08 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("does not export when export type is lastNMessages and message count is more than max", async () => {
|
2022-01-27 08:55:08 +00:00
|
|
|
const component = getComponent();
|
|
|
|
await selectExportType(component, ExportType.LastNMessages);
|
|
|
|
await setMessageCount(component, 99999999999);
|
|
|
|
await submitForm(component);
|
|
|
|
|
2022-01-31 11:54:14 +00:00
|
|
|
expect(htmlExporterInstance.export).not.toHaveBeenCalled();
|
2022-01-27 08:55:08 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("exports when export type is NOT lastNMessages and message count is falsy", async () => {
|
2022-01-27 08:55:08 +00:00
|
|
|
const component = getComponent();
|
|
|
|
await selectExportType(component, ExportType.LastNMessages);
|
|
|
|
await setMessageCount(component, 0);
|
|
|
|
await selectExportType(component, ExportType.Timeline);
|
|
|
|
await submitForm(component);
|
|
|
|
|
2023-03-06 12:13:17 +00:00
|
|
|
await waitFor(() => {
|
|
|
|
expect(htmlExporterInstance.export).toHaveBeenCalled();
|
|
|
|
});
|
2022-01-27 08:55:08 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("size limit", () => {
|
|
|
|
it("renders size limit input with default value", () => {
|
2022-01-27 08:55:08 +00:00
|
|
|
const component = getComponent();
|
2023-03-06 12:13:17 +00:00
|
|
|
expect(getSizeInput(component)).toHaveValue(8);
|
2022-01-27 08:55:08 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("updates size limit on change", async () => {
|
2022-01-27 08:55:08 +00:00
|
|
|
const component = getComponent();
|
|
|
|
await setSizeLimit(component, 20);
|
2023-03-06 12:13:17 +00:00
|
|
|
expect(getSizeInput(component)).toHaveValue(20);
|
2022-01-27 08:55:08 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("does not export when size limit is falsy", async () => {
|
2022-01-27 08:55:08 +00:00
|
|
|
const component = getComponent();
|
|
|
|
await setSizeLimit(component, 0);
|
|
|
|
await submitForm(component);
|
|
|
|
|
2022-01-31 11:54:14 +00:00
|
|
|
expect(htmlExporterInstance.export).not.toHaveBeenCalled();
|
2022-01-27 08:55:08 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("does not export when size limit is larger than max", async () => {
|
2022-01-27 08:55:08 +00:00
|
|
|
const component = getComponent();
|
|
|
|
await setSizeLimit(component, 2001);
|
|
|
|
await submitForm(component);
|
|
|
|
|
2022-01-31 11:54:14 +00:00
|
|
|
expect(htmlExporterInstance.export).not.toHaveBeenCalled();
|
2022-01-27 08:55:08 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("exports when size limit is max", async () => {
|
2022-01-27 08:55:08 +00:00
|
|
|
const component = getComponent();
|
|
|
|
await setSizeLimit(component, 2000);
|
|
|
|
await submitForm(component);
|
|
|
|
|
2023-03-06 12:13:17 +00:00
|
|
|
await waitFor(() => {
|
|
|
|
expect(htmlExporterInstance.export).toHaveBeenCalled();
|
|
|
|
});
|
2022-01-31 11:54:14 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("does not render size limit input when set in ForceRoomExportParameters", () => {
|
2023-02-16 17:21:44 +00:00
|
|
|
mocked(ChatExportMock.getForceChatExportParameters!).mockReturnValue({
|
2022-01-31 11:54:14 +00:00
|
|
|
sizeMb: 10000,
|
|
|
|
});
|
|
|
|
const component = getComponent();
|
2023-03-06 12:13:17 +00:00
|
|
|
expect(getSizeInput(component)).toBeFalsy();
|
2022-01-31 11:54:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 2000mb size limit does not apply when higher limit is configured in config
|
|
|
|
*/
|
2022-12-12 11:24:14 +00:00
|
|
|
it("exports when size limit set in ForceRoomExportParameters is larger than 2000", async () => {
|
2023-02-16 17:21:44 +00:00
|
|
|
mocked(ChatExportMock.getForceChatExportParameters!).mockReturnValue({
|
2022-01-31 11:54:14 +00:00
|
|
|
sizeMb: 10000,
|
|
|
|
});
|
|
|
|
const component = getComponent();
|
|
|
|
await submitForm(component);
|
|
|
|
|
|
|
|
expect(htmlExporterInstance.export).toHaveBeenCalled();
|
2022-01-27 08:55:08 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("include attachments", () => {
|
|
|
|
it("renders input with default value of false", () => {
|
2022-01-27 08:55:08 +00:00
|
|
|
const component = getComponent();
|
2023-03-06 12:13:17 +00:00
|
|
|
expect(getAttachmentsCheckbox(component)).not.toBeChecked();
|
2022-01-27 08:55:08 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("updates include attachments on change", async () => {
|
2022-01-27 08:55:08 +00:00
|
|
|
const component = getComponent();
|
2023-03-06 12:13:17 +00:00
|
|
|
fireEvent.click(getAttachmentsCheckbox(component));
|
|
|
|
expect(getAttachmentsCheckbox(component)).toBeChecked();
|
2022-01-27 08:55:08 +00:00
|
|
|
});
|
2022-01-31 11:54:14 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("does not render input when set in ForceRoomExportParameters", () => {
|
2023-02-16 17:21:44 +00:00
|
|
|
mocked(ChatExportMock.getForceChatExportParameters!).mockReturnValue({
|
2022-01-31 11:54:14 +00:00
|
|
|
includeAttachments: false,
|
|
|
|
});
|
|
|
|
const component = getComponent();
|
2023-03-06 12:13:17 +00:00
|
|
|
expect(getAttachmentsCheckbox(component)).toBeFalsy();
|
2022-01-31 11:54:14 +00:00
|
|
|
});
|
2022-01-27 08:55:08 +00:00
|
|
|
});
|
|
|
|
});
|