|
| 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 { Feature, ServerSupport } from "matrix-js-sdk/src/feature"; |
| 18 | +import { MatrixClient, MatrixEvent, RelationType } from "matrix-js-sdk/src/matrix"; |
| 19 | +import { screen } from "@testing-library/react"; |
| 20 | +import userEvent from "@testing-library/user-event"; |
| 21 | + |
| 22 | +import { flushPromises, mkEvent, stubClient } from "../../../test-utils"; |
| 23 | +import { mkVoiceBroadcastInfoStateEvent } from "../../../voice-broadcast/utils/test-utils"; |
| 24 | +import { VoiceBroadcastInfoState } from "../../../../src/voice-broadcast"; |
| 25 | +import { createRedactEventDialog } from "../../../../src/components/views/dialogs/ConfirmRedactDialog"; |
| 26 | + |
| 27 | +describe("ConfirmRedactDialog", () => { |
| 28 | + const roomId = "!room:example.com"; |
| 29 | + let client: MatrixClient; |
| 30 | + let mxEvent: MatrixEvent; |
| 31 | + |
| 32 | + const setUpVoiceBroadcastStartedEvent = () => { |
| 33 | + mxEvent = mkVoiceBroadcastInfoStateEvent( |
| 34 | + roomId, |
| 35 | + VoiceBroadcastInfoState.Started, |
| 36 | + client.getUserId()!, |
| 37 | + client.deviceId!, |
| 38 | + ); |
| 39 | + }; |
| 40 | + |
| 41 | + const confirmDeleteVoiceBroadcastStartedEvent = async () => { |
| 42 | + createRedactEventDialog({ mxEvent }); |
| 43 | + // double-flush promises required for the dialog to show up |
| 44 | + await flushPromises(); |
| 45 | + await flushPromises(); |
| 46 | + |
| 47 | + await userEvent.click(screen.getByTestId("dialog-primary-button")); |
| 48 | + }; |
| 49 | + |
| 50 | + beforeEach(() => { |
| 51 | + client = stubClient(); |
| 52 | + }); |
| 53 | + |
| 54 | + it("should raise an error for an event without ID", async () => { |
| 55 | + mxEvent = mkEvent({ |
| 56 | + event: true, |
| 57 | + type: "m.room.message", |
| 58 | + room: roomId, |
| 59 | + content: {}, |
| 60 | + user: client.getSafeUserId(), |
| 61 | + }); |
| 62 | + jest.spyOn(mxEvent, "getId").mockReturnValue(undefined); |
| 63 | + expect(async () => { |
| 64 | + await confirmDeleteVoiceBroadcastStartedEvent(); |
| 65 | + }).rejects.toThrow("cannot redact event without ID"); |
| 66 | + }); |
| 67 | + |
| 68 | + it("should raise an error for an event without room-ID", async () => { |
| 69 | + mxEvent = mkEvent({ |
| 70 | + event: true, |
| 71 | + type: "m.room.message", |
| 72 | + room: roomId, |
| 73 | + content: {}, |
| 74 | + user: client.getSafeUserId(), |
| 75 | + }); |
| 76 | + jest.spyOn(mxEvent, "getRoomId").mockReturnValue(undefined); |
| 77 | + expect(async () => { |
| 78 | + await confirmDeleteVoiceBroadcastStartedEvent(); |
| 79 | + }).rejects.toThrow(`cannot redact event ${mxEvent.getId()} without room ID`); |
| 80 | + }); |
| 81 | + |
| 82 | + describe("when redacting a voice broadcast started event", () => { |
| 83 | + beforeEach(() => { |
| 84 | + setUpVoiceBroadcastStartedEvent(); |
| 85 | + }); |
| 86 | + |
| 87 | + describe("and the server does not support relation based redactions", () => { |
| 88 | + beforeEach(() => { |
| 89 | + client.canSupport.set(Feature.RelationBasedRedactions, ServerSupport.Unsupported); |
| 90 | + }); |
| 91 | + |
| 92 | + describe("and displaying and confirm the dialog for a voice broadcast", () => { |
| 93 | + beforeEach(async () => { |
| 94 | + await confirmDeleteVoiceBroadcastStartedEvent(); |
| 95 | + }); |
| 96 | + |
| 97 | + it("should call redact without `with_relations`", () => { |
| 98 | + expect(client.redactEvent).toHaveBeenCalledWith(roomId, mxEvent.getId(), undefined, {}); |
| 99 | + }); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + describe("and the server supports relation based redactions", () => { |
| 104 | + beforeEach(() => { |
| 105 | + client.canSupport.set(Feature.RelationBasedRedactions, ServerSupport.Unstable); |
| 106 | + }); |
| 107 | + |
| 108 | + describe("and displaying and confirm the dialog for a voice broadcast", () => { |
| 109 | + beforeEach(async () => { |
| 110 | + await confirmDeleteVoiceBroadcastStartedEvent(); |
| 111 | + }); |
| 112 | + |
| 113 | + it("should call redact with `with_relations`", () => { |
| 114 | + expect(client.redactEvent).toHaveBeenCalledWith(roomId, mxEvent.getId(), undefined, { |
| 115 | + with_relations: [RelationType.Reference], |
| 116 | + }); |
| 117 | + }); |
| 118 | + }); |
| 119 | + }); |
| 120 | + }); |
| 121 | +}); |
0 commit comments