Prepare for repo merge

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
Michael Telatynski 2024-10-15 11:35:21 +01:00
parent 0f670b8dc0
commit b084ff2313
No known key found for this signature in database
GPG key ID: A2B008A5F49F5D0D
807 changed files with 0 additions and 0 deletions

View file

@ -0,0 +1,75 @@
/*
Copyright 2024 New Vector Ltd.
Copyright 2023 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 { fireEvent, render } from "jest-matrix-react";
import userEvent from "@testing-library/user-event";
import CountryDropdown from "../../../../src/components/views/auth/CountryDropdown";
import SdkConfig from "../../../../src/SdkConfig";
describe("CountryDropdown", () => {
describe("default_country_code", () => {
afterEach(() => {
SdkConfig.reset();
});
it.each([
["GB", 44],
["IE", 353],
["ES", 34],
["FR", 33],
["PL", 48],
["DE", 49],
])("should respect configured default country code for %s", (config, defaultCountryCode) => {
SdkConfig.add({
default_country_code: config,
});
const fn = jest.fn();
render(<CountryDropdown onOptionChange={fn} isSmall={false} showPrefix={false} />);
expect(fn).toHaveBeenCalledWith(expect.objectContaining({ prefix: defaultCountryCode.toString() }));
});
});
describe("defaultCountry", () => {
it.each([
["en-GB", 44],
["en-ie", 353],
["es-ES", 34],
["fr", 33],
["pl", 48],
["de-DE", 49],
])("should pick appropriate default country for %s", (language, defaultCountryCode) => {
Object.defineProperty(navigator, "language", {
configurable: true,
get() {
return language;
},
});
const fn = jest.fn();
render(<CountryDropdown onOptionChange={fn} isSmall={false} showPrefix={false} />);
expect(fn).toHaveBeenCalledWith(expect.objectContaining({ prefix: defaultCountryCode.toString() }));
});
});
it("should allow filtering", async () => {
const fn = jest.fn();
const { getByRole, findByText } = render(
<CountryDropdown onOptionChange={fn} isSmall={false} showPrefix={false} />,
);
const dropdown = getByRole("button");
fireEvent.click(dropdown);
await userEvent.keyboard("Al");
await expect(findByText("Albania (+355)")).resolves.toBeInTheDocument();
});
});

View file

@ -0,0 +1,101 @@
/*
* Copyright 2024 New Vector Ltd.
* Copyright 2024 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, act, fireEvent } from "jest-matrix-react";
import { AuthType } from "matrix-js-sdk/src/interactive-auth";
import userEvent from "@testing-library/user-event";
import {
EmailIdentityAuthEntry,
MasUnlockCrossSigningAuthEntry,
} from "../../../../src/components/views/auth/InteractiveAuthEntryComponents";
import { createTestClient } from "../../../test-utils";
describe("<EmailIdentityAuthEntry/>", () => {
const renderIdentityAuth = () => {
const matrixClient = createTestClient();
return render(
<EmailIdentityAuthEntry
matrixClient={matrixClient}
loginType={AuthType.Email}
onPhaseChange={jest.fn()}
submitAuthDict={jest.fn()}
fail={jest.fn()}
clientSecret="my secret"
showContinue={true}
inputs={{ emailAddress: "alice@example.xyz" }}
/>,
);
};
test("should render", () => {
const { container } = renderIdentityAuth();
expect(container).toMatchSnapshot();
});
test("should clear the requested state when the button tooltip is hidden", async () => {
renderIdentityAuth();
// After a click on the resend button, the button should display the resent label
screen.getByRole("button", { name: "Resend" }).click();
await waitFor(() => expect(screen.queryByRole("button", { name: "Resent!" })).toBeInTheDocument());
expect(screen.queryByRole("button", { name: "Resend" })).toBeNull();
const resentButton = screen.getByRole("button", { name: "Resent!" });
// Hover briefly the button and wait for the tooltip to be displayed
await userEvent.hover(resentButton);
await waitFor(() => expect(screen.getByRole("tooltip", { name: "Resent!" })).toBeInTheDocument());
// On unhover, it should display again the resend button
await act(() => userEvent.unhover(resentButton));
await waitFor(() => expect(screen.queryByRole("button", { name: "Resend" })).toBeInTheDocument());
});
});
describe("<MasUnlockCrossSigningAuthEntry/>", () => {
const renderAuth = (props = {}) => {
const matrixClient = createTestClient();
return render(
<MasUnlockCrossSigningAuthEntry
matrixClient={matrixClient}
loginType={AuthType.Email}
onPhaseChange={jest.fn()}
submitAuthDict={jest.fn()}
fail={jest.fn()}
clientSecret="my secret"
showContinue={true}
stageParams={{ url: "https://example.com" }}
{...props}
/>,
);
};
test("should render", () => {
const { container } = renderAuth();
expect(container).toMatchSnapshot();
});
test("should open idp in new tab on click", async () => {
const spy = jest.spyOn(global.window, "open");
renderAuth();
fireEvent.click(screen.getByRole("button", { name: "Go to your account" }));
expect(spy).toHaveBeenCalledWith("https://example.com", "_blank");
});
test("should retry uia request on click", async () => {
const submitAuthDict = jest.fn();
renderAuth({ submitAuthDict });
fireEvent.click(screen.getByRole("button", { name: "Retry" }));
expect(submitAuthDict).toHaveBeenCalledWith({});
});
});

View file

@ -0,0 +1,94 @@
/*
Copyright 2024 New Vector Ltd.
Copyright 2022 The Matrix.org Foundation C.I.C.
Copyright 2022 Callum Brown
Copyright 2016 OpenMarket 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 React from "react";
import { fireEvent, render, RenderResult } from "jest-matrix-react";
import InteractiveAuthComponent from "../../../../src/components/structures/InteractiveAuth";
import { flushPromises, getMockClientWithEventEmitter, unmockClientPeg } from "../../../test-utils";
describe("InteractiveAuthComponent", function () {
const mockClient = getMockClientWithEventEmitter({
generateClientSecret: jest.fn().mockReturnValue("t35tcl1Ent5ECr3T"),
});
const defaultProps = {
matrixClient: mockClient,
makeRequest: jest.fn().mockResolvedValue(undefined),
onAuthFinished: jest.fn(),
};
const getComponent = (props = {}) => render(<InteractiveAuthComponent {...defaultProps} {...props} />);
beforeEach(function () {
jest.clearAllMocks();
});
afterAll(() => {
unmockClientPeg();
});
const getSubmitButton = ({ container }: RenderResult) =>
container.querySelector(".mx_AccessibleButton_kind_primary");
const getRegistrationTokenInput = ({ container }: RenderResult) =>
container.querySelector('input[name="registrationTokenField"]');
it("Should successfully complete a registration token flow", async () => {
const onAuthFinished = jest.fn();
const makeRequest = jest.fn().mockResolvedValue({ a: 1 });
const authData = {
session: "sess",
flows: [{ stages: ["m.login.registration_token"] }],
};
const wrapper = getComponent({ makeRequest, onAuthFinished, authData });
const registrationTokenNode = getRegistrationTokenInput(wrapper);
const submitNode = getSubmitButton(wrapper);
const formNode = wrapper.container.querySelector("form");
expect(registrationTokenNode).toBeTruthy();
expect(submitNode).toBeTruthy();
expect(formNode).toBeTruthy();
// submit should be disabled
expect(submitNode).toHaveAttribute("disabled");
expect(submitNode).toHaveAttribute("aria-disabled", "true");
// put something in the registration token box
fireEvent.change(registrationTokenNode!, { target: { value: "s3kr3t" } });
expect(getRegistrationTokenInput(wrapper)).toHaveValue("s3kr3t");
expect(submitNode).not.toHaveAttribute("disabled");
expect(submitNode).not.toHaveAttribute("aria-disabled", "true");
// hit enter; that should trigger a request
fireEvent.submit(formNode!);
// wait for auth request to resolve
await flushPromises();
expect(makeRequest).toHaveBeenCalledTimes(1);
expect(makeRequest).toHaveBeenCalledWith(
expect.objectContaining({
session: "sess",
type: "m.login.registration_token",
token: "s3kr3t",
}),
);
expect(onAuthFinished).toHaveBeenCalledTimes(1);
expect(onAuthFinished).toHaveBeenCalledWith(
true,
{ a: 1 },
{ clientSecret: "t35tcl1Ent5ECr3T", emailSid: undefined },
);
});
});

View file

@ -0,0 +1,84 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`<EmailIdentityAuthEntry/> should render 1`] = `
<div>
<div
class="mx_InteractiveAuthEntryComponents_emailWrapper"
>
<p>
<span>
To create your account, open the link in the email we just sent to
<strong>
alice@example.xyz
</strong>
.
</span>
</p>
<p
class="secondary"
>
<span>
Did not receive it?
<div
aria-label="Resend"
class="mx_AccessibleButton mx_AccessibleButton_hasKind mx_AccessibleButton_kind_link_inline"
role="button"
tabindex="0"
>
Resend it
</div>
</span>
</p>
</div>
</div>
`;
exports[`<MasUnlockCrossSigningAuthEntry/> should render 1`] = `
<div>
<div>
<p
class="_typography_yh5dq_162 _font-body-md-regular_yh5dq_59"
>
Reset your identity through your account provider and then come back and click “Retry”.
</p>
<div
class="mx_Flex"
style="--mx-flex-display: flex; --mx-flex-direction: row; --mx-flex-align: start; --mx-flex-justify: start; --mx-flex-gap: var(--cpd-space-4x);"
>
<button
class="_button_i91xf_17 mx_Dialog_nonDialogButton _has-icon_i91xf_66"
data-kind="primary"
data-size="lg"
role="button"
tabindex="0"
>
<svg
aria-hidden="true"
fill="currentColor"
height="20"
viewBox="0 0 24 24"
width="20"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M5 3h6a1 1 0 1 1 0 2H5v14h14v-6a1 1 0 1 1 2 0v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2Z"
/>
<path
d="M15 3h5a1 1 0 0 1 1 1v5a1 1 0 1 1-2 0V6.414l-6.293 6.293a1 1 0 0 1-1.414-1.414L17.586 5H15a1 1 0 1 1 0-2Z"
/>
</svg>
Go to your account
</button>
<button
class="_button_i91xf_17 mx_Dialog_nonDialogButton"
data-kind="secondary"
data-size="lg"
role="button"
tabindex="0"
>
Retry
</button>
</div>
</div>
</div>
`;