|
| 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 React from "react"; |
| 18 | +import { render } from "@testing-library/react"; |
| 19 | +import { MatrixEvent } from "matrix-js-sdk/src/models/event"; |
| 20 | +import { ConditionKind, EventType, PushRuleActionName, Room, TweakName } from "matrix-js-sdk/src/matrix"; |
| 21 | + |
| 22 | +import { pillifyLinks } from "../../src/utils/pillify"; |
| 23 | +import { stubClient } from "../test-utils"; |
| 24 | +import { MatrixClientPeg } from "../../src/MatrixClientPeg"; |
| 25 | +import DMRoomMap from "../../src/utils/DMRoomMap"; |
| 26 | + |
| 27 | +describe("pillify", () => { |
| 28 | + const roomId = "!room:id"; |
| 29 | + const event = new MatrixEvent({ |
| 30 | + room_id: roomId, |
| 31 | + type: EventType.RoomMessage, |
| 32 | + content: { |
| 33 | + body: "@room", |
| 34 | + }, |
| 35 | + }); |
| 36 | + |
| 37 | + beforeEach(() => { |
| 38 | + stubClient(); |
| 39 | + const cli = MatrixClientPeg.get(); |
| 40 | + (cli.getRoom as jest.Mock).mockReturnValue(new Room(roomId, cli, cli.getUserId())); |
| 41 | + cli.pushRules.global = { |
| 42 | + override: [ |
| 43 | + { |
| 44 | + rule_id: ".m.rule.roomnotif", |
| 45 | + default: true, |
| 46 | + enabled: true, |
| 47 | + conditions: [{ |
| 48 | + kind: ConditionKind.EventMatch, |
| 49 | + key: "content.body", |
| 50 | + pattern: "@room", |
| 51 | + }], |
| 52 | + actions: [ |
| 53 | + PushRuleActionName.Notify, |
| 54 | + { |
| 55 | + set_tweak: TweakName.Highlight, |
| 56 | + value: true, |
| 57 | + }, |
| 58 | + ], |
| 59 | + }, |
| 60 | + ], |
| 61 | + }; |
| 62 | + |
| 63 | + DMRoomMap.makeShared(); |
| 64 | + }); |
| 65 | + |
| 66 | + it("should do nothing for empty element", () => { |
| 67 | + const { container } = render(<div />); |
| 68 | + const originalHtml = container.outerHTML; |
| 69 | + const containers: Element[] = []; |
| 70 | + pillifyLinks([container], event, containers); |
| 71 | + expect(containers).toHaveLength(0); |
| 72 | + expect(container.outerHTML).toEqual(originalHtml); |
| 73 | + }); |
| 74 | + |
| 75 | + it("should pillify @room", () => { |
| 76 | + const { container } = render(<div>@room</div>); |
| 77 | + const containers: Element[] = []; |
| 78 | + pillifyLinks([container], event, containers); |
| 79 | + expect(containers).toHaveLength(1); |
| 80 | + expect(container.querySelector(".mx_Pill.mx_AtRoomPill").textContent).toBe("!@room"); |
| 81 | + }); |
| 82 | + |
| 83 | + it("should not double up pillification on repeated calls", () => { |
| 84 | + const { container } = render(<div>@room</div>); |
| 85 | + const containers: Element[] = []; |
| 86 | + pillifyLinks([container], event, containers); |
| 87 | + pillifyLinks([container], event, containers); |
| 88 | + pillifyLinks([container], event, containers); |
| 89 | + pillifyLinks([container], event, containers); |
| 90 | + expect(containers).toHaveLength(1); |
| 91 | + expect(container.querySelector(".mx_Pill.mx_AtRoomPill").textContent).toBe("!@room"); |
| 92 | + }); |
| 93 | +}); |
0 commit comments