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

Commit 8891658

Browse files
committed
Add test to link creation
1 parent 1f2366a commit 8891658

File tree

3 files changed

+132
-1
lines changed

3 files changed

+132
-1
lines changed

src/components/views/rooms/wysiwyg_composer/components/LinkModal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ interface LinkModalProps {
4444
onClose: () => void;
4545
}
4646

47-
function LinkModal({ composer, isTextEnabled, onClose }: LinkModalProps) {
47+
export function LinkModal({ composer, isTextEnabled, onClose }: LinkModalProps) {
4848
const composerContext = useComposerContext();
4949

5050
const [fields, setFields] = useState({ text: "", link: "" });

test/components/views/rooms/wysiwyg_composer/components/FormattingButtons-test.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import userEvent from "@testing-library/user-event";
2020
import { AllActionStates, FormattingFunctions } from "@matrix-org/matrix-wysiwyg";
2121

2222
import { FormattingButtons } from "../../../../../../src/components/views/rooms/wysiwyg_composer/components/FormattingButtons";
23+
import * as LinkModal from "../../../../../../src/components/views/rooms/wysiwyg_composer/components/LinkModal";
2324

2425
describe("FormattingButtons", () => {
2526
const wysiwyg = {
@@ -28,6 +29,7 @@ describe("FormattingButtons", () => {
2829
underline: jest.fn(),
2930
strikeThrough: jest.fn(),
3031
inlineCode: jest.fn(),
32+
link: jest.fn(),
3133
} as unknown as FormattingFunctions;
3234

3335
const actionStates = {
@@ -36,6 +38,7 @@ describe("FormattingButtons", () => {
3638
underline: "enabled",
3739
strikeThrough: "enabled",
3840
inlineCode: "enabled",
41+
link: "enabled",
3942
} as AllActionStates;
4043

4144
afterEach(() => {
@@ -52,23 +55,27 @@ describe("FormattingButtons", () => {
5255
expect(screen.getByLabelText("Underline")).not.toHaveClass("mx_FormattingButtons_active");
5356
expect(screen.getByLabelText("Strikethrough")).not.toHaveClass("mx_FormattingButtons_active");
5457
expect(screen.getByLabelText("Code")).not.toHaveClass("mx_FormattingButtons_active");
58+
expect(screen.getByLabelText("Link")).not.toHaveClass("mx_FormattingButtons_active");
5559
});
5660

5761
it("Should call wysiwyg function on button click", () => {
5862
// When
63+
const spy = jest.spyOn(LinkModal, "openLinkModal");
5964
render(<FormattingButtons composer={wysiwyg} actionStates={actionStates} />);
6065
screen.getByLabelText("Bold").click();
6166
screen.getByLabelText("Italic").click();
6267
screen.getByLabelText("Underline").click();
6368
screen.getByLabelText("Strikethrough").click();
6469
screen.getByLabelText("Code").click();
70+
screen.getByLabelText("Link").click();
6571

6672
// Then
6773
expect(wysiwyg.bold).toHaveBeenCalledTimes(1);
6874
expect(wysiwyg.italic).toHaveBeenCalledTimes(1);
6975
expect(wysiwyg.underline).toHaveBeenCalledTimes(1);
7076
expect(wysiwyg.strikeThrough).toHaveBeenCalledTimes(1);
7177
expect(wysiwyg.inlineCode).toHaveBeenCalledTimes(1);
78+
expect(spy).toHaveBeenCalledTimes(1);
7279
});
7380

7481
it("Should display the tooltip on mouse over", async () => {
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
Copyright 2022 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { FormattingFunctions } from "@matrix-org/matrix-wysiwyg";
18+
import { render, screen, waitFor } from "@testing-library/react";
19+
import React from "react";
20+
import userEvent from "@testing-library/user-event";
21+
22+
import { ComposerContext } from "../../../../../../src/components/views/rooms/wysiwyg_composer/ComposerContext";
23+
import { LinkModal } from "../../../../../../src/components/views/rooms/wysiwyg_composer/components/LinkModal";
24+
import { mockPlatformPeg } from "../../../../../test-utils";
25+
import * as selection from "../../../../../../src/components/views/rooms/wysiwyg_composer/utils/selection";
26+
27+
describe("LinkModal", () => {
28+
const composer = {
29+
link: jest.fn(),
30+
} as unknown as FormattingFunctions;
31+
const defaultValue = { focusNode: null, anchorNode: null, focusOffset: 3, anchorOffset: 4 };
32+
33+
const customRender = (isTextEnabled: boolean, onClose: () => void) => {
34+
return render(
35+
<ComposerContext.Provider value={{ selection: defaultValue }}>
36+
<LinkModal composer={composer} isTextEnabled={isTextEnabled} onClose={onClose} />
37+
</ComposerContext.Provider>,
38+
);
39+
};
40+
41+
mockPlatformPeg({ overrideBrowserShortcuts: jest.fn().mockReturnValue(false) });
42+
const selectionSpy = jest.spyOn(selection, "setSelection");
43+
44+
afterEach(() => {
45+
jest.clearAllMocks();
46+
jest.useRealTimers();
47+
});
48+
49+
it("Should create a link", async () => {
50+
// When
51+
const onClose = jest.fn();
52+
customRender(false, onClose);
53+
54+
// Then
55+
expect(screen.getByLabelText("Link")).toBeTruthy();
56+
expect(screen.getByText("Save")).toBeDisabled();
57+
58+
// When
59+
await userEvent.type(screen.getByLabelText("Link"), "l");
60+
61+
// Then
62+
await waitFor(() => {
63+
expect(screen.getByText("Save")).toBeEnabled();
64+
expect(screen.getByLabelText("Link")).toHaveAttribute("value", "l");
65+
});
66+
67+
// When
68+
jest.useFakeTimers();
69+
screen.getByText("Save").click();
70+
71+
// Then
72+
expect(selectionSpy).toHaveBeenCalledWith(defaultValue);
73+
expect(onClose).toBeCalledTimes(1);
74+
75+
// When
76+
jest.runAllTimers();
77+
78+
// Then
79+
expect(composer.link).toHaveBeenCalledWith("l", undefined);
80+
});
81+
82+
it("Should create a link with text", async () => {
83+
// When
84+
const onClose = jest.fn();
85+
customRender(true, onClose);
86+
87+
// Then
88+
expect(screen.getByLabelText("Text")).toBeTruthy();
89+
expect(screen.getByLabelText("Link")).toBeTruthy();
90+
expect(screen.getByText("Save")).toBeDisabled();
91+
92+
// When
93+
await userEvent.type(screen.getByLabelText("Text"), "t");
94+
95+
// Then
96+
await waitFor(() => {
97+
expect(screen.getByText("Save")).toBeDisabled();
98+
expect(screen.getByLabelText("Text")).toHaveAttribute("value", "t");
99+
});
100+
101+
// When
102+
await userEvent.type(screen.getByLabelText("Link"), "l");
103+
104+
// Then
105+
await waitFor(() => {
106+
expect(screen.getByText("Save")).toBeEnabled();
107+
expect(screen.getByLabelText("Link")).toHaveAttribute("value", "l");
108+
});
109+
110+
// When
111+
jest.useFakeTimers();
112+
screen.getByText("Save").click();
113+
114+
// Then
115+
expect(selectionSpy).toHaveBeenCalledWith(defaultValue);
116+
expect(onClose).toBeCalledTimes(1);
117+
118+
// When
119+
jest.runAllTimers();
120+
121+
// Then
122+
expect(composer.link).toHaveBeenCalledWith("l", "t");
123+
});
124+
});

0 commit comments

Comments
 (0)