Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
dbkr committed Dec 6, 2024
1 parent e7e7331 commit 2ce127c
Showing 1 changed file with 22 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,125 +7,55 @@ Please see LICENSE files in the repository root for full details.
*/

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

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

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

describe("InitialCryptoSetupDialog", () => {
let client: MatrixClient;
let createCrossSigningResolve: () => void;
let createCrossSigningReject: (e: Error) => void;
const storeMock = {
getStatus: jest.fn(),
retry: jest.fn(),
on: jest.fn(),
off: jest.fn(),
};

beforeEach(() => {
client = createTestClient();
mocked(createCrossSigning).mockImplementation(() => {
return new Promise((resolve, reject) => {
createCrossSigningResolve = resolve;
createCrossSigningReject = reject;
});
});
jest.spyOn(InitialCryptoSetupStore, "sharedInstance").mockReturnValue(storeMock as any);
});

afterEach(() => {
jest.resetAllMocks();
jest.restoreAllMocks();
});

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

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

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

createCrossSigningResolve!();
render(<InitialCryptoSetupDialog onFinished={onFinished} />);

await waitFor(() => expect(onFinished).toHaveBeenCalledWith(true));
expect(screen.getByTestId("spinner")).toBeInTheDocument();
});

it("should display an error if createCrossSigning fails", async () => {
render(
<InitialCryptoSetupDialog
matrixClient={client}
accountPassword="hunter2"
tokenLogin={false}
onFinished={jest.fn()}
/>,
);
it("should display an error if setup has failed", async () => {
storeMock.getStatus.mockReturnValue("error");

createCrossSigningReject!(new Error("generic error message"));
render(<InitialCryptoSetupDialog onFinished={jest.fn()} />);

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

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

render(
<InitialCryptoSetupDialog
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 () => {
it("calls retry when retry button pressed", async () => {
const onFinished = jest.fn();
storeMock.getStatus.mockReturnValue("error");

render(
<InitialCryptoSetupDialog
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(
<InitialCryptoSetupDialog
matrixClient={client}
accountPassword="hunter2"
tokenLogin={false}
onFinished={jest.fn()}
/>,
);

createCrossSigningReject!(new Error("generic error message"));
render(<InitialCryptoSetupDialog onFinished={onFinished} />);

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

expect(createCrossSigning).toHaveBeenCalledTimes(2);
expect(storeMock.retry).toHaveBeenCalled();
});
});

0 comments on commit 2ce127c

Please sign in to comment.