element-web/apps/web/test/viewmodels/message-body/DecryptionFailureBodyViewModel-test.tsx
Zack 4f3a1a2cc6
Refactor and Move RedactedBodyView To Shared Components (#32772)
* refactoring and creation of shared-components for reductedBodyView

* move redacted message rendering to shared MVVM view

* Update snapshots + fix lint errors

* Remove MatrixClientPeg and use reguler react matrix client context

* Stop resyncing redacted body view models with mxEvent

* Fix redacted_because test fixtures for stricter event typing

* Simplify redacted body client access

* Watch timestamp setting in redacted body view model

* Refactor redacted and decryption failure body factories into MBodyFactory

* Prettier Fix

* Refactor FileBody into same pattern for consitancy
2026-03-24 10:02:07 +00:00

101 lines
3.9 KiB
TypeScript

/*
* Copyright 2026 Element Creations 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 { DecryptionFailureCode } from "matrix-js-sdk/src/crypto-api";
import { DecryptionFailureReason } from "@element-hq/web-shared-components";
import { DecryptionFailureBodyViewModel } from "../../../src/viewmodels/message-body/DecryptionFailureBodyViewModel";
describe("DecryptionFailureBodyViewModel", () => {
it("should return the snapshot", () => {
const vm = new DecryptionFailureBodyViewModel({
decryptionFailureCode: null,
verificationState: true,
});
expect(vm.getSnapshot()).toMatchObject({
decryptionFailureReason: DecryptionFailureReason.UNABLE_TO_DECRYPT,
isLocalDeviceVerified: true,
});
});
it.each([
{
code: DecryptionFailureCode.HISTORICAL_MESSAGE_BACKUP_UNCONFIGURED,
reason: DecryptionFailureReason.HISTORICAL_MESSAGE_BACKUP_UNCONFIGURED,
},
{
code: DecryptionFailureCode.HISTORICAL_MESSAGE_NO_KEY_BACKUP,
reason: DecryptionFailureReason.HISTORICAL_MESSAGE_NO_KEY_BACKUP,
},
{
code: DecryptionFailureCode.HISTORICAL_MESSAGE_USER_NOT_JOINED,
reason: DecryptionFailureReason.HISTORICAL_MESSAGE_USER_NOT_JOINED,
},
{
code: DecryptionFailureCode.MEGOLM_KEY_WITHHELD,
reason: DecryptionFailureReason.UNABLE_TO_DECRYPT,
},
{
code: DecryptionFailureCode.MEGOLM_KEY_WITHHELD_FOR_UNVERIFIED_DEVICE,
reason: DecryptionFailureReason.MEGOLM_KEY_WITHHELD_FOR_UNVERIFIED_DEVICE,
},
{
code: DecryptionFailureCode.MEGOLM_UNKNOWN_INBOUND_SESSION_ID,
reason: DecryptionFailureReason.UNABLE_TO_DECRYPT,
},
{
code: DecryptionFailureCode.OLM_UNKNOWN_MESSAGE_INDEX,
reason: DecryptionFailureReason.UNABLE_TO_DECRYPT,
},
{
code: DecryptionFailureCode.SENDER_IDENTITY_PREVIOUSLY_VERIFIED,
reason: DecryptionFailureReason.SENDER_IDENTITY_PREVIOUSLY_VERIFIED,
},
{
code: DecryptionFailureCode.UNKNOWN_ERROR,
reason: DecryptionFailureReason.UNABLE_TO_DECRYPT,
},
{
code: DecryptionFailureCode.UNKNOWN_SENDER_DEVICE,
reason: DecryptionFailureReason.UNABLE_TO_DECRYPT,
},
{
code: DecryptionFailureCode.UNSIGNED_SENDER_DEVICE,
reason: DecryptionFailureReason.UNSIGNED_SENDER_DEVICE,
},
])("should return the snapshot with code converted to reason (%s)", ({ code, reason }) => {
const vm = new DecryptionFailureBodyViewModel({
decryptionFailureCode: code,
});
expect(vm.getSnapshot().decryptionFailureReason).toBe(reason);
});
it("should update snapshot when setProps is called with new verificationState", () => {
const vm = new DecryptionFailureBodyViewModel({
decryptionFailureCode: DecryptionFailureCode.UNKNOWN_ERROR,
verificationState: false,
});
expect(vm.getSnapshot().isLocalDeviceVerified).toBe(false);
vm.setVerificationState(true);
expect(vm.getSnapshot().isLocalDeviceVerified).toBe(true);
});
it("should update snapshot when decryption failure code changes", () => {
const vm = new DecryptionFailureBodyViewModel({
decryptionFailureCode: DecryptionFailureCode.UNKNOWN_ERROR,
});
expect(vm.getSnapshot().decryptionFailureReason).toBe(DecryptionFailureReason.UNABLE_TO_DECRYPT);
vm.setDecryptionFailureCode(DecryptionFailureCode.UNSIGNED_SENDER_DEVICE);
expect(vm.getSnapshot().decryptionFailureReason).toBe(DecryptionFailureReason.UNSIGNED_SENDER_DEVICE);
});
});