|
| 1 | +/* |
| 2 | + * Copyright 2024 New Vector Ltd. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only |
| 5 | + * Please see LICENSE files in the repository root for full details. |
| 6 | + * |
| 7 | + */ |
| 8 | + |
| 9 | +import React from "react"; |
| 10 | +import { screen, render, waitFor } from "@testing-library/react"; |
| 11 | +import userEvent from "@testing-library/user-event"; |
| 12 | +// Needed to be able to mock decodeRecoveryKey |
| 13 | +// eslint-disable-next-line no-restricted-imports |
| 14 | +import * as recoveryKeyModule from "matrix-js-sdk/src/crypto-api/recovery-key"; |
| 15 | + |
| 16 | +import RestoreKeyBackupDialog from "../../../../../src/components/views/dialogs/security/RestoreKeyBackupDialog.tsx"; |
| 17 | +import { stubClient } from "../../../../test-utils"; |
| 18 | + |
| 19 | +describe("<RestoreKeyBackupDialog />", () => { |
| 20 | + beforeEach(() => { |
| 21 | + stubClient(); |
| 22 | + jest.spyOn(recoveryKeyModule, "decodeRecoveryKey").mockReturnValue(new Uint8Array(32)); |
| 23 | + }); |
| 24 | + |
| 25 | + it("should render", async () => { |
| 26 | + const { asFragment } = render(<RestoreKeyBackupDialog onFinished={jest.fn()} />); |
| 27 | + await waitFor(() => expect(screen.getByText("Enter Security Key")).toBeInTheDocument()); |
| 28 | + expect(asFragment()).toMatchSnapshot(); |
| 29 | + }); |
| 30 | + |
| 31 | + it("should display an error when recovery key is invalid", async () => { |
| 32 | + jest.spyOn(recoveryKeyModule, "decodeRecoveryKey").mockImplementation(() => { |
| 33 | + throw new Error("Invalid recovery key"); |
| 34 | + }); |
| 35 | + const { asFragment } = render(<RestoreKeyBackupDialog onFinished={jest.fn()} />); |
| 36 | + await waitFor(() => expect(screen.getByText("Enter Security Key")).toBeInTheDocument()); |
| 37 | + |
| 38 | + await userEvent.type(screen.getByRole("textbox"), "invalid key"); |
| 39 | + await waitFor(() => expect(screen.getByText("👎 Not a valid Security Key")).toBeInTheDocument()); |
| 40 | + expect(asFragment()).toMatchSnapshot(); |
| 41 | + }); |
| 42 | + |
| 43 | + it("should not raise an error when recovery is valid", async () => { |
| 44 | + const { asFragment } = render(<RestoreKeyBackupDialog onFinished={jest.fn()} />); |
| 45 | + await waitFor(() => expect(screen.getByText("Enter Security Key")).toBeInTheDocument()); |
| 46 | + |
| 47 | + await userEvent.type(screen.getByRole("textbox"), "valid key"); |
| 48 | + await waitFor(() => expect(screen.getByText("👍 This looks like a valid Security Key!")).toBeInTheDocument()); |
| 49 | + expect(asFragment()).toMatchSnapshot(); |
| 50 | + }); |
| 51 | +}); |
0 commit comments