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

Commit 1b9f571

Browse files
committed
Add tests
1 parent 77734e7 commit 1b9f571

File tree

3 files changed

+169
-10
lines changed

3 files changed

+169
-10
lines changed

src/utils/Reply.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ import { IContent, MatrixEvent } from "matrix-js-sdk/src/models/event";
1818
import sanitizeHtml from "sanitize-html";
1919
import escapeHtml from "escape-html";
2020
import { THREAD_RELATION_TYPE } from "matrix-js-sdk/src/models/thread";
21+
import { MsgType } from "matrix-js-sdk/src/@types/event";
2122

2223
import { PERMITTED_URL_SCHEMES } from "../HtmlUtils";
2324
import { makeUserPermalink, RoomPermalinkCreator } from "./permalinks/Permalinks";
2425
import { RecursivePartial } from "../@types/common";
2526
import SettingsStore from "../settings/SettingsStore";
2627

27-
export function getParentEventId(ev: MatrixEvent): string | undefined {
28+
export function getParentEventId(ev?: MatrixEvent): string | undefined {
2829
if (!ev || ev.isRedacted()) return;
2930
if (ev.replyEventId) {
3031
return ev.replyEventId;
@@ -70,7 +71,7 @@ export function getNestedReplyText(
7071
): { body: string, html: string } | null {
7172
if (!ev) return null;
7273

73-
let { body, formatted_body: html } = ev.getContent();
74+
let { body, formatted_body: html, msgtype } = ev.getContent();
7475
if (getParentEventId(ev)) {
7576
if (body) body = stripPlainReply(body);
7677
}
@@ -94,9 +95,9 @@ export function getNestedReplyText(
9495
const mxid = ev.getSender();
9596

9697
// This fallback contains text that is explicitly EN.
97-
switch (ev.getContent().msgtype) {
98-
case 'm.text':
99-
case 'm.notice': {
98+
switch (msgtype) {
99+
case MsgType.Text:
100+
case MsgType.Notice: {
100101
html = `<mx-reply><blockquote><a href="${evLink}">In reply to</a> <a href="${userLink}">${mxid}</a>`
101102
+ `<br>${html}</blockquote></mx-reply>`;
102103
const lines = body.trim().split('\n');
@@ -106,27 +107,27 @@ export function getNestedReplyText(
106107
}
107108
break;
108109
}
109-
case 'm.image':
110+
case MsgType.Image:
110111
html = `<mx-reply><blockquote><a href="${evLink}">In reply to</a> <a href="${userLink}">${mxid}</a>`
111112
+ `<br>sent an image.</blockquote></mx-reply>`;
112113
body = `> <${mxid}> sent an image.\n\n`;
113114
break;
114-
case 'm.video':
115+
case MsgType.Video:
115116
html = `<mx-reply><blockquote><a href="${evLink}">In reply to</a> <a href="${userLink}">${mxid}</a>`
116117
+ `<br>sent a video.</blockquote></mx-reply>`;
117118
body = `> <${mxid}> sent a video.\n\n`;
118119
break;
119-
case 'm.audio':
120+
case MsgType.Audio:
120121
html = `<mx-reply><blockquote><a href="${evLink}">In reply to</a> <a href="${userLink}">${mxid}</a>`
121122
+ `<br>sent an audio file.</blockquote></mx-reply>`;
122123
body = `> <${mxid}> sent an audio file.\n\n`;
123124
break;
124-
case 'm.file':
125+
case MsgType.File:
125126
html = `<mx-reply><blockquote><a href="${evLink}">In reply to</a> <a href="${userLink}">${mxid}</a>`
126127
+ `<br>sent a file.</blockquote></mx-reply>`;
127128
body = `> <${mxid}> sent a file.\n\n`;
128129
break;
129-
case 'm.emote': {
130+
case MsgType.Emote: {
130131
html = `<mx-reply><blockquote><a href="${evLink}">In reply to</a> * `
131132
+ `<a href="${userLink}">${mxid}</a><br>${html}</blockquote></mx-reply>`;
132133
const lines = body.trim().split('\n');

test/Reply-test.ts

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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 './skinned-sdk';
18+
import {
19+
getNestedReplyText,
20+
getParentEventId,
21+
shouldDisplayReply,
22+
stripHTMLReply,
23+
stripPlainReply,
24+
} from "../src/utils/Reply";
25+
import { mkEvent } from "./test-utils";
26+
import { RoomPermalinkCreator } from "../src/utils/permalinks/Permalinks";
27+
28+
// don't litter test console with logs
29+
jest.mock("matrix-js-sdk/src/logger");
30+
31+
describe("Reply", () => {
32+
describe("getParentEventId", () => {
33+
it("returns undefined if given a falsey value", async () => {
34+
expect(getParentEventId()).toBeUndefined();
35+
});
36+
it("returns undefined if given a redacted event", async () => {
37+
const event = mkEvent({
38+
event: true,
39+
type: "m.room.message",
40+
user: "@user1:server",
41+
room: "!room1:server",
42+
content: {},
43+
});
44+
event.makeRedacted(event);
45+
46+
expect(getParentEventId(event)).toBeUndefined();
47+
});
48+
it("returns undefined if the given event is not a reply", async () => {
49+
const event = mkEvent({
50+
event: true,
51+
type: "m.room.message",
52+
user: "@user1:server",
53+
room: "!room1:server",
54+
content: {},
55+
});
56+
57+
expect(getParentEventId(event)).toBeUndefined();
58+
});
59+
it("returns id of the event being replied to", async () => {
60+
const event = mkEvent({
61+
event: true,
62+
type: "m.room.message",
63+
user: "@user1:server",
64+
room: "!room1:server",
65+
content: {
66+
"m.relates_to": {
67+
"m.in_reply_to": {
68+
"event_id": "$event1",
69+
},
70+
},
71+
},
72+
});
73+
74+
expect(getParentEventId(event)).toBe("$event1");
75+
});
76+
});
77+
78+
describe("stripPlainReply", () => {
79+
it("Removes leading quotes until the first blank line", () => {
80+
expect(stripPlainReply(`
81+
> This is part
82+
> of the quote
83+
84+
But this is not
85+
`.trim())).toBe("But this is not");
86+
});
87+
});
88+
89+
describe("stripHTMLReply", () => {
90+
it("Removes <mx-reply> from the input", () => {
91+
expect(stripHTMLReply(`
92+
<mx-reply>
93+
This is part
94+
of the quote
95+
</mx-reply>
96+
But this is not
97+
`).trim()).toBe("But this is not");
98+
});
99+
});
100+
101+
describe("getNestedReplyText", () => {
102+
it("Returns valid reply fallback text for m.text msgtypes", () => {
103+
const event = mkEvent({
104+
event: true,
105+
type: "m.room.message",
106+
user: "@user1:server",
107+
room: "!room1:server",
108+
content: {
109+
body: "body",
110+
msgtype: "m.text",
111+
},
112+
});
113+
114+
expect(getNestedReplyText(event, {
115+
forEvent(eventId: string): string {
116+
return "$$permalink$$";
117+
},
118+
} as RoomPermalinkCreator)).toMatchSnapshot();
119+
});
120+
});
121+
122+
describe("shouldDisplayReply", () => {
123+
it("Returns false for redacted events", () => {
124+
const event = mkEvent({
125+
event: true,
126+
type: "m.room.message",
127+
user: "@user1:server",
128+
room: "!room1:server",
129+
content: {},
130+
});
131+
event.makeRedacted(event);
132+
133+
expect(shouldDisplayReply(event)).toBe(false);
134+
});
135+
136+
it("Returns false for non-reply events", () => {
137+
const event = mkEvent({
138+
event: true,
139+
type: "m.room.message",
140+
user: "@user1:server",
141+
room: "!room1:server",
142+
content: {},
143+
});
144+
145+
expect(shouldDisplayReply(event)).toBe(false);
146+
});
147+
});
148+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Reply getNestedReplyText Returns valid reply fallback text for m.text msgtypes 1`] = `
4+
Object {
5+
"body": "> <@user1:server> body
6+
7+
",
8+
"html": "<mx-reply><blockquote><a href=\\"$$permalink$$\\">In reply to</a> <a href=\\"https://matrix.to/#/@user1:server\\">@user1:server</a><br>body</blockquote></mx-reply>",
9+
}
10+
`;

0 commit comments

Comments
 (0)