Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: matrix-org/matrix-react-sdk
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 3a178c91e3ba31c49aca707e84e6df73a220935f
Choose a base ref
..
head repository: matrix-org/matrix-react-sdk
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 88916589c35912e138d0a8bb7e10cae90276a964
Choose a head ref
Showing with 124 additions and 0 deletions.
  1. +124 −0 test/components/views/rooms/wysiwyg_composer/components/LinkModal-test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
Copyright 2022 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { FormattingFunctions } from "@matrix-org/matrix-wysiwyg";
import { render, screen, waitFor } from "@testing-library/react";
import React from "react";
import userEvent from "@testing-library/user-event";

import { ComposerContext } from "../../../../../../src/components/views/rooms/wysiwyg_composer/ComposerContext";
import { LinkModal } from "../../../../../../src/components/views/rooms/wysiwyg_composer/components/LinkModal";
import { mockPlatformPeg } from "../../../../../test-utils";
import * as selection from "../../../../../../src/components/views/rooms/wysiwyg_composer/utils/selection";

describe("LinkModal", () => {
const composer = {
link: jest.fn(),
} as unknown as FormattingFunctions;
const defaultValue = { focusNode: null, anchorNode: null, focusOffset: 3, anchorOffset: 4 };

const customRender = (isTextEnabled: boolean, onClose: () => void) => {
return render(
<ComposerContext.Provider value={{ selection: defaultValue }}>
<LinkModal composer={composer} isTextEnabled={isTextEnabled} onClose={onClose} />
</ComposerContext.Provider>,
);
};

mockPlatformPeg({ overrideBrowserShortcuts: jest.fn().mockReturnValue(false) });
const selectionSpy = jest.spyOn(selection, "setSelection");

afterEach(() => {
jest.clearAllMocks();
jest.useRealTimers();
});

it("Should create a link", async () => {
// When
const onClose = jest.fn();
customRender(false, onClose);

// Then
expect(screen.getByLabelText("Link")).toBeTruthy();
expect(screen.getByText("Save")).toBeDisabled();

// When
await userEvent.type(screen.getByLabelText("Link"), "l");

// Then
await waitFor(() => {
expect(screen.getByText("Save")).toBeEnabled();
expect(screen.getByLabelText("Link")).toHaveAttribute("value", "l");
});

// When
jest.useFakeTimers();
screen.getByText("Save").click();

// Then
expect(selectionSpy).toHaveBeenCalledWith(defaultValue);
expect(onClose).toBeCalledTimes(1);

// When
jest.runAllTimers();

// Then
expect(composer.link).toHaveBeenCalledWith("l", undefined);
});

it("Should create a link with text", async () => {
// When
const onClose = jest.fn();
customRender(true, onClose);

// Then
expect(screen.getByLabelText("Text")).toBeTruthy();
expect(screen.getByLabelText("Link")).toBeTruthy();
expect(screen.getByText("Save")).toBeDisabled();

// When
await userEvent.type(screen.getByLabelText("Text"), "t");

// Then
await waitFor(() => {
expect(screen.getByText("Save")).toBeDisabled();
expect(screen.getByLabelText("Text")).toHaveAttribute("value", "t");
});

// When
await userEvent.type(screen.getByLabelText("Link"), "l");

// Then
await waitFor(() => {
expect(screen.getByText("Save")).toBeEnabled();
expect(screen.getByLabelText("Link")).toHaveAttribute("value", "l");
});

// When
jest.useFakeTimers();
screen.getByText("Save").click();

// Then
expect(selectionSpy).toHaveBeenCalledWith(defaultValue);
expect(onClose).toBeCalledTimes(1);

// When
jest.runAllTimers();

// Then
expect(composer.link).toHaveBeenCalledWith("l", "t");
});
});