UNPKG

@shopgate/pwa-common

Version:

Common library for the Shopgate Connect PWA.

89 lines (88 loc) 3 kB
import React from 'react'; import { shallow, mount } from 'enzyme'; import Select from "./index"; import SelectItem from "./components/Item"; import { jsx as _jsx } from "react/jsx-runtime"; describe('<Select />', () => { jest.useFakeTimers(); it('opens and closes the item list', () => { const wrapper = shallow(/*#__PURE__*/_jsx(Select, {})); let previousOpenState = wrapper.state('isOpen'); wrapper.instance().toggleOpenState(); expect(wrapper.state('isOpen')).toBe(!previousOpenState); previousOpenState = wrapper.state('isOpen'); wrapper.instance().toggleOpenState(); expect(wrapper.state('isOpen')).toBe(!previousOpenState); }); it('renders without items', () => { const wrapper = mount(/*#__PURE__*/_jsx(Select, {})); wrapper.instance().toggleOpenState(); expect(wrapper).toMatchSnapshot(); expect(wrapper.find(SelectItem).length).toBe(0); }); it('renders with implicit items (closed)', () => { const items = ['a', 'b', 'c', 'd', 'e', 'f']; const wrapper = mount(/*#__PURE__*/_jsx(Select, { items: items })); expect(wrapper).toMatchSnapshot(); expect(wrapper.find(SelectItem).length).toBe(0); }); it('renders with implicit items (opened)', () => { const items = ['a', 'b', 'c', 'd', 'e', 'f']; const wrapper = mount(/*#__PURE__*/_jsx(Select, { items: items })); wrapper.instance().toggleOpenState(); wrapper.update(); expect(wrapper).toMatchSnapshot(); expect(wrapper.find(SelectItem).length).toBe(items.length); }); it('accepts implicit and explicit items', () => { const items = ['a', 'b', { value: 'c' }, 'd', { value: 'e', label: 'E' }, 'f']; const wrapper = mount(/*#__PURE__*/_jsx(Select, { items: items })); wrapper.instance().toggleOpenState(); wrapper.update(); expect(wrapper).toMatchSnapshot(); expect(wrapper.find(SelectItem).length).toBe(items.length); let i = 0; wrapper.find(SelectItem).forEach(item => { let expectedLabel = items[i]; if (expectedLabel.label) { expectedLabel = expectedLabel.label; } if (expectedLabel.value) { expectedLabel = expectedLabel.value; } expect(item.prop('label')).toBe(expectedLabel); i += 1; }); }); it('triggers callback on change', () => { const items = ['a', 'b', 'c', 'd', 'e', 'f']; const selectionIndex = Math.floor(items.length / 2); /** * Mocked callback for the onSelect event * @param {string} value Mocked value */ const callback = value => { expect(value).toBe(items[selectionIndex]); }; const wrapper = mount(/*#__PURE__*/_jsx(Select, { items: items, onChange: callback })); wrapper.instance().toggleOpenState(); wrapper.update(); expect(wrapper).toMatchSnapshot(); const node = wrapper.find(SelectItem).at(selectionIndex); node.prop('onSelect')(node.prop('value'), node.prop('label')); }); });