|
| 1 | +/** |
| 2 | + * @jest-environment jsdom |
| 3 | + */ |
| 4 | +import React from 'react'; |
| 5 | +import Enzyme, { mount } from 'enzyme'; |
| 6 | +import Adapter from 'enzyme-adapter-react-16'; |
| 7 | +import SummitDropdown from '..'; |
| 8 | + |
| 9 | +Enzyme.configure({ adapter: new Adapter() }); |
| 10 | + |
| 11 | +const summits = [ |
| 12 | + { id: 1, name: 'Summit A', start_date: 1000 }, |
| 13 | + { id: 2, name: 'Summit B', start_date: 2000 }, |
| 14 | +]; |
| 15 | + |
| 16 | +const defaultProps = { |
| 17 | + summits, |
| 18 | + actionLabel: 'Go', |
| 19 | + actionClass: '', |
| 20 | + onClick: jest.fn(), |
| 21 | +}; |
| 22 | + |
| 23 | +function render(props = {}) { |
| 24 | + return mount(<SummitDropdown {...defaultProps} {...props} />); |
| 25 | +} |
| 26 | + |
| 27 | +describe('SummitDropdown summitValue state', () => { |
| 28 | + beforeEach(() => { |
| 29 | + defaultProps.onClick.mockClear(); |
| 30 | + }); |
| 31 | + |
| 32 | + test('summitValue is null on initial render', () => { |
| 33 | + const wrapper = render(); |
| 34 | + expect(wrapper.instance().state.summitValue).toBeNull(); |
| 35 | + }); |
| 36 | + |
| 37 | + test('handleChange sets summitValue when given a valid object', () => { |
| 38 | + const wrapper = render(); |
| 39 | + const option = { label: 'Summit A', value: 1 }; |
| 40 | + |
| 41 | + wrapper.instance().handleChange(option); |
| 42 | + |
| 43 | + expect(wrapper.instance().state.summitValue).toEqual(option); |
| 44 | + }); |
| 45 | + |
| 46 | + test('handleChange does not set summitValue when given a non-object', () => { |
| 47 | + const wrapper = render(); |
| 48 | + |
| 49 | + wrapper.instance().handleChange('not-an-object'); |
| 50 | + |
| 51 | + expect(wrapper.instance().state.summitValue).toBeNull(); |
| 52 | + }); |
| 53 | + |
| 54 | + test('handleChange does not set summitValue when given null', () => { |
| 55 | + const wrapper = render(); |
| 56 | + |
| 57 | + wrapper.instance().handleChange(null); |
| 58 | + |
| 59 | + expect(wrapper.instance().state.summitValue).toBeNull(); |
| 60 | + }); |
| 61 | + |
| 62 | + test('handleClick does not call onClick when summitValue is null', () => { |
| 63 | + const wrapper = render(); |
| 64 | + const fakeEvent = { preventDefault: jest.fn() }; |
| 65 | + |
| 66 | + // summitValue starts as null — onClick must NOT be called |
| 67 | + wrapper.instance().handleClick(fakeEvent); |
| 68 | + |
| 69 | + expect(defaultProps.onClick).not.toHaveBeenCalled(); |
| 70 | + }); |
| 71 | + |
| 72 | + test('handleClick calls onClick with summit id when summitValue is set', () => { |
| 73 | + const wrapper = render(); |
| 74 | + const option = { label: 'Summit A', value: 1 }; |
| 75 | + const fakeEvent = { preventDefault: jest.fn() }; |
| 76 | + |
| 77 | + wrapper.instance().handleChange(option); |
| 78 | + wrapper.instance().handleClick(fakeEvent); |
| 79 | + |
| 80 | + expect(defaultProps.onClick).toHaveBeenCalledWith(1); |
| 81 | + }); |
| 82 | + |
| 83 | + test('button is disabled when summitValue is null', () => { |
| 84 | + const wrapper = render(); |
| 85 | + expect(wrapper.find('button').prop('disabled')).toBe(true); |
| 86 | + }); |
| 87 | + |
| 88 | + test('button is enabled after selecting a summit', () => { |
| 89 | + const wrapper = render(); |
| 90 | + wrapper.instance().handleChange({ label: 'Summit A', value: 1 }); |
| 91 | + wrapper.update(); |
| 92 | + |
| 93 | + expect(wrapper.find('button').prop('disabled')).toBe(false); |
| 94 | + }); |
| 95 | +}); |
0 commit comments