fix test
This commit is contained in:
parent
e7e7331558
commit
2ce127c5a8
1 changed files with 22 additions and 92 deletions
|
@ -7,31 +7,22 @@ Please see LICENSE files in the repository root for full details.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import { render, screen, waitFor } from "jest-matrix-react";
|
import { render, screen } from "jest-matrix-react";
|
||||||
import { mocked } from "jest-mock";
|
import userEvent from "@testing-library/user-event";
|
||||||
import { MatrixClient } from "matrix-js-sdk/src/matrix";
|
|
||||||
|
|
||||||
import { createCrossSigning } from "../../../../../src/CreateCrossSigning";
|
|
||||||
import { InitialCryptoSetupDialog } from "../../../../../src/components/views/dialogs/security/InitialCryptoSetupDialog";
|
import { InitialCryptoSetupDialog } from "../../../../../src/components/views/dialogs/security/InitialCryptoSetupDialog";
|
||||||
import { createTestClient } from "../../../../test-utils";
|
import { InitialCryptoSetupStore } from "../../../../../src/stores/InitialCryptoSetupStore";
|
||||||
|
|
||||||
jest.mock("../../../../../src/CreateCrossSigning", () => ({
|
|
||||||
createCrossSigning: jest.fn(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
describe("InitialCryptoSetupDialog", () => {
|
describe("InitialCryptoSetupDialog", () => {
|
||||||
let client: MatrixClient;
|
const storeMock = {
|
||||||
let createCrossSigningResolve: () => void;
|
getStatus: jest.fn(),
|
||||||
let createCrossSigningReject: (e: Error) => void;
|
retry: jest.fn(),
|
||||||
|
on: jest.fn(),
|
||||||
|
off: jest.fn(),
|
||||||
|
};
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
client = createTestClient();
|
jest.spyOn(InitialCryptoSetupStore, "sharedInstance").mockReturnValue(storeMock as any);
|
||||||
mocked(createCrossSigning).mockImplementation(() => {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
createCrossSigningResolve = resolve;
|
|
||||||
createCrossSigningReject = reject;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
|
@ -39,93 +30,32 @@ describe("InitialCryptoSetupDialog", () => {
|
||||||
jest.restoreAllMocks();
|
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();
|
const onFinished = jest.fn();
|
||||||
|
|
||||||
render(
|
storeMock.getStatus.mockReturnValue("in_progress");
|
||||||
<InitialCryptoSetupDialog
|
|
||||||
matrixClient={client}
|
render(<InitialCryptoSetupDialog onFinished={onFinished} />);
|
||||||
accountPassword="hunter2"
|
|
||||||
tokenLogin={false}
|
|
||||||
onFinished={onFinished}
|
|
||||||
/>,
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(createCrossSigning).toHaveBeenCalledWith(client, false, "hunter2");
|
|
||||||
expect(screen.getByTestId("spinner")).toBeInTheDocument();
|
expect(screen.getByTestId("spinner")).toBeInTheDocument();
|
||||||
|
|
||||||
createCrossSigningResolve!();
|
|
||||||
|
|
||||||
await waitFor(() => expect(onFinished).toHaveBeenCalledWith(true));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should display an error if createCrossSigning fails", async () => {
|
it("should display an error if setup has failed", async () => {
|
||||||
render(
|
storeMock.getStatus.mockReturnValue("error");
|
||||||
<InitialCryptoSetupDialog
|
|
||||||
matrixClient={client}
|
|
||||||
accountPassword="hunter2"
|
|
||||||
tokenLogin={false}
|
|
||||||
onFinished={jest.fn()}
|
|
||||||
/>,
|
|
||||||
);
|
|
||||||
|
|
||||||
createCrossSigningReject!(new Error("generic error message"));
|
render(<InitialCryptoSetupDialog onFinished={jest.fn()} />);
|
||||||
|
|
||||||
await expect(await screen.findByRole("button", { name: "Retry" })).toBeInTheDocument();
|
await expect(await screen.findByRole("button", { name: "Retry" })).toBeInTheDocument();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("ignores failures when tokenLogin is true", async () => {
|
it("calls retry when retry button pressed", async () => {
|
||||||
const onFinished = jest.fn();
|
const onFinished = jest.fn();
|
||||||
|
storeMock.getStatus.mockReturnValue("error");
|
||||||
|
|
||||||
render(
|
render(<InitialCryptoSetupDialog onFinished={onFinished} />);
|
||||||
<InitialCryptoSetupDialog
|
|
||||||
matrixClient={client}
|
|
||||||
accountPassword="hunter2"
|
|
||||||
tokenLogin={true}
|
|
||||||
onFinished={onFinished}
|
|
||||||
/>,
|
|
||||||
);
|
|
||||||
|
|
||||||
createCrossSigningReject!(new Error("generic error message"));
|
await userEvent.click(await screen.findByRole("button", { name: "Retry" }));
|
||||||
|
|
||||||
await waitFor(() => expect(onFinished).toHaveBeenCalledWith(false));
|
expect(storeMock.retry).toHaveBeenCalled();
|
||||||
});
|
|
||||||
|
|
||||||
it("cancels the dialog when the cancel button is clicked", async () => {
|
|
||||||
const onFinished = jest.fn();
|
|
||||||
|
|
||||||
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"));
|
|
||||||
|
|
||||||
const retryButton = await screen.findByRole("button", { name: "Retry" });
|
|
||||||
retryButton.click();
|
|
||||||
|
|
||||||
expect(createCrossSigning).toHaveBeenCalledTimes(2);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue