Skip to content

Commit

Permalink
Start adding tests for Combobox
Browse files Browse the repository at this point in the history
This caused me lots of pain which was finally solved by https://kentcdodds.com/blog/fix-the-not-wrapped-in-act-warning
  • Loading branch information
oBusk committed Feb 8, 2022
1 parent 23b63a3 commit 91cc7a4
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/components/Combobox/Combobox.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/** @jest-environment jsdom */

import { act, fireEvent, render, screen } from "@testing-library/react";
import Combobox from "./Combobox";

describe("Combobox", () => {
const setup = async () => {
const suggestionPromise = new Promise<{ value: string }[]>((resolve) =>
resolve([{ value: "a" }, { value: "b" }]),
);

const suggestionFinder = jest.fn(
(query: string | undefined) => suggestionPromise,
);

const utils = render(
<Combobox id="test" suggestionFinder={suggestionFinder} />,
);

const input: HTMLInputElement = await screen.findByRole("textbox");

return {
suggestionPromise,
suggestionFinder,
input,
...utils,
};
};

it("Renders an input", async () => {
const { input } = await setup();

expect(input).toBeInTheDocument();
});

it("Calls suggestionFinder on input", async () => {
const { suggestionPromise, suggestionFinder, input } = await setup();

fireEvent.focus(input);
fireEvent.input(input, { target: { value: "a" } });

expect(suggestionFinder).toHaveBeenCalledTimes(1);
expect(suggestionFinder).toHaveBeenCalledWith("a");

await act(() => suggestionPromise.then(() => {}));
});
});

0 comments on commit 91cc7a4

Please sign in to comment.