|
| 1 | +import React from 'react'; |
| 2 | +import { shallow, ShallowWrapper } from 'enzyme'; |
| 3 | +import ItemList, { Props } from '../ItemList'; |
| 4 | +import { Collaborator } from '../../../@types'; |
| 5 | + |
| 6 | +describe('components/ItemList', () => { |
| 7 | + const defaults: Props<Collaborator> = { |
| 8 | + items: [ |
| 9 | + { id: 'testid1', name: 'test1' }, |
| 10 | + { id: 'testid2', name: 'test2' }, |
| 11 | + ], |
| 12 | + onSelect: jest.fn(), |
| 13 | + }; |
| 14 | + |
| 15 | + const getWrapper = (props = {}): ShallowWrapper => shallow(<ItemList {...defaults} {...props} />); |
| 16 | + |
| 17 | + describe('render()', () => { |
| 18 | + test('should render elements with correct props', () => { |
| 19 | + const wrapper = getWrapper({ activeItemIndex: 1 }); |
| 20 | + |
| 21 | + const itemList = wrapper.find('[data-testid="ba-ItemList"]'); |
| 22 | + const firstChild = itemList.childAt(0); |
| 23 | + const secondChild = itemList.childAt(1); |
| 24 | + |
| 25 | + expect(itemList.props()).toMatchObject({ |
| 26 | + role: 'listbox', |
| 27 | + }); |
| 28 | + expect(itemList.children()).toHaveLength(2); |
| 29 | + expect(firstChild.prop('className')).toEqual('ba-ItemList-row'); |
| 30 | + expect(firstChild.prop('isActive')).toEqual(false); |
| 31 | + expect(secondChild.prop('isActive')).toEqual(true); |
| 32 | + }); |
| 33 | + |
| 34 | + test('should trigger events', () => { |
| 35 | + const mockSetIndex = jest.fn(); |
| 36 | + const mockEvent = { |
| 37 | + preventDefault: jest.fn(), |
| 38 | + }; |
| 39 | + |
| 40 | + const wrapper = getWrapper({ onActivate: mockSetIndex }); |
| 41 | + const firstChild = wrapper.find('[data-testid="ba-ItemList"]').childAt(0); |
| 42 | + |
| 43 | + firstChild.simulate('click', mockEvent); |
| 44 | + expect(defaults.onSelect).toBeCalledWith(0, mockEvent); |
| 45 | + |
| 46 | + firstChild.simulate('mousedown', mockEvent); |
| 47 | + expect(mockEvent.preventDefault).toBeCalled(); |
| 48 | + |
| 49 | + firstChild.simulate('mouseenter'); |
| 50 | + expect(mockSetIndex).toBeCalledWith(0); |
| 51 | + }); |
| 52 | + }); |
| 53 | +}); |
0 commit comments