This repository has been archived by the owner on Oct 22, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
126 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
test/components/views/dialogs/security/CreateCrossSigningDialog-test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / Jest (2)CreateCrossSigningDialog › should retry when the retry button is clicked
|
||
}); | ||
}); |