|
| 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 | +}); |
0 commit comments