2022-11-02 11:51:20 +01:00
|
|
|
/*
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-11-02 11:51:20 +01:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
2025-01-06 11:18:54 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
|
2024-09-09 14:57:16 +01:00
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-11-02 11:51:20 +01:00
|
|
|
*/
|
|
|
|
|
|
2024-10-14 21:41:58 +05:30
|
|
|
import { cleanup, fireEvent, render, screen, waitFor } from "jest-matrix-react";
|
2022-11-02 11:51:20 +01:00
|
|
|
import React from "react";
|
2024-10-24 13:58:39 +01:00
|
|
|
import { ClientRendezvousFailureReason, MSC4108FailureReason } from "matrix-js-sdk/src/rendezvous";
|
2022-11-02 11:51:20 +01:00
|
|
|
|
2024-10-15 14:57:26 +01:00
|
|
|
import LoginWithQRFlow from "../../../../../../src/components/views/auth/LoginWithQRFlow";
|
|
|
|
|
import { LoginWithQRFailureReason, FailureReason } from "../../../../../../src/components/views/auth/LoginWithQR";
|
|
|
|
|
import { Click, Phase } from "../../../../../../src/components/views/auth/LoginWithQR-types";
|
2022-11-02 11:51:20 +01:00
|
|
|
|
|
|
|
|
describe("<LoginWithQRFlow />", () => {
|
|
|
|
|
const onClick = jest.fn();
|
|
|
|
|
|
|
|
|
|
const defaultProps = {
|
|
|
|
|
onClick,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getComponent = (props: {
|
|
|
|
|
phase: Phase;
|
|
|
|
|
onClick?: () => Promise<void>;
|
2024-01-11 16:11:47 +00:00
|
|
|
failureReason?: FailureReason;
|
2024-10-24 13:58:39 +01:00
|
|
|
code?: Uint8Array;
|
2022-11-02 11:51:20 +01:00
|
|
|
}) => <LoginWithQRFlow {...defaultProps} {...props} />;
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {});
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
onClick.mockReset();
|
|
|
|
|
cleanup();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("renders spinner while loading", async () => {
|
|
|
|
|
const { container } = render(getComponent({ phase: Phase.Loading }));
|
|
|
|
|
expect(container).toMatchSnapshot();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("renders spinner whilst QR generating", async () => {
|
|
|
|
|
const { container } = render(getComponent({ phase: Phase.ShowingQR }));
|
|
|
|
|
expect(screen.getAllByTestId("cancel-button")).toHaveLength(1);
|
|
|
|
|
expect(container).toMatchSnapshot();
|
|
|
|
|
fireEvent.click(screen.getByTestId("cancel-button"));
|
2024-06-06 09:57:28 +01:00
|
|
|
expect(onClick).toHaveBeenCalledWith(Click.Cancel, undefined);
|
2022-11-02 11:51:20 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("renders QR code", async () => {
|
2024-10-24 13:58:39 +01:00
|
|
|
const { container } = render(
|
|
|
|
|
getComponent({ phase: Phase.ShowingQR, code: new TextEncoder().encode("mock-code") }),
|
|
|
|
|
);
|
2022-11-02 11:51:20 +01:00
|
|
|
// QR code is rendered async so we wait for it:
|
|
|
|
|
await waitFor(() => screen.getAllByAltText("QR Code").length === 1);
|
|
|
|
|
expect(container).toMatchSnapshot();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("renders spinner while signing in", async () => {
|
|
|
|
|
const { container } = render(getComponent({ phase: Phase.WaitingForDevice }));
|
|
|
|
|
expect(screen.getAllByTestId("cancel-button")).toHaveLength(1);
|
|
|
|
|
expect(container).toMatchSnapshot();
|
|
|
|
|
fireEvent.click(screen.getByTestId("cancel-button"));
|
2024-06-06 09:57:28 +01:00
|
|
|
expect(onClick).toHaveBeenCalledWith(Click.Cancel, undefined);
|
2022-11-02 11:51:20 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("renders spinner while verifying", async () => {
|
|
|
|
|
const { container } = render(getComponent({ phase: Phase.Verifying }));
|
|
|
|
|
expect(container).toMatchSnapshot();
|
|
|
|
|
});
|
|
|
|
|
|
2024-06-06 09:57:28 +01:00
|
|
|
it("renders check code confirmation", async () => {
|
|
|
|
|
const { container } = render(getComponent({ phase: Phase.OutOfBandConfirmation }));
|
|
|
|
|
expect(container).toMatchSnapshot();
|
|
|
|
|
});
|
|
|
|
|
|
2022-11-02 11:51:20 +01:00
|
|
|
describe("errors", () => {
|
2024-01-11 16:11:47 +00:00
|
|
|
for (const failureReason of [
|
2024-06-06 09:57:28 +01:00
|
|
|
...Object.values(MSC4108FailureReason),
|
2024-01-11 16:11:47 +00:00
|
|
|
...Object.values(LoginWithQRFailureReason),
|
2024-06-06 09:57:28 +01:00
|
|
|
...Object.values(ClientRendezvousFailureReason),
|
2024-01-11 16:11:47 +00:00
|
|
|
]) {
|
2022-11-02 11:51:20 +01:00
|
|
|
it(`renders ${failureReason}`, async () => {
|
|
|
|
|
const { container } = render(
|
|
|
|
|
getComponent({
|
|
|
|
|
phase: Phase.Error,
|
|
|
|
|
failureReason,
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
expect(screen.getAllByTestId("cancellation-message")).toHaveLength(1);
|
|
|
|
|
expect(container).toMatchSnapshot();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|