Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"@babel/preset-typescript": "^7.3.3",
"@testing-library/jest-dom": "^5.0.1",
"@testing-library/react": "^12.1.5",
"@testing-library/user-event": "^14.4.3",
"@types/jest": "^29.1.2",
"@types/react": "^17.0.47",
"@types/react-select": "^5.0.1",
Expand Down
30 changes: 30 additions & 0 deletions src/__tests__/select-event.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import "@testing-library/jest-dom/extend-expect";

import { fireEvent, render } from "@testing-library/react";
import userEvent from "@testing-library/user-event";

import React from "react";
import Select from "react-select";
Expand Down Expand Up @@ -77,6 +78,35 @@ describe("The openMenu event helper", () => {
await selectEvent.select(input, "Strawberry");
expect(form).toHaveFormValues({ food: "strawberry" });
});

it("fires focus event", async () => {
const focusCallback = jest.fn();
const { input } = renderForm(<Select {...defaultProps} onFocus={focusCallback} />);
selectEvent.openMenu(input);
expect(focusCallback).toHaveBeenCalledTimes(1);
});

it("allows closing by clicking elsewhere", async () => {
const { input, getByText, queryByText } = renderForm(<Select {...defaultProps} />);
selectEvent.openMenu(input);
expect(getByText("Chocolate")).toBeInTheDocument();
expect(getByText("Vanilla")).toBeInTheDocument();
expect(getByText("Strawberry")).toBeInTheDocument();
expect(getByText("Mango")).toBeInTheDocument();
await userEvent.click(document.body);
expect(queryByText("Chocolate")).not.toBeInTheDocument();
expect(queryByText("Vanilla")).not.toBeInTheDocument();
expect(queryByText("Strawberry")).not.toBeInTheDocument();
expect(queryByText("Mango")).not.toBeInTheDocument();
});

it("allows blur event to fire when clicking elsewhere", async () => {
const blurCallback = jest.fn();
const { input } = renderForm(<Select {...defaultProps} onBlur={blurCallback} />);
selectEvent.openMenu(input);
await userEvent.click(document.body);
expect(blurCallback).toHaveBeenCalledTimes(1);
});
});

describe("The select event helpers", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function getReactSelectContainerFromInput(input: HTMLElement): HTMLElement {
* @param {HTMLElement} input The input field (eg. `getByLabelText('The label')`)
*/
export const openMenu = (input: HTMLElement) => {
fireEvent.focus(input);
input.focus();
fireEvent.keyDown(input, {
key: "ArrowDown",
keyCode: 40,
Expand Down