import React from "react"; import { mount, shallow } from "enzyme"; import ComboBox from "."; import DropDownItem from "../drop-down-item"; const baseOptions = [ { key: 0, label: "Select", }, { key: 1, label: "Select", }, { key: 2, label: "Select", }, ]; const advancedOptions = ( <> Some text ); const baseProps = { noBorder: false, isDisabled: false, selectedOption: { key: 0, icon: "CatalogFolderIcon", label: "Select", default: true, }, options: baseOptions, opened: false, onSelect: () => jest.fn(), size: "base", scaled: true, }; const toggleDisplayProps = { options: [], selectedOption: { key: 0, label: "Selected option", }, scaled: false, size: "content", displayType: "toggle", }; describe("", () => { it("rendered without error", () => { const wrapper = mount(); expect(wrapper).toExist(); }); it("with advanced options", () => { const wrapper = mount( ); expect(wrapper).toExist(); }); it("disabled", () => { const wrapper = mount(); expect(wrapper.prop("isDisabled")).toEqual(true); }); it("without borders", () => { const wrapper = mount(); expect(wrapper.prop("noBorder")).toEqual(true); }); it("opened", () => { const wrapper = mount(); expect(wrapper.prop("opened")).toEqual(true); }); it("opened without borders", () => { const wrapper = mount( ); expect(wrapper.prop("opened")).toEqual(true); expect(wrapper.prop("noBorder")).toEqual(true); }); it("with DropDown max height", () => { const wrapper = mount(); expect(wrapper.prop("dropDownMaxHeight")).toEqual(200); }); it("without scaled", () => { const wrapper = mount(); expect(wrapper.prop("scaled")).toEqual(false); }); it("scaled", () => { const wrapper = mount(); expect(wrapper.prop("scaled")).toEqual(true); }); it("scaled options", () => { const wrapper = mount(); expect(wrapper.prop("scaledOptions")).toEqual(true); }); it("middle size options", () => { const wrapper = mount( ); expect(wrapper.prop("size")).toEqual("middle"); }); it("big size options", () => { const wrapper = mount( ); expect(wrapper.prop("size")).toEqual("big"); }); it("huge size options", () => { const wrapper = mount( ); expect(wrapper.prop("size")).toEqual("huge"); }); it("by content size options", () => { const wrapper = mount( ); expect(wrapper.prop("size")).toEqual("content"); }); it("with children node", () => { const wrapper = mount(
demo
); expect(wrapper.contains(
demo
)).toBe(true); }); it("not re-render", () => { const wrapper = shallow().instance(); const shouldUpdate = wrapper.shouldComponentUpdate( wrapper.props, wrapper.state ); expect(shouldUpdate).toBe(false); }); it("re-render", () => { const wrapper = shallow().instance(); const shouldUpdate = wrapper.shouldComponentUpdate( { opened: true }, wrapper.state ); expect(shouldUpdate).toBe(true); }); it("causes function comboBoxClick() with disabled prop", () => { const wrapper = shallow(); const instance = wrapper.instance(); instance.comboBoxClick(); expect(wrapper.state("isOpen")).toBe(false); }); it("causes function comboBoxClick()", () => { const wrapper = shallow(); const instance = wrapper.instance(); instance.comboBoxClick(); expect(wrapper.state("isOpen")).toBe(true); }); it("causes function optionClick()", () => { const onSelect = jest.fn(); const selectedOption = { key: 1, label: "Select", }; const wrapper = shallow( ); const instance = wrapper.instance(); instance.optionClick(selectedOption); expect(wrapper.state("isOpen")).toBe(false); expect(onSelect).toHaveBeenCalledWith(selectedOption); }); it("causes function stopAction()", () => { const wrapper = mount(); const instance = wrapper.instance(); instance.stopAction(new Event("click")); expect(wrapper.state("isOpen")).toBe(false); }); it("causes function handleClick() with simulate", () => { const wrapper = mount(); wrapper.simulate("click"); expect(wrapper.state("isOpen")).toBe(false); }); it("causes function handleClick() with simulate and ComboBox not opened", () => { const wrapper = mount(); wrapper.simulate("click"); expect(wrapper.state("isOpen")).toBe(true); }); it("componentDidUpdate() lifecycle test", () => { const wrapper = shallow(); const instance = wrapper.instance(); const newSelected = { key: 1, label: "Select" }; jest.spyOn(instance, "setIsOpen"); wrapper.setProps({ opened: true, }); expect(wrapper.props().opened).toBe(true); wrapper.setProps({ opened: false, }); expect(wrapper.props().opened).toBe(false); instance.componentDidUpdate( { opened: true, selectedOption: newSelected, }, { isOpen: true, } ); instance.forceUpdate(); //Need for manual re-render, enzyme issue expect(instance.setIsOpen).toHaveBeenCalled(); }); it("accepts id", () => { const wrapper = mount(); expect(wrapper.prop("id")).toEqual("testId"); }); it("accepts className", () => { const wrapper = mount(); expect(wrapper.prop("className")).toEqual("test"); }); it("accepts style", () => { const wrapper = mount(); expect(wrapper.getDOMNode().style).toHaveProperty("color", "red"); }); it("render like toggle displayType", () => { const onToggleClick = jest.fn(); const wrapper = mount( ); expect(wrapper.prop("displayType")).toEqual("toggle"); }); it("click on toggle", () => { const onToggleClick = jest.fn(); const wrapper = mount( ); jest.spyOn(wrapper.instance(), "setIsOpen"); wrapper.simulate("click"); expect(onToggleClick).toHaveBeenCalled(); }); it("click outside", () => { const onToggleClick = jest.fn(); const wrapper = mount( ); const instance = wrapper.instance(); jest.spyOn(instance, "handleClickOutside"); instance.handleClickOutside(new Event("click")); //TODO: rework with simulation expect(wrapper.state("isOpen")).toBe(false); expect(wrapper.prop("opened")).toBe(true); expect(instance.handleClickOutside).toHaveBeenCalled(); expect(onToggleClick).toHaveBeenCalled(); }); });