diff --git a/test/unit-tests/components/views/settings/encryption/DeleteKeyStoragePanel-test.tsx b/test/unit-tests/components/views/settings/encryption/DeleteKeyStoragePanel-test.tsx new file mode 100644 index 0000000000..f5593d9910 --- /dev/null +++ b/test/unit-tests/components/views/settings/encryption/DeleteKeyStoragePanel-test.tsx @@ -0,0 +1,91 @@ +/* + * 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 React from "react"; +import { MatrixClient } from "matrix-js-sdk/src/matrix"; +import { render, screen, waitFor } from "jest-matrix-react"; +import userEvent from "@testing-library/user-event"; +import { mocked } from "jest-mock"; +import { isEnabled } from "@sentry/core"; + +import { createTestClient, withClientContextRenderOptions } from "../../../../../test-utils"; +import { DeleteKeyStoragePanel } from "../../../../../../src/components/views/settings/encryption/DeleteKeyStoragePanel"; +import { useKeyStoragePanelViewModel } from "../../../../../../src/components/viewmodels/settings/encryption/KeyStoragePanelViewModel"; + +jest.mock("../../../../../../src/components/viewmodels/settings/encryption/KeyStoragePanelViewModel"); + +describe("", () => { + let matrixClient: MatrixClient; + + beforeEach(() => { + matrixClient = createTestClient(); + }); + + it("should match snapshot", async () => { + const { asFragment } = render( + {}} />, + withClientContextRenderOptions(matrixClient), + ); + expect(asFragment()).toMatchSnapshot(); + }); + + it("should call onFinished when cancel pressed", async () => { + const user = userEvent.setup(); + + const onFinish = jest.fn(); + render(, withClientContextRenderOptions(matrixClient)); + + await user.click(screen.getByRole("button", { name: "Cancel" })); + expect(onFinish).toHaveBeenCalled(); + }); + + it("should call disable key storage when confirm pressed", async () => { + const setEnabled = jest.fn(); + + mocked(useKeyStoragePanelViewModel).mockReturnValue({ + setEnabled, + isEnabled: true, + loading: false, + busy: false, + }); + + const user = userEvent.setup(); + + const onFinish = jest.fn(); + render(, withClientContextRenderOptions(matrixClient)); + + await user.click(screen.getByRole("button", { name: "Delete key storage" })); + + expect(setEnabled).toHaveBeenCalledWith(false); + }); + + it("should wait with button disabled while setEnabled runs", async () => { + let setEnabledResolve: () => void; + const setEnabledPromise = new Promise((r) => { + setEnabledResolve = r; + }); + + mocked(useKeyStoragePanelViewModel).mockReturnValue({ + setEnabled: jest.fn().mockReturnValue(setEnabledPromise), + isEnabled: true, + loading: false, + busy: false, + }); + + const user = userEvent.setup(); + + const onFinish = jest.fn(); + render(, withClientContextRenderOptions(matrixClient)); + + await user.click(screen.getByRole("button", { name: "Delete key storage" })); + + expect(onFinish).not.toHaveBeenCalled(); + expect(screen.getByRole("button", { name: "Delete key storage" })).toHaveAttribute("aria-disabled", "true"); + setEnabledResolve!(); + await waitFor(() => expect(onFinish).toHaveBeenCalled()); + }); +}); diff --git a/test/unit-tests/components/views/settings/encryption/__snapshots__/DeleteKeyStoragePanel-test.tsx.snap b/test/unit-tests/components/views/settings/encryption/__snapshots__/DeleteKeyStoragePanel-test.tsx.snap new file mode 100644 index 0000000000..4e59ef6686 --- /dev/null +++ b/test/unit-tests/components/views/settings/encryption/__snapshots__/DeleteKeyStoragePanel-test.tsx.snap @@ -0,0 +1,155 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[` should match snapshot 1`] = ` + + +
+
+
+ + + +
+

+ Are you sure you want to turn off key storage and delete it? +

+
+
+ Deleting key storage will remove your cryptographic identity and message keys from the server and turn off the following security features: +
    +
  • + + You will not have encrypted message history on new devices +
  • +
  • + + You will lose access to your encrypted messages if you are signed out of Element everywhere +
  • +
+
+ +
+
+`;