element-web/test/unit-tests/components/structures/AutocompleteInput-test.tsx

276 lines
9.3 KiB
TypeScript
Raw Normal View History

2022-12-08 14:40:31 +03:00
/*
Copyright 2024 New Vector Ltd.
2022-12-08 14:40:31 +03:00
Copyright 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.
2022-12-08 14:40:31 +03:00
*/
2022-12-12 14:24:14 +03:00
import React from "react";
import { screen, render, fireEvent, waitFor, within, act } from "jest-matrix-react";
2022-12-08 14:40:31 +03:00
import * as TestUtils from "../../../test-utils";
import AutocompleteProvider from "../../../../src/autocomplete/AutocompleteProvider";
import { ICompletion } from "../../../../src/autocomplete/Autocompleter";
import { AutocompleteInput } from "../../../../src/components/structures/AutocompleteInput";
2022-12-08 14:40:31 +03:00
2022-12-12 14:24:14 +03:00
describe("AutocompleteInput", () => {
2022-12-08 14:40:31 +03:00
const mockCompletion: ICompletion[] = [
{
type: "user",
completion: "user_1",
completionId: "@user_1:host.local",
range: { start: 1, end: 1 },
component: <div />,
},
{
type: "user",
completion: "user_2",
completionId: "@user_2:host.local",
range: { start: 1, end: 1 },
component: <div />,
},
2022-12-08 14:40:31 +03:00
];
2022-12-12 14:24:14 +03:00
const constructMockProvider = (data: ICompletion[]) =>
({
getCompletions: jest.fn().mockImplementation(async () => data),
}) as unknown as AutocompleteProvider;
2022-12-08 14:40:31 +03:00
beforeEach(() => {
TestUtils.stubClient();
});
const getEditorInput = () => {
2022-12-12 14:24:14 +03:00
const input = screen.getByTestId("autocomplete-input");
2022-12-08 14:40:31 +03:00
expect(input).toBeDefined();
return input;
};
2022-12-12 14:24:14 +03:00
it("should render suggestions when a query is set", async () => {
2022-12-08 14:40:31 +03:00
const mockProvider = constructMockProvider(mockCompletion);
const onSelectionChangeMock = jest.fn();
render(
<AutocompleteInput
provider={mockProvider}
2022-12-12 14:24:14 +03:00
placeholder="Search ..."
2022-12-08 14:40:31 +03:00
selection={[]}
onSelectionChange={onSelectionChangeMock}
/>,
);
const input = getEditorInput();
act(() => {
fireEvent.focus(input);
2022-12-12 14:24:14 +03:00
fireEvent.change(input, { target: { value: "user" } });
2022-12-08 14:40:31 +03:00
});
await waitFor(() => expect(mockProvider.getCompletions).toHaveBeenCalledTimes(1));
2022-12-12 14:24:14 +03:00
expect(screen.getByTestId("autocomplete-matches").childNodes).toHaveLength(mockCompletion.length);
2022-12-08 14:40:31 +03:00
});
2022-12-12 14:24:14 +03:00
it("should render selected items passed in via props", () => {
2022-12-08 14:40:31 +03:00
const mockProvider = constructMockProvider(mockCompletion);
const onSelectionChangeMock = jest.fn();
render(
<AutocompleteInput
provider={mockProvider}
2022-12-12 14:24:14 +03:00
placeholder="Search ..."
2022-12-08 14:40:31 +03:00
selection={mockCompletion}
onSelectionChange={onSelectionChangeMock}
/>,
);
2022-12-12 14:24:14 +03:00
const editor = screen.getByTestId("autocomplete-editor");
2022-12-08 14:40:31 +03:00
const selection = within(editor).getAllByTestId("autocomplete-selection-item", { exact: false });
expect(selection).toHaveLength(mockCompletion.length);
});
2022-12-12 14:24:14 +03:00
it("should call onSelectionChange() when an item is removed from selection", () => {
2022-12-08 14:40:31 +03:00
const mockProvider = constructMockProvider(mockCompletion);
const onSelectionChangeMock = jest.fn();
render(
<AutocompleteInput
provider={mockProvider}
2022-12-12 14:24:14 +03:00
placeholder="Search ..."
2022-12-08 14:40:31 +03:00
selection={mockCompletion}
onSelectionChange={onSelectionChangeMock}
/>,
);
2022-12-12 14:24:14 +03:00
const editor = screen.getByTestId("autocomplete-editor");
2022-12-08 14:40:31 +03:00
const removeButtons = within(editor).getAllByTestId("autocomplete-selection-remove-button", { exact: false });
expect(removeButtons).toHaveLength(mockCompletion.length);
act(() => {
fireEvent.click(removeButtons[0]);
});
expect(onSelectionChangeMock).toHaveBeenCalledTimes(1);
expect(onSelectionChangeMock).toHaveBeenCalledWith([mockCompletion[1]]);
});
2022-12-12 14:24:14 +03:00
it("should render custom selection element when renderSelection() is defined", () => {
2022-12-08 14:40:31 +03:00
const mockProvider = constructMockProvider(mockCompletion);
const onSelectionChangeMock = jest.fn();
2022-12-12 14:24:14 +03:00
const renderSelection = () => <span data-testid="custom-selection-element">custom selection element</span>;
2022-12-08 14:40:31 +03:00
render(
<AutocompleteInput
provider={mockProvider}
2022-12-12 14:24:14 +03:00
placeholder="Search ..."
2022-12-08 14:40:31 +03:00
selection={mockCompletion}
onSelectionChange={onSelectionChangeMock}
renderSelection={renderSelection}
/>,
);
2022-12-12 14:24:14 +03:00
expect(screen.getAllByTestId("custom-selection-element")).toHaveLength(mockCompletion.length);
2022-12-08 14:40:31 +03:00
});
2022-12-12 14:24:14 +03:00
it("should render custom suggestion element when renderSuggestion() is defined", async () => {
2022-12-08 14:40:31 +03:00
const mockProvider = constructMockProvider(mockCompletion);
const onSelectionChangeMock = jest.fn();
2022-12-12 14:24:14 +03:00
const renderSuggestion = () => <span data-testid="custom-suggestion-element">custom suggestion element</span>;
2022-12-08 14:40:31 +03:00
render(
<AutocompleteInput
provider={mockProvider}
2022-12-12 14:24:14 +03:00
placeholder="Search ..."
2022-12-08 14:40:31 +03:00
selection={mockCompletion}
onSelectionChange={onSelectionChangeMock}
renderSuggestion={renderSuggestion}
/>,
);
const input = getEditorInput();
act(() => {
fireEvent.focus(input);
2022-12-12 14:24:14 +03:00
fireEvent.change(input, { target: { value: "user" } });
2022-12-08 14:40:31 +03:00
});
await waitFor(() => expect(mockProvider.getCompletions).toHaveBeenCalledTimes(1));
2022-12-12 14:24:14 +03:00
expect(screen.getAllByTestId("custom-suggestion-element")).toHaveLength(mockCompletion.length);
2022-12-08 14:40:31 +03:00
});
2022-12-12 14:24:14 +03:00
it("should mark selected suggestions as selected", async () => {
2022-12-08 14:40:31 +03:00
const mockProvider = constructMockProvider(mockCompletion);
const onSelectionChangeMock = jest.fn();
const { container } = render(
<AutocompleteInput
provider={mockProvider}
2022-12-12 14:24:14 +03:00
placeholder="Search ..."
2022-12-08 14:40:31 +03:00
selection={mockCompletion}
onSelectionChange={onSelectionChangeMock}
/>,
);
const input = getEditorInput();
act(() => {
fireEvent.focus(input);
2022-12-12 14:24:14 +03:00
fireEvent.change(input, { target: { value: "user" } });
2022-12-08 14:40:31 +03:00
});
await waitFor(() => expect(mockProvider.getCompletions).toHaveBeenCalledTimes(1));
2022-12-12 14:24:14 +03:00
const suggestions = await within(container).findAllByTestId("autocomplete-suggestion-item", { exact: false });
2022-12-08 14:40:31 +03:00
expect(suggestions).toHaveLength(mockCompletion.length);
2022-12-12 14:24:14 +03:00
suggestions.map((suggestion) => expect(suggestion).toHaveClass("mx_AutocompleteInput_suggestion--selected"));
2022-12-08 14:40:31 +03:00
});
2022-12-12 14:24:14 +03:00
it("should remove the last added selection when backspace is pressed in empty input", () => {
2022-12-08 14:40:31 +03:00
const mockProvider = constructMockProvider(mockCompletion);
const onSelectionChangeMock = jest.fn();
render(
<AutocompleteInput
provider={mockProvider}
2022-12-12 14:24:14 +03:00
placeholder="Search ..."
2022-12-08 14:40:31 +03:00
selection={mockCompletion}
onSelectionChange={onSelectionChangeMock}
/>,
);
const input = getEditorInput();
act(() => {
2022-12-12 14:24:14 +03:00
fireEvent.keyDown(input, { key: "Backspace" });
2022-12-08 14:40:31 +03:00
});
expect(onSelectionChangeMock).toHaveBeenCalledWith([mockCompletion[0]]);
});
2022-12-12 14:24:14 +03:00
it("should toggle a selected item when a suggestion is clicked", async () => {
2022-12-08 14:40:31 +03:00
const mockProvider = constructMockProvider(mockCompletion);
const onSelectionChangeMock = jest.fn();
const { container } = render(
<AutocompleteInput
provider={mockProvider}
2022-12-12 14:24:14 +03:00
placeholder="Search ..."
2022-12-08 14:40:31 +03:00
selection={[]}
onSelectionChange={onSelectionChangeMock}
/>,
);
const input = getEditorInput();
act(() => {
fireEvent.focus(input);
2022-12-12 14:24:14 +03:00
fireEvent.change(input, { target: { value: "user" } });
2022-12-08 14:40:31 +03:00
});
2022-12-12 14:24:14 +03:00
const suggestions = await within(container).findAllByTestId("autocomplete-suggestion-item", { exact: false });
2022-12-08 14:40:31 +03:00
act(() => {
fireEvent.mouseDown(suggestions[0]);
});
expect(onSelectionChangeMock).toHaveBeenCalledWith([mockCompletion[0]]);
});
it("should clear text field and suggestions when a suggestion is accepted", async () => {
const mockProvider = constructMockProvider(mockCompletion);
const onSelectionChangeMock = jest.fn();
const { container } = render(
<AutocompleteInput
provider={mockProvider}
placeholder="Search ..."
selection={[]}
onSelectionChange={onSelectionChangeMock}
/>,
);
const input = getEditorInput();
act(() => {
fireEvent.focus(input);
fireEvent.change(input, { target: { value: "user" } });
});
const suggestions = await within(container).findAllByTestId("autocomplete-suggestion-item", { exact: false });
act(() => {
fireEvent.mouseDown(suggestions[0]);
});
expect(input).toHaveValue("");
expect(within(container).queryAllByTestId("autocomplete-suggestion-item", { exact: false })).toHaveLength(0);
});
2022-12-08 14:40:31 +03:00
afterAll(() => {
jest.clearAllMocks();
jest.resetModules();
});
});