import React from 'react'; import { render, mount } from 'enzyme'; import DropDownIcon from 'components/Icon/icons/arrows/dropdown'; import VMenuIcon from 'components/Icon/icons/content/vmenu'; import MultiDropdown from '../index'; const items = [ { value: 'value_1', label: 'Label 1', }, { value: 'value_2', label: 'Label 2', disabled: true, }, ]; const itemsNoDisabled = [ { value: 'value_1', label: 'Label 1', }, { value: 'value_2', label: 'Label 2', }, ]; const itemsWithSubmenu = [ { value: 'TH', label: 'Thailand', options: [ { value: 'TH_BKK', label: 'Bangkok', }, { value: 'TH_CNX', label: 'Chiang Mai', }, ], }, { value: 'PH', label: 'Philippines', options: [ { value: 'PH_MNL', label: 'Manila', disabled: true, }, { value: 'PH_CEB', label: 'Cebu', }, ], }, ]; // TODO: add more test cases for Dropdown component describe('MultiDropdown', () => { describe('Snapshots', () => { it('render', () => { const wrapper = render(); expect(wrapper).toMatchSnapshot(); }); it('render', () => { const wrapper = render(); expect(wrapper).toMatchSnapshot(); }); }); describe('Render', () => { it('not to throw', () => { expect(() => mount()).not.toThrow(); }); it('enabled', () => { const wrapper = mount(); expect(wrapper.find('button').props().disabled).toBe(false); }); it('disabled', () => { const wrapper = mount(); expect(wrapper.find('button').props().disabled).toBe(true); }); it('default variant - default icon', () => { const wrapper = mount(); expect(wrapper.find('Memo(DropDownIcon)')).toHaveLength(1); }); it('compact variant - default icon', () => { const wrapper = mount(); expect(wrapper.find('Memo(VMenuIcon)')).toHaveLength(1); }); it('default variant - custom icon', () => { const wrapper = mount( } /> ); expect(wrapper.find('Memo(VMenuIcon)')).toHaveLength(1); }); it('compact variant - custom icon', () => { const wrapper = mount( } /> ); expect(wrapper.find('Memo(DropDownIcon)')).toHaveLength(1); }); }); describe('Functions', () => { it('select first option with keyboard by Enter', () => { const onChange = jest.fn(); const wrapper = mount( ); const button = wrapper.find('button'); button.simulate('keydown', { key: ' ' }); button.simulate('keydown', { key: 'ArrowDown' }); button.simulate('keydown', { key: 'Enter' }); expect(onChange).toHaveBeenCalledTimes(1); expect(onChange).toHaveBeenCalledWith(items[0], [items[0]]); }); it('select first option with keyboard by spacebar', () => { const onChange = jest.fn(); const wrapper = mount( ); const button = wrapper.find('button'); button.simulate('keydown', { key: ' ' }); button.simulate('keydown', { key: 'ArrowDown' }); button.simulate('keydown', { key: ' ' }); expect(onChange).toHaveBeenCalledTimes(1); expect(onChange).toHaveBeenCalledWith(items[0], [items[0]]); }); it('does not select disabled second option with keyboard', () => { const onChange = jest.fn(); const wrapper = mount( ); const button = wrapper.find('button'); button.simulate('keydown', { key: ' ' }); button.simulate('keydown', { key: 'ArrowDown' }); button.simulate('keydown', { key: 'ArrowDown' }); button.simulate('keydown', { key: ' ' }); expect(onChange).toHaveBeenCalledTimes(0); }); it('calls onToggle when dropdown menu is toggled', () => { const onToggle = jest.fn(); const wrapper = mount( ); const button = wrapper.find('button'); button.simulate('click'); expect(onToggle).toHaveBeenCalledWith(true); button.simulate('click'); expect(onToggle).toHaveBeenCalledWith(false); }); it('select item when dropdown menu itme is clicked', () => { const onChange = jest.fn(); const wrapper = mount( ); const button = wrapper.find('button'); button.simulate('click'); const itemLi = wrapper.find('li'); itemLi.at(0).simulate('click'); expect(onChange).toHaveBeenCalledWith(itemsNoDisabled[0], [ itemsNoDisabled[0], ]); itemLi.at(1).simulate('click'); expect(onChange).toHaveBeenCalledWith(itemsNoDisabled[1], [ itemsNoDisabled[0], itemsNoDisabled[1], ]); }); it('reset when dropdown menu reset button is clicked', () => { const onChange = jest.fn(); const wrapper = mount( ); const button = wrapper.find('button'); button.simulate('click'); const itemLi = wrapper.find('li'); itemLi.at(0).simulate('click'); expect(onChange).toHaveBeenCalledWith(itemsNoDisabled[0], [ itemsNoDisabled[0], ]); itemLi.at(1).simulate('click'); expect(onChange).toHaveBeenCalledWith(itemsNoDisabled[1], [ itemsNoDisabled[0], itemsNoDisabled[1], ]); itemLi.at(2).simulate('click'); expect(onChange).toHaveBeenCalledWith({}, []); }); }); });