From 7ec387374f0eed682a5494780484315b77e8df9c Mon Sep 17 00:00:00 2001 From: Drew Budwin Date: Thu, 23 Jul 2020 09:03:27 -0400 Subject: [PATCH] Add config option to create to control whether a new option is selected or not If the creation of the new option in production code is dependent on some other service--like inserting the new option in a database--there is a potential for that operation to fail. When writing a unit test to verify how that failure is handled, it may not be desirable to select the new option given to `create` if the insert into the database failed. This commit introduces a new option in the `CreateConfig` object called `autoSelect` that will enable or disable the automatic selection of the new option. See issue #35 for more details. --- README.md | 1 + src/__tests__/select-event.test.tsx | 24 +++++++++++++++++++++++- src/index.ts | 14 +++++++++----- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 1e128fc..e9216dc 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,7 @@ expect(getByRole("form")).toHaveFormValues({ food: "papaya" }); `create` take an optional `config` parameter: - `config.createOptionText` can be used when [creating elements with a custom label text, using the `formatCreateLabel` prop](https://react-select.com/props#creatable-props). +- `config.autoSelect` is used to automatically select the newly created option, it defaults to `true`. - `config.container` can be used when the `react-select` dropdown is rendered in a portal using `menuPortalTarget`. ### `clearFirst(input: HTMLElement): Promise` diff --git a/src/__tests__/select-event.test.tsx b/src/__tests__/select-event.test.tsx index ded9066..239242f 100644 --- a/src/__tests__/select-event.test.tsx +++ b/src/__tests__/select-event.test.tsx @@ -1,8 +1,11 @@ import "@testing-library/jest-dom/extend-expect"; + +import { fireEvent, render } from "@testing-library/react"; + import React from "react"; -import { render, fireEvent } from "@testing-library/react"; import Select from "react-select"; import selectEvent from ".."; + let Async: any; let Creatable: any; let AsyncCreatable: any; @@ -124,6 +127,13 @@ describe("The select event helpers", () => { expect(form).toHaveFormValues({ food: "papaya" }); }); + it("types in and adds a new option but does not select it by default", async () => { + const { form, input } = renderForm(); + expect(form).toHaveFormValues({ food: "" }); + await selectEvent.create(input, "papaya", { autoSelect: false }); + expect(form).toHaveFormValues({ food: "" }); + }); + it("types in and adds a new option with custom create label when searching by fixed string", async () => { const { form, input } = renderForm( "Add new option"} /> @@ -377,6 +387,18 @@ describe("The select event helpers", () => { expect(form).toHaveFormValues({ food: "papaya" }); }); + it("types in and adds a new option but does not select it by default", async () => { + const { form, input } = renderForm( + + ); + expect(form).toHaveFormValues({ food: "" }); + await selectEvent.create(input, "papaya", { + autoSelect: false, + container: document.body, + }); + expect(form).toHaveFormValues({ food: "" }); + }); + it("clears the first item in a multi-select dropdown", async () => { const { form, input } = renderForm( { const createOptionText = config.createOptionText || /^Create "/; openMenu(input); type(input, option); fireEvent.change(input, { target: { value: option } }); - await select(input, createOptionText, config); - await findByText(getReactSelectContainerFromInput(input), option); + if (autoSelect) { + await select(input, createOptionText, config); + await findByText(getReactSelectContainerFromInput(input), option); + } }; /**