From b8a402010242dd454bff0b28969ad569bbe06491 Mon Sep 17 00:00:00 2001 From: R Midhun Suresh Date: Tue, 26 Aug 2025 14:08:10 +0530 Subject: [PATCH] Write tests --- .../event-tiles/E2ePadlockViewModel-test.ts | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 test/viewmodels/event-tiles/E2ePadlockViewModel-test.ts diff --git a/test/viewmodels/event-tiles/E2ePadlockViewModel-test.ts b/test/viewmodels/event-tiles/E2ePadlockViewModel-test.ts new file mode 100644 index 0000000000..5af6c7682b --- /dev/null +++ b/test/viewmodels/event-tiles/E2ePadlockViewModel-test.ts @@ -0,0 +1,64 @@ +/* +Copyright 2025 New Vector Ltd. + +SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial +Please see LICENSE files in the repository root for full details. +*/ + +import { mkEvent, stubClient } from "../../test-utils"; +import { E2ePadlockViewModel } from "../../../src/viewmodels/event-tile/E2ePadlockViewModel"; +import { isLocalRoom } from "../../../src/utils/localRoom/isLocalRoom"; +import type { Mock } from "jest-mock"; + +jest.mock("../../../src/utils/localRoom/isLocalRoom"); + +describe("E2ePadlockViewModel", () => { + it("should have initial state with noShield = true", () => { + const event = mkEvent({ + type: "m.room.message", + user: "foo@matrix.org", + content: { + body: "This is a message", + msgtype: "m.text", + }, + }); + const cli = stubClient(); + const isRoomEncrypted = true; + const vm = new E2ePadlockViewModel({ event, cli, isRoomEncrypted }); + expect(vm.getSnapshot()).toStrictEqual({ noShield: true }); + }); + + it("should have state with noShield = true for local room", async () => { + (isLocalRoom as Mock).mockImplementation(() => true); + const event = mkEvent({ + event: true, + type: "m.room.message", + user: "foo@matrix.org", + content: { + body: "This is a message", + msgtype: "m.text", + }, + }); + const cli = stubClient(); + const isRoomEncrypted = true; + const vm = new E2ePadlockViewModel({ event, cli, isRoomEncrypted }); + await vm.verifyEvent(); + expect(vm.getSnapshot()).toStrictEqual({ noShield: true }); + jest.restoreAllMocks(); + }); + + it("should not show padlock for events expected to be unencrypted", async () => { + const event = mkEvent({ + event: true, + type: "m.foo", + user: "foo@matrix.org", + content: {}, + skey: "foo", + }); + const cli = stubClient(); + const isRoomEncrypted = true; + const vm = new E2ePadlockViewModel({ event, cli, isRoomEncrypted }); + await vm.verifyEvent(); + expect(vm.getSnapshot()).toStrictEqual({ noShield: true }); + }); +});