riot-web/test/components/structures/AutocompleteInput-test.tsx

284 lines
9.7 KiB
TypeScript
Raw Normal View History

2022-12-08 12:40:31 +01:00
/*
Copyright 2022 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2022-12-12 12:24:14 +01:00
import React from "react";
import { screen, render, fireEvent, waitFor, within, act } from "@testing-library/react";
2022-12-08 12:40:31 +01:00
2022-12-12 12:24:14 +01:00
import * as TestUtils from "../../test-utils";
import AutocompleteProvider from "../../../src/autocomplete/AutocompleteProvider";
import { ICompletion } from "../../../src/autocomplete/Autocompleter";
2022-12-08 12:40:31 +01:00
import { AutocompleteInput } from "../../../src/components/structures/AutocompleteInput";
2022-12-12 12:24:14 +01:00
describe("AutocompleteInput", () => {
2022-12-08 12:40:31 +01: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 12:40:31 +01:00
];
2022-12-12 12:24:14 +01:00
const constructMockProvider = (data: ICompletion[]) =>
({
getCompletions: jest.fn().mockImplementation(async () => data),
}) as unknown as AutocompleteProvider;
2022-12-08 12:40:31 +01:00
beforeEach(() => {
TestUtils.stubClient();
});
const getEditorInput = () => {
2022-12-12 12:24:14 +01:00
const input = screen.getByTestId("autocomplete-input");
2022-12-08 12:40:31 +01:00
expect(input).toBeDefined();
return input;
};
2022-12-12 12:24:14 +01:00
it("should render suggestions when a query is set", async () => {
2022-12-08 12:40:31 +01:00
const mockProvider = constructMockProvider(mockCompletion);
const onSelectionChangeMock = jest.fn();
render(
<AutocompleteInput
provider={mockProvider}
2022-12-12 12:24:14 +01:00
placeholder="Search ..."
2022-12-08 12:40:31 +01:00
selection={[]}
onSelectionChange={onSelectionChangeMock}
/>,
);
const input = getEditorInput();
act(() => {
fireEvent.focus(input);
2022-12-12 12:24:14 +01:00
fireEvent.change(input, { target: { value: "user" } });
2022-12-08 12:40:31 +01:00
});
await waitFor(() => expect(mockProvider.getCompletions).toHaveBeenCalledTimes(1));
2022-12-12 12:24:14 +01:00
expect(screen.getByTestId("autocomplete-matches").childNodes).toHaveLength(mockCompletion.length);
2022-12-08 12:40:31 +01:00
});
2022-12-12 12:24:14 +01:00
it("should render selected items passed in via props", () => {
2022-12-08 12:40:31 +01:00
const mockProvider = constructMockProvider(mockCompletion);
const onSelectionChangeMock = jest.fn();
render(
<AutocompleteInput
provider={mockProvider}
2022-12-12 12:24:14 +01:00
placeholder="Search ..."
2022-12-08 12:40:31 +01:00
selection={mockCompletion}
onSelectionChange={onSelectionChangeMock}
/>,
);
2022-12-12 12:24:14 +01:00
const editor = screen.getByTestId("autocomplete-editor");
2022-12-08 12:40:31 +01:00
const selection = within(editor).getAllByTestId("autocomplete-selection-item", { exact: false });
expect(selection).toHaveLength(mockCompletion.length);
});
2022-12-12 12:24:14 +01:00
it("should call onSelectionChange() when an item is removed from selection", () => {
2022-12-08 12:40:31 +01:00
const mockProvider = constructMockProvider(mockCompletion);
const onSelectionChangeMock = jest.fn();
render(
<AutocompleteInput
provider={mockProvider}
2022-12-12 12:24:14 +01:00
placeholder="Search ..."
2022-12-08 12:40:31 +01:00
selection={mockCompletion}
onSelectionChange={onSelectionChangeMock}
/>,
);
2022-12-12 12:24:14 +01:00
const editor = screen.getByTestId("autocomplete-editor");
2022-12-08 12:40:31 +01: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 12:24:14 +01:00
it("should render custom selection element when renderSelection() is defined", () => {
2022-12-08 12:40:31 +01:00
const mockProvider = constructMockProvider(mockCompletion);
const onSelectionChangeMock = jest.fn();
2022-12-12 12:24:14 +01:00
const renderSelection = () => <span data-testid="custom-selection-element">custom selection element</span>;
2022-12-08 12:40:31 +01:00
render(
<AutocompleteInput
provider={mockProvider}
2022-12-12 12:24:14 +01:00
placeholder="Search ..."
2022-12-08 12:40:31 +01:00
selection={mockCompletion}
onSelectionChange={onSelectionChangeMock}
renderSelection={renderSelection}
/>,
);
2022-12-12 12:24:14 +01:00
expect(screen.getAllByTestId("custom-selection-element")).toHaveLength(mockCompletion.length);
2022-12-08 12:40:31 +01:00
});
2022-12-12 12:24:14 +01:00
it("should render custom suggestion element when renderSuggestion() is defined", async () => {
2022-12-08 12:40:31 +01:00
const mockProvider = constructMockProvider(mockCompletion);
const onSelectionChangeMock = jest.fn();
2022-12-12 12:24:14 +01:00
const renderSuggestion = () => <span data-testid="custom-suggestion-element">custom suggestion element</span>;
2022-12-08 12:40:31 +01:00
render(
<AutocompleteInput
provider={mockProvider}
2022-12-12 12:24:14 +01:00
placeholder="Search ..."
2022-12-08 12:40:31 +01:00
selection={mockCompletion}
onSelectionChange={onSelectionChangeMock}
renderSuggestion={renderSuggestion}
/>,
);
const input = getEditorInput();
act(() => {
fireEvent.focus(input);
2022-12-12 12:24:14 +01:00
fireEvent.change(input, { target: { value: "user" } });
2022-12-08 12:40:31 +01:00
});
await waitFor(() => expect(mockProvider.getCompletions).toHaveBeenCalledTimes(1));
2022-12-12 12:24:14 +01:00
expect(screen.getAllByTestId("custom-suggestion-element")).toHaveLength(mockCompletion.length);
2022-12-08 12:40:31 +01:00
});
2022-12-12 12:24:14 +01:00
it("should mark selected suggestions as selected", async () => {
2022-12-08 12:40:31 +01:00
const mockProvider = constructMockProvider(mockCompletion);
const onSelectionChangeMock = jest.fn();
const { container } = render(
<AutocompleteInput
provider={mockProvider}
2022-12-12 12:24:14 +01:00
placeholder="Search ..."
2022-12-08 12:40:31 +01:00
selection={mockCompletion}
onSelectionChange={onSelectionChangeMock}
/>,
);
const input = getEditorInput();
act(() => {
fireEvent.focus(input);
2022-12-12 12:24:14 +01:00
fireEvent.change(input, { target: { value: "user" } });
2022-12-08 12:40:31 +01:00
});
await waitFor(() => expect(mockProvider.getCompletions).toHaveBeenCalledTimes(1));
2022-12-12 12:24:14 +01:00
const suggestions = await within(container).findAllByTestId("autocomplete-suggestion-item", { exact: false });
2022-12-08 12:40:31 +01:00
expect(suggestions).toHaveLength(mockCompletion.length);
2022-12-12 12:24:14 +01:00
suggestions.map((suggestion) => expect(suggestion).toHaveClass("mx_AutocompleteInput_suggestion--selected"));
2022-12-08 12:40:31 +01:00
});
2022-12-12 12:24:14 +01:00
it("should remove the last added selection when backspace is pressed in empty input", () => {
2022-12-08 12:40:31 +01:00
const mockProvider = constructMockProvider(mockCompletion);
const onSelectionChangeMock = jest.fn();
render(
<AutocompleteInput
provider={mockProvider}
2022-12-12 12:24:14 +01:00
placeholder="Search ..."
2022-12-08 12:40:31 +01:00
selection={mockCompletion}
onSelectionChange={onSelectionChangeMock}
/>,
);
const input = getEditorInput();
act(() => {
2022-12-12 12:24:14 +01:00
fireEvent.keyDown(input, { key: "Backspace" });
2022-12-08 12:40:31 +01:00
});
expect(onSelectionChangeMock).toHaveBeenCalledWith([mockCompletion[0]]);
});
2022-12-12 12:24:14 +01:00
it("should toggle a selected item when a suggestion is clicked", async () => {
2022-12-08 12:40:31 +01:00
const mockProvider = constructMockProvider(mockCompletion);
const onSelectionChangeMock = jest.fn();
const { container } = render(
<AutocompleteInput
provider={mockProvider}
2022-12-12 12:24:14 +01:00
placeholder="Search ..."
2022-12-08 12:40:31 +01:00
selection={[]}
onSelectionChange={onSelectionChangeMock}
/>,
);
const input = getEditorInput();
act(() => {
fireEvent.focus(input);
2022-12-12 12:24:14 +01:00
fireEvent.change(input, { target: { value: "user" } });
2022-12-08 12:40:31 +01:00
});
2022-12-12 12:24:14 +01:00
const suggestions = await within(container).findAllByTestId("autocomplete-suggestion-item", { exact: false });
2022-12-08 12:40:31 +01: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 12:40:31 +01:00
afterAll(() => {
jest.clearAllMocks();
jest.resetModules();
});
});