Skip to content

Commit

Permalink
Merge branch 'feat/add-test-to-typeahead' into next-release
Browse files Browse the repository at this point in the history
  • Loading branch information
mucahit committed Mar 11, 2022
2 parents 5b8f1f3 + e41cfef commit bc9d8e1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/form/input/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,9 @@ const Input = React.forwardRef<HTMLInputElement, InputProps>(
let caret = event.currentTarget.selectionStart || 0;

if (
String(value).length === finalEventValue.length + 1 ||
String(value).length === finalEventValue.length - 1
finalEventValue &&
(String(value).length === finalEventValue.length + 1 ||
String(value).length === finalEventValue.length - 1)
) {
if (prevValueThousandthsSeparatorCount === thousandthsSeparatorCount + 1) {
caret -= 1;
Expand Down
49 changes: 48 additions & 1 deletion src/form/input/typeahead/typeahead-input.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from "react";
import {render} from "@testing-library/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";
Expand All @@ -22,4 +23,50 @@ describe("<TypeaheadInput />", () => {

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");
});
});

0 comments on commit bc9d8e1

Please sign in to comment.