Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(typeahead-input): Add tests #162

Merged
merged 4 commits into from
Mar 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 72 additions & 25 deletions src/form/input/typeahead/typeahead-input.test.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,72 @@
import React from "react";
import {render} from "@testing-library/react";
import "@testing-library/jest-dom";

import {testA11y} from "../../../core/utils/test/testUtils";
import TypeaheadInput, {TypeaheadInputProps} from "./TypeaheadInput";

describe("<TypeaheadInput />", () => {
const defaultTypeaheadInputProps: TypeaheadInputProps = {
testid: "typeahead-input",
name: "typeahead-input",
onQueryChange: jest.fn(),
placeholder: "typeahead input"
};

it("should render correctly", () => {
render(<TypeaheadInput {...defaultTypeaheadInputProps} />);
});

it("should pass a11y test", async () => {
const {container} = render(<TypeaheadInput {...defaultTypeaheadInputProps} />);

await testA11y(container);
});
});
import React from "react";
import {render, screen} from "@testing-library/react";
import "@testing-library/jest-dom";
import userEvent from "@testing-library/user-event";

import {testA11y} from "../../../core/utils/test/testUtils";
import TypeaheadInput, {TypeaheadInputProps} from "./TypeaheadInput";

describe("<TypeaheadInput />", () => {
const defaultTypeaheadInputProps: TypeaheadInputProps = {
testid: "typeahead-input",
name: "typeahead-input",
onQueryChange: jest.fn(),
placeholder: "typeahead input"
};

it("should render correctly", () => {
render(<TypeaheadInput {...defaultTypeaheadInputProps} />);
});

it("should pass a11y test", async () => {
const {container} = render(<TypeaheadInput {...defaultTypeaheadInputProps} />);

await testA11y(container);
});

it("should update value on change", () => {
render(<TypeaheadInput {...defaultTypeaheadInputProps} />);

const typeaheadInput = screen.getByRole("textbox");

userEvent.type(typeaheadInput, "test");

expect(typeaheadInput).toHaveValue("test");
});

it("should render left and right icons correctly", () => {
const iconContent = <p data-testid={"icon"}>{"Test"}</p>;

const {rerender, container} = render(
<TypeaheadInput leftIcon={iconContent} {...defaultTypeaheadInputProps} />
);

const leftIcon = screen.getByText("Test");

expect(container).toContainElement(leftIcon);

rerender(<TypeaheadInput rightIcon={iconContent} {...defaultTypeaheadInputProps} />);

const rightIcon = screen.getByText("Test");

expect(container).toContainElement(rightIcon);
});

it("should run onQueryChange when value is changed", () => {
const handleQueryChange = jest.fn();

render(
<TypeaheadInput {...defaultTypeaheadInputProps} onQueryChange={handleQueryChange} />
);

handleQueryChange("test");

expect(handleQueryChange).toHaveBeenCalled();
});

it("should add placeholder correctly", () => {
render(<TypeaheadInput {...defaultTypeaheadInputProps} />);

expect(screen.getByRole("textbox")).toHaveAttribute("placeholder", "typeahead input");
});
});