|
| 1 | +/* |
| 2 | + Copyright 2026 Element Creations Ltd. |
| 3 | +
|
| 4 | + SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial |
| 5 | + Please see LICENSE files in the repository root for full details. |
| 6 | + */ |
| 7 | + |
| 8 | +import React, { type ComponentProps } from "react"; |
| 9 | +import { render, type RenderResult } from "jest-matrix-react"; |
| 10 | +import { getAllByRole, getAllByText, getByText } from "@testing-library/dom"; |
| 11 | + |
| 12 | +import UnknownIdentityUsersWarningDialog from "../../../../../../src/components/views/dialogs/invite/UnknownIdentityUsersWarningDialog.tsx"; |
| 13 | +import { InviteKind } from "../../../../../../src/components/views/dialogs/InviteDialogTypes.ts"; |
| 14 | +import { DirectoryMember, ThreepidMember } from "../../../../../../src/utils/direct-messages.ts"; |
| 15 | +import { getMockClientWithEventEmitter, mockClientMethodsUser } from "../../../../../test-utils"; |
| 16 | + |
| 17 | +describe("UnknownIdentityUsersWarningDialog", () => { |
| 18 | + beforeEach(() => { |
| 19 | + getMockClientWithEventEmitter({ |
| 20 | + ...mockClientMethodsUser(), |
| 21 | + }); |
| 22 | + }); |
| 23 | + |
| 24 | + afterEach(() => { |
| 25 | + jest.restoreAllMocks(); |
| 26 | + }); |
| 27 | + |
| 28 | + it("should show entries for each user", () => { |
| 29 | + const result = renderComponent({ |
| 30 | + users: [ |
| 31 | + new DirectoryMember({ user_id: "@alice:example.com" }), |
| 32 | + new DirectoryMember({ |
| 33 | + user_id: "@bob:example.net", |
| 34 | + display_name: "Bob", |
| 35 | + avatar_url: "mxc://example.com/abc", |
| 36 | + }), |
| 37 | + new ThreepidMember("charlie@example.com"), |
| 38 | + ], |
| 39 | + }); |
| 40 | + |
| 41 | + const list = result.getByRole("listbox"); |
| 42 | + const entries = getAllByRole(list, "option"); |
| 43 | + expect(entries).toHaveLength(3); |
| 44 | + |
| 45 | + // No displayname so mxid is displayed twice |
| 46 | + expect(getAllByText(entries[0], "@alice:example.com")).toHaveLength(2); |
| 47 | + |
| 48 | + getByText(entries[1], "Bob"); |
| 49 | + getByText(entries[2], "charlie@example.com"); |
| 50 | + }); |
| 51 | + |
| 52 | + describe("in DM mode", () => { |
| 53 | + const kind = InviteKind.Dm; |
| 54 | + |
| 55 | + it("shows a 'Continue' button", () => { |
| 56 | + const onContinue = jest.fn(); |
| 57 | + const result = renderComponent({ kind, onContinue }); |
| 58 | + const continueButton = result.getByRole("button", { name: "Continue" }); |
| 59 | + continueButton.click(); |
| 60 | + expect(onContinue).toHaveBeenCalled(); |
| 61 | + }); |
| 62 | + |
| 63 | + it("shows a 'Cancel' button", () => { |
| 64 | + const onCancel = jest.fn(); |
| 65 | + const result = renderComponent({ kind, onCancel }); |
| 66 | + const cancelButton = result.getByRole("button", { name: "Cancel" }); |
| 67 | + cancelButton.click(); |
| 68 | + expect(onCancel).toHaveBeenCalled(); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + describe("in Invite mode", () => { |
| 73 | + const kind = InviteKind.Invite; |
| 74 | + |
| 75 | + it("shows an 'Invite' button", () => { |
| 76 | + const onContinue = jest.fn(); |
| 77 | + const result = renderComponent({ kind, onContinue }); |
| 78 | + const continueButton = result.getByRole("button", { name: "Invite" }); |
| 79 | + continueButton.click(); |
| 80 | + expect(onContinue).toHaveBeenCalled(); |
| 81 | + }); |
| 82 | + |
| 83 | + it("shows a 'Remove' button", () => { |
| 84 | + const onRemove = jest.fn(); |
| 85 | + const result = renderComponent({ kind, onRemove }); |
| 86 | + const removeButton = result.getByRole("button", { name: "Remove" }); |
| 87 | + removeButton.click(); |
| 88 | + expect(onRemove).toHaveBeenCalled(); |
| 89 | + }); |
| 90 | + }); |
| 91 | +}); |
| 92 | + |
| 93 | +function renderComponent(props: Partial<ComponentProps<typeof UnknownIdentityUsersWarningDialog>>): RenderResult { |
| 94 | + const props1: ComponentProps<typeof UnknownIdentityUsersWarningDialog> = { |
| 95 | + onContinue: () => {}, |
| 96 | + onCancel: () => {}, |
| 97 | + onRemove: () => {}, |
| 98 | + screenName: undefined, |
| 99 | + kind: InviteKind.Dm, |
| 100 | + users: [], |
| 101 | + ...props, |
| 102 | + }; |
| 103 | + return render(<UnknownIdentityUsersWarningDialog {...props1} />); |
| 104 | +} |
0 commit comments