Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dbkr committed Oct 10, 2024
1 parent 94fd783 commit 1ad8f1e
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 2 deletions.
2 changes: 0 additions & 2 deletions test/CreateCrossSigning-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ describe("CreateCrossSigning", () => {
client = createTestClient();
});

afterEach(() => {});

it("should call bootstrapCrossSigning with an authUploadDeviceSigningKeys function", async () => {
await createCrossSigning(client, false, "password");

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
Copyright 2024 New Vector Ltd.
Copyright 2018-2022 The Matrix.org Foundation C.I.C.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
Please see LICENSE files in the repository root for full details.
*/

import React from "react";
import { render, screen, waitFor } from "@testing-library/react";
import { mocked } from "jest-mock";
import { MatrixClient } from "matrix-js-sdk/src/matrix";

import { createCrossSigning } from "../../../../../src/CreateCrossSigning";
import CreateCrossSigningDialog from "../../../../../src/components/views/dialogs/security/CreateCrossSigningDialog";
import { createTestClient } from "../../../../test-utils";

jest.mock("../../../../../src/CreateCrossSigning", () => ({
createCrossSigning: jest.fn(),
}));

describe("CreateCrossSigningDialog", () => {
let client: MatrixClient;
let createCrossSigningResolve: () => void;
let createCrossSigningReject: (e: Error) => void;

beforeEach(() => {
client = createTestClient();
mocked(createCrossSigning).mockImplementation(() => {
return new Promise((resolve, reject) => {
createCrossSigningResolve = resolve;
createCrossSigningReject = reject;
});
});
});

it("should call createCrossSigning and show a spinner while it runs", async () => {
const onFinished = jest.fn();

render(
<CreateCrossSigningDialog
matrixClient={client}
accountPassword="hunter2"
tokenLogin={false}
onFinished={onFinished}
/>,
);

expect(createCrossSigning).toHaveBeenCalledWith(client, false, "hunter2");
expect(screen.getByTestId("spinner")).toBeInTheDocument();

createCrossSigningResolve!();

await waitFor(() => expect(onFinished).toHaveBeenCalledWith(true));
});

it("should display an error if createCrossSigning fails", async () => {
render(
<CreateCrossSigningDialog
matrixClient={client}
accountPassword="hunter2"
tokenLogin={false}
onFinished={jest.fn()}
/>,
);

createCrossSigningReject!(new Error("generic error message"));

await expect(await screen.findByRole("button", { name: "Retry" })).toBeInTheDocument();
});

it("ignores failures when tokenLogin is true", async () => {
const onFinished = jest.fn();

render(
<CreateCrossSigningDialog
matrixClient={client}
accountPassword="hunter2"
tokenLogin={true}
onFinished={onFinished}
/>,
);

createCrossSigningReject!(new Error("generic error message"));

await waitFor(() => expect(onFinished).toHaveBeenCalledWith(false));
});

it("cancels the dialog when the cancel button is clicked", async () => {
const onFinished = jest.fn();

render(
<CreateCrossSigningDialog
matrixClient={client}
accountPassword="hunter2"
tokenLogin={false}
onFinished={onFinished}
/>,
);

createCrossSigningReject!(new Error("generic error message"));

const cancelButton = await screen.findByRole("button", { name: "Cancel" });
cancelButton.click();

expect(onFinished).toHaveBeenCalledWith(false);
});

it("should retry when the retry button is clicked", async () => {
render(
<CreateCrossSigningDialog
matrixClient={client}
accountPassword="hunter2"
tokenLogin={false}
onFinished={jest.fn()}
/>,
);

createCrossSigningReject!(new Error("generic error message"));

const retryButton = await screen.findByRole("button", { name: "Retry" });
retryButton.click();

expect(createCrossSigning).toHaveBeenCalledTimes(2);

Check failure on line 124 in test/components/views/dialogs/security/CreateCrossSigningDialog-test.tsx

View workflow job for this annotation

GitHub Actions / Jest (2)

CreateCrossSigningDialog › should retry when the retry button is clicked

expect(jest.fn()).toHaveBeenCalledTimes(expected) Expected number of calls: 2 Received number of calls: 6 at Object.toHaveBeenCalledTimes (test/components/views/dialogs/security/CreateCrossSigningDialog-test.tsx:124:36)
});
});

0 comments on commit 1ad8f1e

Please sign in to comment.