import * as _ from "lodash"; import * as assert from "assert"; import { shallow } from "enzyme"; import * as React from "react"; import ReactSelect from "react-select"; import * as sinon from "sinon"; import { Select, cssClass } from "../src/Select/Select"; describe("Select", () => { const testOptions = [ { label: "Test Option 1", value: "testopt1" }, { label: "Test Option 2", value: "testopt2" }, { label: "Test Option 3", value: "testopt3" }, { label: "Test Option 4", value: "testopt4" }, ]; it("wraps ReactSelect with an element with the provided id", () => { const select = shallow(); const label = select.find(`label.${cssClass.LABEL}`); assert.equal(label.hasClass(cssClass.LABEL_HIDDEN), false); assert.equal(label.prop("htmlFor"), "testid"); }); it("renders the label when no placeholder is provided", () => { const select = shallow(, ); const label = select.find(`.${cssClass.LABEL}`); assert.equal(label.hasClass(cssClass.LABEL_HIDDEN), false); }); it("hides the label when a placeholder is provided but no value is selected", () => { const select = shallow( , ); const label = select.find(`.${cssClass.LABEL}`); assert.equal(label.hasClass(cssClass.LABEL_HIDDEN), false); }); it("hides the label when a placeholder is provided and value is an empty array", () => { const select = shallow( , ); const expectedPropValues = { className: cssClass.REACT_SELECT, clearable: false, disabled: false, multi: false, name: "testname", onChange, options: testOptions, placeholder: "test placeholder", searchable: false, value: testOptions[2], }; const reactSelectProps = _.pick( select.find(ReactSelect).props(), Object.keys(expectedPropValues), ); assert.deepEqual(reactSelectProps, expectedPropValues); }); it("allows selecting multiple options if specified", () => { const multiSelect = shallow( ); const reactSelect = select.find(ReactSelect); assert(reactSelect.prop("disabled")); }); it("sets clearable on the react select element if specified", () => { const select = shallow(); const reactSelect = select.find(ReactSelect); assert(reactSelect.prop("searchable")); }); it("defaults to an empty string placeholder ReactSelect", () => { const select = shallow(); assert(select.find(".Select--required").exists()); }); it("renders the error if specified, hiding required", () => { const select = shallow( ); const creatable = select.find(ReactSelect.Creatable); assert(creatable.exists()); assert.equal(creatable.prop("name"), "testname"); }); it("passes through creatablePromptFn to the ReactSelect Creatable component to allow customizing prompt text for new options", () => { const promptFn = (label) => `Add ${label}`; const select = shallow(