Factor out crypto setup process into a store (#28675)
* Factor out crypto setup process into a store To make components pure and avoid react 18 dev mode problems due to components making requests when mounted. * fix test * test for the store * Add comment
This commit is contained in:
parent
b86bb5cc2f
commit
b330de5d6e
8 changed files with 274 additions and 153 deletions
|
@ -7,31 +7,22 @@ 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(() => {
|
||||
|
@ -39,93 +30,32 @@ describe("InitialCryptoSetupDialog", () => {
|
|||
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}
|
||||
/>,
|
||||
);
|
||||
storeMock.getStatus.mockReturnValue("in_progress");
|
||||
|
||||
render(<InitialCryptoSetupDialog 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(
|
||||
<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 () => {
|
||||
it("calls retry when retry button pressed", async () => {
|
||||
const onFinished = jest.fn();
|
||||
storeMock.getStatus.mockReturnValue("error");
|
||||
|
||||
render(
|
||||
<InitialCryptoSetupDialog
|
||||
matrixClient={client}
|
||||
accountPassword="hunter2"
|
||||
tokenLogin={true}
|
||||
onFinished={onFinished}
|
||||
/>,
|
||||
);
|
||||
render(<InitialCryptoSetupDialog onFinished={onFinished} />);
|
||||
|
||||
createCrossSigningReject!(new Error("generic error message"));
|
||||
await userEvent.click(await screen.findByRole("button", { name: "Retry" }));
|
||||
|
||||
await waitFor(() => expect(onFinished).toHaveBeenCalledWith(false));
|
||||
});
|
||||
|
||||
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);
|
||||
expect(storeMock.retry).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
|
85
test/unit-tests/stores/InitialCryptoSetupStore-test.ts
Normal file
85
test/unit-tests/stores/InitialCryptoSetupStore-test.ts
Normal file
|
@ -0,0 +1,85 @@
|
|||
/*
|
||||
Copyright 2024 New Vector Ltd.
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
||||
Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
import { mocked } from "jest-mock";
|
||||
import { MatrixClient } from "matrix-js-sdk/src/matrix";
|
||||
import { waitFor } from "jest-matrix-react";
|
||||
|
||||
import { createCrossSigning } from "../../../src/CreateCrossSigning";
|
||||
import { InitialCryptoSetupStore } from "../../../src/stores/InitialCryptoSetupStore";
|
||||
import { SdkContextClass } from "../../../src/contexts/SDKContext";
|
||||
import { createTestClient } from "../../test-utils";
|
||||
import { AccountPasswordStore } from "../../../src/stores/AccountPasswordStore";
|
||||
|
||||
jest.mock("../../../src/CreateCrossSigning", () => ({
|
||||
createCrossSigning: jest.fn(),
|
||||
}));
|
||||
|
||||
describe("InitialCryptoSetupStore", () => {
|
||||
let testStore: InitialCryptoSetupStore;
|
||||
let client: MatrixClient;
|
||||
let stores: SdkContextClass;
|
||||
|
||||
let createCrossSigningResolve: () => void;
|
||||
let createCrossSigningReject: (e: Error) => void;
|
||||
|
||||
beforeEach(() => {
|
||||
testStore = new InitialCryptoSetupStore();
|
||||
client = createTestClient();
|
||||
stores = {
|
||||
accountPasswordStore: {
|
||||
getPassword: jest.fn(),
|
||||
} as unknown as AccountPasswordStore,
|
||||
} as unknown as SdkContextClass;
|
||||
|
||||
mocked(createCrossSigning).mockImplementation(() => {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
createCrossSigningResolve = resolve;
|
||||
createCrossSigningReject = reject;
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it("should call createCrossSigning when startInitialCryptoSetup is called", async () => {
|
||||
testStore.startInitialCryptoSetup(client, false, stores, jest.fn());
|
||||
|
||||
await waitFor(() => expect(createCrossSigning).toHaveBeenCalled());
|
||||
});
|
||||
|
||||
it("emits an update event when createCrossSigning resolves", async () => {
|
||||
const updateSpy = jest.fn();
|
||||
testStore.on("update", updateSpy);
|
||||
|
||||
testStore.startInitialCryptoSetup(client, false, stores, jest.fn());
|
||||
createCrossSigningResolve();
|
||||
|
||||
await waitFor(() => expect(updateSpy).toHaveBeenCalled());
|
||||
expect(testStore.getStatus()).toBe("complete");
|
||||
});
|
||||
|
||||
it("emits an update event when createCrossSigning rejects", async () => {
|
||||
const updateSpy = jest.fn();
|
||||
testStore.on("update", updateSpy);
|
||||
|
||||
testStore.startInitialCryptoSetup(client, false, stores, jest.fn());
|
||||
createCrossSigningReject(new Error("Test error"));
|
||||
|
||||
await waitFor(() => expect(updateSpy).toHaveBeenCalled());
|
||||
expect(testStore.getStatus()).toBe("error");
|
||||
});
|
||||
|
||||
it("should ignore failures if tokenLogin is true", async () => {
|
||||
const updateSpy = jest.fn();
|
||||
testStore.on("update", updateSpy);
|
||||
|
||||
testStore.startInitialCryptoSetup(client, true, stores, jest.fn());
|
||||
createCrossSigningReject(new Error("Test error"));
|
||||
|
||||
await waitFor(() => expect(updateSpy).toHaveBeenCalled());
|
||||
expect(testStore.getStatus()).toBe("complete");
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue