|
| 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 | +import React from "react"; |
| 9 | +import { render, screen } from "@testing-library/react"; |
| 10 | +import { mocked } from "jest-mock"; |
| 11 | +import EventEmitter from "events"; |
| 12 | + |
| 13 | +import CompleteSecurity from "../../../../src/components/structures/auth/CompleteSecurity"; |
| 14 | +import { stubClient } from "../../../test-utils"; |
| 15 | +import { Phase, SetupEncryptionStore } from "../../../../src/stores/SetupEncryptionStore"; |
| 16 | +import SdkConfig from "../../../../src/SdkConfig"; |
| 17 | + |
| 18 | +class MockSetupEncryptionStore extends EventEmitter { |
| 19 | + public phase: Phase = Phase.Intro; |
| 20 | + public lostKeys(): boolean { |
| 21 | + return false; |
| 22 | + } |
| 23 | + |
| 24 | + public start: () => void = jest.fn(); |
| 25 | + public stop: () => void = jest.fn(); |
| 26 | +} |
| 27 | + |
| 28 | +describe("CompleteSecurity", () => { |
| 29 | + beforeEach(() => { |
| 30 | + const client = stubClient(); |
| 31 | + const deviceIdToDevice = new Map(); |
| 32 | + deviceIdToDevice.set("DEVICE_ID", { |
| 33 | + deviceId: "DEVICE_ID", |
| 34 | + userId: "USER_ID", |
| 35 | + }); |
| 36 | + const userIdToDevices = new Map(); |
| 37 | + userIdToDevices.set("USER_ID", deviceIdToDevice); |
| 38 | + mocked(client.getCrypto()!.getUserDeviceInfo).mockResolvedValue(userIdToDevices); |
| 39 | + |
| 40 | + const mockSetupEncryptionStore = new MockSetupEncryptionStore(); |
| 41 | + jest.spyOn(SetupEncryptionStore, "sharedInstance").mockReturnValue( |
| 42 | + mockSetupEncryptionStore as SetupEncryptionStore, |
| 43 | + ); |
| 44 | + }); |
| 45 | + |
| 46 | + afterEach(() => { |
| 47 | + jest.restoreAllMocks(); |
| 48 | + }); |
| 49 | + |
| 50 | + it("Renders with a cancel button by default", () => { |
| 51 | + render(<CompleteSecurity onFinished={() => {}} />); |
| 52 | + |
| 53 | + expect(screen.getByRole("button", { name: "Skip verification for now" })).toBeInTheDocument(); |
| 54 | + }); |
| 55 | + |
| 56 | + it("Renders with a cancel button if forceVerification false", () => { |
| 57 | + jest.spyOn(SdkConfig, "get").mockImplementation((key: string) => { |
| 58 | + if (key === "forceVerification") { |
| 59 | + return false; |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + render(<CompleteSecurity onFinished={() => {}} />); |
| 64 | + |
| 65 | + expect(screen.getByRole("button", { name: "Skip verification for now" })).toBeInTheDocument(); |
| 66 | + }); |
| 67 | + |
| 68 | + it("Renders without a cancel button if forceVerification true", () => { |
| 69 | + jest.spyOn(SdkConfig, "get").mockImplementation((key: string) => { |
| 70 | + if (key === "force_verification") { |
| 71 | + return true; |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + render(<CompleteSecurity onFinished={() => {}} />); |
| 76 | + |
| 77 | + expect(screen.queryByRole("button", { name: "Skip verification for now" })).not.toBeInTheDocument(); |
| 78 | + }); |
| 79 | +}); |
0 commit comments