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