|
| 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