import Select from "../../src/components/Select";
describe("Shallow ", () => {
beforeEach(() => {
mock.options = [
{ id: 3, text: 'three' },
{ id: 4, text: 'four' },
{ id: 5, text: 'five' },
{ id: 6, text: 'six' },
{ id: 7, text: 'seven' },
{ id: 8, text: 'eight' },
{ id: 9, text: 'nine' },
{ id: 10, text: 'ten' },
{ id: 11, text: 'eleven' },
{ id: 12, text: 'twelve' },
];
});
it("should render with options in the state", () => {
const component = shallow();
expect(component.state("options")).toHaveLength(10);
});
it("should save options' object structure", () => {
const optionsList = mock.options.map(option => ({
additionalKey: true,
...option
}));
const component = shallow();
expect(
component.state("options").every(option => option.additionalKey)
).toBe(true);
});
it("should update state.value when new value after props update", () => {
const component = shallow(
);
component.setProps({ value: 4 });
expect(component.state("value")).toBe("4");
});
});
describe("Render ", () => {
it("should render a Select with default props", () => {
const component = render();
expect(component).toMatchSnapshot();
});
});
describe("Mount ", () => {
it("should not render a Select clear button without choosen option and should render it on option select", () => {
const component = mount(
);
expect(component.find(cssName + "__clear-selection")).toHaveLength(1);
component.setProps({ value: null });
expect(component.find(cssName + "__clear-selection")).toHaveLength(0);
});
it("should open dropdown on when state.dropdownOpened = true", () => {
const component = mount();
expect(component.setState({ dropdownOpened: true })).toMatchSnapshot();
});
it("should render Select with default option and show placeholder after reseting value", () => {
const component = mount(
);
const selection = component.find(cssName + "__selection-text");
expect(selection.text()).toBe("three");
component.instance().onClearSelection();
expect(selection.text()).toBe("Select something");
});
it("should render a disabled unclickable Select with choosen option", () => {
const makeClick = sinon.spy();
const component = mount(
);
expect(component).toMatchSnapshot();
component.find(cssName + "__container").simulate("click");
expect(makeClick.calledOnce).toBe(false);
});
it("should change css class name if prop passed", () => {
const component = mount();
expect(component).toMatchSnapshot();
expect(component.find(".TestSelector__container")).toHaveLength(1);
expect(component.find(".TestSelector__selection")).toHaveLength(1);
component.setState({ dropdownOpened: true });
expect(component.find(".TestSelector__dropdown")).toHaveLength(1);
});
it("should use optionRenderer if passed to render SelectOptionsList", () => {
const spy = sinon.spy(mock, "renderer");
const component = mount(
);
component.setState({ dropdownOpened: true });
expect(spy.callCount).toBe(mock.options.length);
});
describe('Search input', () => {
const findSearchInput = wrapper => wrapper.find(cssName + "__search-field");
it("renders when search.show prop is true", () => {
const component = mount(
);
component.setState({ dropdownOpened: true });
expect(findSearchInput(component)).toHaveLength(1);
});
it('does not render when there is no search.show is falsy', () => {
const component = mount(
);
component.setState({ dropdownOpened: true });
expect(findSearchInput(component)).toHaveLength(0);
});
it("does not render when there is no options", () => {
const component = mount(