2020-01-06 14:52:08 +00:00
|
|
|
/*
|
2022-04-28 09:32:50 +00:00
|
|
|
Copyright 2019, 2022 The Matrix.org Foundation C.I.C.
|
2020-01-06 14:52:08 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-01-06 13:28:29 +00:00
|
|
|
import React from "react";
|
2023-02-13 11:39:16 +00:00
|
|
|
import { MatrixClient, MatrixEvent } from "matrix-js-sdk/src/matrix";
|
2022-04-28 09:32:50 +00:00
|
|
|
import { MockedObject } from "jest-mock";
|
2022-12-29 16:21:00 +00:00
|
|
|
import { render } from "@testing-library/react";
|
2020-01-06 13:28:29 +00:00
|
|
|
|
2022-04-28 09:32:50 +00:00
|
|
|
import { getMockClientWithEventEmitter, mkEvent, mkStubRoom } from "../../../test-utils";
|
2021-06-29 12:11:58 +00:00
|
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
2020-01-06 14:38:21 +00:00
|
|
|
import * as languageHandler from "../../../../src/languageHandler";
|
2021-07-16 19:30:26 +00:00
|
|
|
import DMRoomMap from "../../../../src/utils/DMRoomMap";
|
2022-04-28 09:32:50 +00:00
|
|
|
import TextualBody from "../../../../src/components/views/messages/TextualBody";
|
|
|
|
import MatrixClientContext from "../../../../src/contexts/MatrixClientContext";
|
|
|
|
import { RoomPermalinkCreator } from "../../../../src/utils/permalinks/Permalinks";
|
|
|
|
import { MediaEventHelper } from "../../../../src/utils/MediaEventHelper";
|
2020-01-06 13:28:29 +00:00
|
|
|
|
|
|
|
describe("<TextualBody />", () => {
|
|
|
|
afterEach(() => {
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.spyOn(MatrixClientPeg, "get").mockRestore();
|
2020-01-06 13:28:29 +00:00
|
|
|
});
|
|
|
|
|
2022-04-28 09:32:50 +00:00
|
|
|
const defaultRoom = mkStubRoom("room_id", "test room", undefined);
|
|
|
|
let defaultMatrixClient: MockedObject<MatrixClient>;
|
|
|
|
beforeEach(() => {
|
|
|
|
defaultMatrixClient = getMockClientWithEventEmitter({
|
|
|
|
getRoom: () => defaultRoom,
|
2023-02-13 11:39:16 +00:00
|
|
|
getAccountData: (): MatrixEvent | undefined => undefined,
|
2020-10-29 13:22:09 +00:00
|
|
|
isGuest: () => false,
|
2023-02-13 11:39:16 +00:00
|
|
|
mxcUrlToHttp: (s: string) => s,
|
2022-04-28 09:32:50 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const defaultEvent = mkEvent({
|
|
|
|
type: "m.room.message",
|
|
|
|
room: "room_id",
|
|
|
|
user: "sender",
|
|
|
|
content: {
|
|
|
|
body: "winks",
|
|
|
|
msgtype: "m.emote",
|
|
|
|
},
|
|
|
|
event: true,
|
|
|
|
});
|
|
|
|
const defaultProps = {
|
|
|
|
mxEvent: defaultEvent,
|
2023-02-13 11:39:16 +00:00
|
|
|
highlights: [] as string[],
|
2022-12-12 11:24:14 +00:00
|
|
|
highlightLink: "",
|
2022-04-28 09:32:50 +00:00
|
|
|
onMessageAllowed: jest.fn(),
|
|
|
|
onHeightChanged: jest.fn(),
|
|
|
|
permalinkCreator: new RoomPermalinkCreator(defaultRoom),
|
|
|
|
mediaEventHelper: {} as MediaEventHelper,
|
|
|
|
};
|
2022-12-29 16:21:00 +00:00
|
|
|
const getComponent = (props = {}, matrixClient: MatrixClient = defaultMatrixClient, renderingFn?: any) =>
|
|
|
|
(renderingFn ?? render)(
|
|
|
|
<MatrixClientContext.Provider value={matrixClient}>
|
|
|
|
<TextualBody {...defaultProps} {...props} />
|
|
|
|
</MatrixClientContext.Provider>,
|
|
|
|
);
|
2022-04-28 09:32:50 +00:00
|
|
|
|
|
|
|
it("renders m.emote correctly", () => {
|
2021-07-16 19:30:26 +00:00
|
|
|
DMRoomMap.makeShared();
|
2020-01-06 13:28:29 +00:00
|
|
|
|
|
|
|
const ev = mkEvent({
|
|
|
|
type: "m.room.message",
|
|
|
|
room: "room_id",
|
|
|
|
user: "sender",
|
|
|
|
content: {
|
|
|
|
body: "winks",
|
|
|
|
msgtype: "m.emote",
|
|
|
|
},
|
|
|
|
event: true,
|
|
|
|
});
|
|
|
|
|
2022-12-29 16:21:00 +00:00
|
|
|
const { container } = getComponent({ mxEvent: ev });
|
|
|
|
expect(container).toHaveTextContent("* sender winks");
|
|
|
|
const content = container.querySelector(".mx_EventTile_body");
|
|
|
|
expect(content).toContainHTML('<span class="mx_EventTile_body" dir="auto">winks</span>');
|
2020-01-06 13:28:29 +00:00
|
|
|
});
|
|
|
|
|
2020-01-10 00:24:13 +00:00
|
|
|
it("renders m.notice correctly", () => {
|
2021-07-16 19:30:26 +00:00
|
|
|
DMRoomMap.makeShared();
|
2020-01-06 13:28:29 +00:00
|
|
|
|
|
|
|
const ev = mkEvent({
|
|
|
|
type: "m.room.message",
|
|
|
|
room: "room_id",
|
|
|
|
user: "bot_sender",
|
|
|
|
content: {
|
|
|
|
body: "this is a notice, probably from a bot",
|
|
|
|
msgtype: "m.notice",
|
|
|
|
},
|
|
|
|
event: true,
|
|
|
|
});
|
|
|
|
|
2022-12-29 16:21:00 +00:00
|
|
|
const { container } = getComponent({ mxEvent: ev });
|
|
|
|
expect(container).toHaveTextContent(ev.getContent().body);
|
|
|
|
const content = container.querySelector(".mx_EventTile_body");
|
|
|
|
expect(content).toContainHTML(`<span class="mx_EventTile_body" dir="auto">${ev.getContent().body}</span>`);
|
2020-01-06 13:28:29 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("renders plain-text m.text correctly", () => {
|
2020-01-10 00:24:13 +00:00
|
|
|
beforeEach(() => {
|
2021-07-16 19:30:26 +00:00
|
|
|
DMRoomMap.makeShared();
|
2020-01-10 00:24:13 +00:00
|
|
|
});
|
2020-01-06 13:28:29 +00:00
|
|
|
|
2020-01-10 00:24:13 +00:00
|
|
|
it("simple message renders as expected", () => {
|
2020-01-06 13:28:29 +00:00
|
|
|
const ev = mkEvent({
|
|
|
|
type: "m.room.message",
|
|
|
|
room: "room_id",
|
|
|
|
user: "sender",
|
|
|
|
content: {
|
|
|
|
body: "this is a plaintext message",
|
|
|
|
msgtype: "m.text",
|
|
|
|
},
|
|
|
|
event: true,
|
|
|
|
});
|
|
|
|
|
2022-12-29 16:21:00 +00:00
|
|
|
const { container } = getComponent({ mxEvent: ev });
|
|
|
|
expect(container).toHaveTextContent(ev.getContent().body);
|
|
|
|
const content = container.querySelector(".mx_EventTile_body");
|
|
|
|
expect(content).toContainHTML(`<span class="mx_EventTile_body" dir="auto">${ev.getContent().body}</span>`);
|
2020-01-06 13:28:29 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// If pills were rendered within a Portal/same shadow DOM then it'd be easier to test
|
2020-01-10 00:24:13 +00:00
|
|
|
it("linkification get applied correctly into the DOM", () => {
|
2020-01-06 13:28:29 +00:00
|
|
|
const ev = mkEvent({
|
|
|
|
type: "m.room.message",
|
|
|
|
room: "room_id",
|
|
|
|
user: "sender",
|
|
|
|
content: {
|
|
|
|
body: "Visit https://matrix.org/",
|
|
|
|
msgtype: "m.text",
|
|
|
|
},
|
|
|
|
event: true,
|
|
|
|
});
|
|
|
|
|
2022-12-29 16:21:00 +00:00
|
|
|
const { container } = getComponent({ mxEvent: ev });
|
|
|
|
expect(container).toHaveTextContent(ev.getContent().body);
|
|
|
|
const content = container.querySelector(".mx_EventTile_body");
|
|
|
|
expect(content).toContainHTML(
|
2022-12-12 11:24:14 +00:00
|
|
|
'<span class="mx_EventTile_body" dir="auto">' +
|
|
|
|
'Visit <a href="https://matrix.org/" class="linkified" target="_blank" rel="noreferrer noopener">' +
|
|
|
|
"https://matrix.org/</a></span>",
|
|
|
|
);
|
2020-01-06 13:28:29 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("renders formatted m.text correctly", () => {
|
2023-02-13 11:39:16 +00:00
|
|
|
let matrixClient: MatrixClient;
|
2020-01-10 00:24:13 +00:00
|
|
|
beforeEach(() => {
|
2022-04-28 09:32:50 +00:00
|
|
|
matrixClient = getMockClientWithEventEmitter({
|
|
|
|
getRoom: () => mkStubRoom("room_id", "room name", undefined),
|
2023-02-13 11:39:16 +00:00
|
|
|
getAccountData: (): MatrixEvent | undefined => undefined,
|
2020-01-10 00:24:13 +00:00
|
|
|
getUserId: () => "@me:my_server",
|
|
|
|
getHomeserverUrl: () => "https://my_server/",
|
2023-02-13 11:39:16 +00:00
|
|
|
on: (): void => undefined,
|
|
|
|
removeListener: (): void => undefined,
|
2020-10-29 13:22:09 +00:00
|
|
|
isGuest: () => false,
|
2023-02-13 11:39:16 +00:00
|
|
|
mxcUrlToHttp: (s: string) => s,
|
2022-04-28 09:32:50 +00:00
|
|
|
});
|
2021-07-16 19:30:26 +00:00
|
|
|
DMRoomMap.makeShared();
|
2020-01-10 00:24:13 +00:00
|
|
|
});
|
2020-01-06 13:28:29 +00:00
|
|
|
|
2020-01-10 00:24:13 +00:00
|
|
|
it("italics, bold, underline and strikethrough render as expected", () => {
|
2020-01-06 13:28:29 +00:00
|
|
|
const ev = mkEvent({
|
|
|
|
type: "m.room.message",
|
|
|
|
room: "room_id",
|
|
|
|
user: "sender",
|
|
|
|
content: {
|
|
|
|
body: "foo *baz* __bar__ <del>del</del> <u>u</u>",
|
|
|
|
msgtype: "m.text",
|
|
|
|
format: "org.matrix.custom.html",
|
|
|
|
formatted_body: "foo <em>baz</em> <strong>bar</strong> <del>del</del> <u>u</u>",
|
|
|
|
},
|
|
|
|
event: true,
|
|
|
|
});
|
|
|
|
|
2022-12-29 16:21:00 +00:00
|
|
|
const { container } = getComponent({ mxEvent: ev }, matrixClient);
|
|
|
|
expect(container).toHaveTextContent("foo baz bar del u");
|
|
|
|
const content = container.querySelector(".mx_EventTile_body");
|
|
|
|
expect(content).toContainHTML(
|
2022-12-12 11:24:14 +00:00
|
|
|
'<span class="mx_EventTile_body markdown-body" dir="auto">' +
|
|
|
|
ev.getContent().formatted_body +
|
|
|
|
"</span>",
|
|
|
|
);
|
2020-01-06 13:28:29 +00:00
|
|
|
});
|
|
|
|
|
2020-01-10 00:24:13 +00:00
|
|
|
it("spoilers get injected properly into the DOM", () => {
|
2020-01-06 13:28:29 +00:00
|
|
|
const ev = mkEvent({
|
|
|
|
type: "m.room.message",
|
|
|
|
room: "room_id",
|
|
|
|
user: "sender",
|
|
|
|
content: {
|
|
|
|
body: "Hey [Spoiler for movie](mxc://someserver/somefile)",
|
|
|
|
msgtype: "m.text",
|
|
|
|
format: "org.matrix.custom.html",
|
2022-12-12 11:24:14 +00:00
|
|
|
formatted_body: 'Hey <span data-mx-spoiler="movie">the movie was awesome</span>',
|
2020-01-06 13:28:29 +00:00
|
|
|
},
|
|
|
|
event: true,
|
|
|
|
});
|
|
|
|
|
2022-12-29 16:21:00 +00:00
|
|
|
const { container } = getComponent({ mxEvent: ev }, matrixClient);
|
|
|
|
expect(container).toHaveTextContent("Hey (movie) the movie was awesome");
|
|
|
|
const content = container.querySelector(".mx_EventTile_body");
|
|
|
|
expect(content).toContainHTML(
|
2022-12-12 11:24:14 +00:00
|
|
|
'<span class="mx_EventTile_body markdown-body" dir="auto">' +
|
|
|
|
"Hey <span>" +
|
|
|
|
'<span class="mx_EventTile_spoiler">' +
|
|
|
|
'<span class="mx_EventTile_spoiler_reason">(movie)</span> ' +
|
|
|
|
'<span class="mx_EventTile_spoiler_content"><span>the movie was awesome</span></span>' +
|
|
|
|
"</span></span></span>",
|
|
|
|
);
|
2020-01-06 13:28:29 +00:00
|
|
|
});
|
|
|
|
|
2022-05-03 00:26:37 +00:00
|
|
|
it("linkification is not applied to code blocks", () => {
|
|
|
|
const ev = mkEvent({
|
|
|
|
type: "m.room.message",
|
|
|
|
room: "room_id",
|
|
|
|
user: "sender",
|
|
|
|
content: {
|
|
|
|
body: "Visit `https://matrix.org/`\n```\nhttps://matrix.org/\n```",
|
|
|
|
msgtype: "m.text",
|
|
|
|
format: "org.matrix.custom.html",
|
|
|
|
formatted_body: "<p>Visit <code>https://matrix.org/</code></p>\n<pre>https://matrix.org/\n</pre>\n",
|
|
|
|
},
|
|
|
|
event: true,
|
|
|
|
});
|
|
|
|
|
2022-12-29 16:21:00 +00:00
|
|
|
const { container } = getComponent({ mxEvent: ev }, matrixClient);
|
|
|
|
expect(container).toHaveTextContent("Visit https://matrix.org/ 1https://matrix.org/");
|
|
|
|
const content = container.querySelector(".mx_EventTile_body");
|
|
|
|
expect(content).toMatchSnapshot();
|
2022-05-03 00:26:37 +00:00
|
|
|
});
|
|
|
|
|
2020-01-06 13:28:29 +00:00
|
|
|
// If pills were rendered within a Portal/same shadow DOM then it'd be easier to test
|
2020-01-10 00:24:13 +00:00
|
|
|
it("pills get injected correctly into the DOM", () => {
|
2020-01-06 13:28:29 +00:00
|
|
|
const ev = mkEvent({
|
|
|
|
type: "m.room.message",
|
|
|
|
room: "room_id",
|
|
|
|
user: "sender",
|
|
|
|
content: {
|
|
|
|
body: "Hey User",
|
|
|
|
msgtype: "m.text",
|
|
|
|
format: "org.matrix.custom.html",
|
2022-12-12 11:24:14 +00:00
|
|
|
formatted_body: 'Hey <a href="https://matrix.to/#/@user:server">Member</a>',
|
2020-01-06 13:28:29 +00:00
|
|
|
},
|
|
|
|
event: true,
|
|
|
|
});
|
|
|
|
|
2022-12-29 16:21:00 +00:00
|
|
|
const { container } = getComponent({ mxEvent: ev }, matrixClient);
|
|
|
|
expect(container).toHaveTextContent("Hey Member");
|
|
|
|
const content = container.querySelector(".mx_EventTile_body");
|
|
|
|
expect(content).toMatchSnapshot();
|
2020-01-06 13:28:29 +00:00
|
|
|
});
|
2021-01-27 11:46:20 +00:00
|
|
|
|
2022-02-22 11:46:34 +00:00
|
|
|
it("pills do not appear in code blocks", () => {
|
|
|
|
const ev = mkEvent({
|
|
|
|
type: "m.room.message",
|
|
|
|
room: "room_id",
|
|
|
|
user: "sender",
|
|
|
|
content: {
|
|
|
|
body: "`@room`\n```\n@room\n```",
|
|
|
|
msgtype: "m.text",
|
|
|
|
format: "org.matrix.custom.html",
|
|
|
|
formatted_body: "<p><code>@room</code></p>\n<pre><code>@room\n</code></pre>\n",
|
|
|
|
},
|
|
|
|
event: true,
|
|
|
|
});
|
|
|
|
|
2022-12-29 16:21:00 +00:00
|
|
|
const { container } = getComponent({ mxEvent: ev });
|
|
|
|
expect(container).toHaveTextContent("@room 1@room");
|
|
|
|
const content = container.querySelector(".mx_EventTile_body");
|
|
|
|
expect(content).toMatchSnapshot();
|
2022-02-22 11:46:34 +00:00
|
|
|
});
|
|
|
|
|
2021-01-27 11:46:20 +00:00
|
|
|
it("pills do not appear for event permalinks", () => {
|
|
|
|
const ev = mkEvent({
|
|
|
|
type: "m.room.message",
|
|
|
|
room: "room_id",
|
|
|
|
user: "sender",
|
|
|
|
content: {
|
|
|
|
body:
|
|
|
|
"An [event link](https://matrix.to/#/!ZxbRYPQXDXKGmDnJNg:example.com/" +
|
|
|
|
"$16085560162aNpaH:example.com?via=example.com) with text",
|
|
|
|
msgtype: "m.text",
|
|
|
|
format: "org.matrix.custom.html",
|
|
|
|
formatted_body:
|
2022-12-12 11:24:14 +00:00
|
|
|
'An <a href="https://matrix.to/#/!ZxbRYPQXDXKGmDnJNg:example.com/' +
|
|
|
|
'$16085560162aNpaH:example.com?via=example.com">event link</a> with text',
|
2021-01-27 11:46:20 +00:00
|
|
|
},
|
|
|
|
event: true,
|
|
|
|
});
|
|
|
|
|
2022-12-29 16:21:00 +00:00
|
|
|
const { container } = getComponent({ mxEvent: ev }, matrixClient);
|
|
|
|
expect(container).toHaveTextContent("An event link with text");
|
|
|
|
const content = container.querySelector(".mx_EventTile_body");
|
|
|
|
expect(content).toContainHTML(
|
2021-01-27 11:46:20 +00:00
|
|
|
'<span class="mx_EventTile_body markdown-body" dir="auto">' +
|
2022-12-12 11:24:14 +00:00
|
|
|
'An <a href="https://matrix.to/#/!ZxbRYPQXDXKGmDnJNg:example.com/' +
|
|
|
|
'$16085560162aNpaH:example.com?via=example.com" ' +
|
|
|
|
'rel="noreferrer noopener">event link</a> with text</span>',
|
2021-01-27 11:46:20 +00:00
|
|
|
);
|
|
|
|
});
|
2021-02-03 15:18:19 +00:00
|
|
|
|
|
|
|
it("pills appear for room links with vias", () => {
|
|
|
|
const ev = mkEvent({
|
|
|
|
type: "m.room.message",
|
|
|
|
room: "room_id",
|
|
|
|
user: "sender",
|
|
|
|
content: {
|
|
|
|
body:
|
|
|
|
"A [room link](https://matrix.to/#/!ZxbRYPQXDXKGmDnJNg:example.com" +
|
|
|
|
"?via=example.com&via=bob.com) with vias",
|
|
|
|
msgtype: "m.text",
|
|
|
|
format: "org.matrix.custom.html",
|
|
|
|
formatted_body:
|
2022-12-12 11:24:14 +00:00
|
|
|
'A <a href="https://matrix.to/#/!ZxbRYPQXDXKGmDnJNg:example.com' +
|
|
|
|
'?via=example.com&via=bob.com">room link</a> with vias',
|
2021-02-03 15:18:19 +00:00
|
|
|
},
|
|
|
|
event: true,
|
|
|
|
});
|
|
|
|
|
2022-12-29 16:21:00 +00:00
|
|
|
const { container } = getComponent({ mxEvent: ev }, matrixClient);
|
|
|
|
expect(container).toHaveTextContent("A room name with vias");
|
|
|
|
const content = container.querySelector(".mx_EventTile_body");
|
|
|
|
expect(content).toContainHTML(
|
2021-02-03 15:18:19 +00:00
|
|
|
'<span class="mx_EventTile_body markdown-body" dir="auto">' +
|
2022-12-12 11:24:14 +00:00
|
|
|
'A <span><bdi><a class="mx_Pill mx_RoomPill" ' +
|
|
|
|
'href="https://matrix.to/#/!ZxbRYPQXDXKGmDnJNg:example.com' +
|
|
|
|
'?via=example.com&via=bob.com"' +
|
|
|
|
'><img class="mx_BaseAvatar mx_BaseAvatar_image" ' +
|
|
|
|
'src="mxc://avatar.url/room.png" ' +
|
|
|
|
'style="width: 16px; height: 16px;" alt="" data-testid="avatar-img" aria-hidden="true">' +
|
|
|
|
'<span class="mx_Pill_linkText">room name</span></a></bdi></span> with vias</span>',
|
2021-02-03 15:18:19 +00:00
|
|
|
);
|
|
|
|
});
|
2021-12-02 09:25:12 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders formatted body without html corretly", () => {
|
2021-12-02 09:25:12 +00:00
|
|
|
const ev = mkEvent({
|
|
|
|
type: "m.room.message",
|
|
|
|
room: "room_id",
|
|
|
|
user: "sender",
|
|
|
|
content: {
|
|
|
|
body: "escaped \\*markdown\\*",
|
|
|
|
msgtype: "m.text",
|
|
|
|
format: "org.matrix.custom.html",
|
|
|
|
formatted_body: "escaped *markdown*",
|
|
|
|
},
|
|
|
|
event: true,
|
|
|
|
});
|
|
|
|
|
2022-12-29 16:21:00 +00:00
|
|
|
const { container } = getComponent({ mxEvent: ev }, matrixClient);
|
2021-12-02 09:25:12 +00:00
|
|
|
|
2022-12-29 16:21:00 +00:00
|
|
|
const content = container.querySelector(".mx_EventTile_body");
|
|
|
|
expect(content).toContainHTML(
|
2022-12-12 11:24:14 +00:00
|
|
|
'<span class="mx_EventTile_body" dir="auto">' + "escaped *markdown*" + "</span>",
|
2021-12-02 09:25:12 +00:00
|
|
|
);
|
|
|
|
});
|
2020-01-06 13:28:29 +00:00
|
|
|
});
|
2020-01-06 14:38:21 +00:00
|
|
|
|
2020-01-10 00:24:13 +00:00
|
|
|
it("renders url previews correctly", () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
languageHandler.setMissingEntryGenerator((key) => key.split("|", 2)[1]);
|
2020-01-06 14:38:21 +00:00
|
|
|
|
2022-04-28 09:32:50 +00:00
|
|
|
const matrixClient = getMockClientWithEventEmitter({
|
|
|
|
getRoom: () => mkStubRoom("room_id", "room name", undefined),
|
2023-02-13 11:39:16 +00:00
|
|
|
getAccountData: (): MatrixClient | undefined => undefined,
|
|
|
|
getUrlPreview: (url: string) => new Promise(() => {}),
|
2021-01-14 17:39:58 +00:00
|
|
|
isGuest: () => false,
|
2023-02-13 11:39:16 +00:00
|
|
|
mxcUrlToHttp: (s: string) => s,
|
2022-04-28 09:32:50 +00:00
|
|
|
});
|
2021-07-16 19:30:26 +00:00
|
|
|
DMRoomMap.makeShared();
|
2020-01-06 14:38:21 +00:00
|
|
|
|
|
|
|
const ev = mkEvent({
|
|
|
|
type: "m.room.message",
|
|
|
|
room: "room_id",
|
|
|
|
user: "sender",
|
|
|
|
content: {
|
|
|
|
body: "Visit https://matrix.org/",
|
|
|
|
msgtype: "m.text",
|
|
|
|
},
|
|
|
|
event: true,
|
|
|
|
});
|
|
|
|
|
2022-12-29 16:21:00 +00:00
|
|
|
const { container, rerender } = getComponent(
|
|
|
|
{ mxEvent: ev, showUrlPreview: true, onHeightChanged: jest.fn() },
|
|
|
|
matrixClient,
|
|
|
|
);
|
2020-01-06 14:38:21 +00:00
|
|
|
|
2022-12-29 16:21:00 +00:00
|
|
|
expect(container).toHaveTextContent(ev.getContent().body);
|
|
|
|
expect(container.querySelector("a")).toHaveAttribute("href", "https://matrix.org/");
|
2020-01-06 14:38:21 +00:00
|
|
|
|
|
|
|
// simulate an event edit and check the transition from the old URL preview to the new one
|
|
|
|
const ev2 = mkEvent({
|
|
|
|
type: "m.room.message",
|
|
|
|
room: "room_id",
|
|
|
|
user: "sender",
|
|
|
|
content: {
|
|
|
|
"m.new_content": {
|
|
|
|
body: "Visit https://vector.im/ and https://riot.im/",
|
|
|
|
msgtype: "m.text",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
event: true,
|
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.spyOn(ev, "replacingEventDate").mockReturnValue(new Date(1993, 7, 3));
|
2020-01-06 14:38:21 +00:00
|
|
|
ev.makeReplaced(ev2);
|
|
|
|
|
2022-12-29 16:21:00 +00:00
|
|
|
getComponent(
|
|
|
|
{ mxEvent: ev, showUrlPreview: true, onHeightChanged: jest.fn(), replacingEventId: ev.getId() },
|
|
|
|
matrixClient,
|
|
|
|
rerender,
|
2022-12-12 11:24:14 +00:00
|
|
|
);
|
2022-12-29 16:21:00 +00:00
|
|
|
|
|
|
|
expect(container).toHaveTextContent(ev2.getContent()["m.new_content"].body + "(edited)");
|
|
|
|
|
|
|
|
const links = ["https://vector.im/", "https://riot.im/"];
|
|
|
|
const anchorNodes = container.querySelectorAll("a");
|
|
|
|
Array.from(anchorNodes).forEach((node, index) => {
|
|
|
|
expect(node).toHaveAttribute("href", links[index]);
|
|
|
|
});
|
2020-01-06 14:38:21 +00:00
|
|
|
});
|
2020-01-06 13:28:29 +00:00
|
|
|
});
|