|
| 1 | +/* eslint-env jest */ |
| 2 | + |
| 3 | +import React from 'react' |
| 4 | +import { mount } from 'enzyme' |
| 5 | +import CheckboxGroup from '..' |
| 6 | +import LabeledCheckbox from '../../../../LabeledCheckbox/LabeledCheckbox.export' |
| 7 | + |
| 8 | +const mountComponent = (props) => { |
| 9 | + return mount(<CheckboxGroup {...props} />) |
| 10 | +} |
| 11 | + |
| 12 | +const OPTIONS = [ |
| 13 | + { |
| 14 | + key: 'key-a', |
| 15 | + value: 'value-a' |
| 16 | + }, { |
| 17 | + key: 'key-b', |
| 18 | + value: 'value-b' |
| 19 | + }, { |
| 20 | + key: 'key-c', |
| 21 | + value: 'value-c' |
| 22 | + } |
| 23 | +] |
| 24 | + |
| 25 | +describe('<CheckboxGroup />', () => { |
| 26 | + let checkBoxGroupComponent, defaultProps |
| 27 | + |
| 28 | + beforeAll(() => { |
| 29 | + defaultProps = { |
| 30 | + options: OPTIONS, |
| 31 | + checkedOptions: [] |
| 32 | + } |
| 33 | + }) |
| 34 | + |
| 35 | + describe('PROPS', () => { |
| 36 | + describe('Options', () => { |
| 37 | + it('Should render an empty group if options is not given', () => { |
| 38 | + // Arrange |
| 39 | + const props = Object.assign({}, defaultProps, { options: undefined }) |
| 40 | + checkBoxGroupComponent = mountComponent(props) |
| 41 | + |
| 42 | + // Assert |
| 43 | + const renderedOptions = checkBoxGroupComponent.find(LabeledCheckbox) |
| 44 | + expect(renderedOptions).toHaveLength(0) |
| 45 | + }) |
| 46 | + it('Should render an empty group if options is an empty array', () => { |
| 47 | + // Arrange |
| 48 | + const props = Object.assign({}, defaultProps, { options: [] }) |
| 49 | + checkBoxGroupComponent = mountComponent(props) |
| 50 | + |
| 51 | + // Assert |
| 52 | + const renderedOptions = checkBoxGroupComponent.find(LabeledCheckbox) |
| 53 | + expect(renderedOptions).toHaveLength(0) |
| 54 | + }) |
| 55 | + it('Should render the given options as a list of LabeledCheckbox components', () => { |
| 56 | + // Arrange |
| 57 | + checkBoxGroupComponent = mountComponent(defaultProps) |
| 58 | + |
| 59 | + // Assert |
| 60 | + const renderedOptions = checkBoxGroupComponent.find(LabeledCheckbox) |
| 61 | + expect(renderedOptions).toHaveLength(OPTIONS.length) |
| 62 | + renderedOptions.forEach((option, i) => { |
| 63 | + expect(option.text()).toEqual(OPTIONS[i].value) |
| 64 | + }) |
| 65 | + }) |
| 66 | + }) |
| 67 | + |
| 68 | + describe('CheckedOptions', () => { |
| 69 | + it('Should render a list of unchecked LabeledCheckbox components - empty check options', () => { |
| 70 | + // Arrange |
| 71 | + const props = Object.assign({}, defaultProps, { checkedOptions: undefined }) |
| 72 | + checkBoxGroupComponent = mountComponent(props) |
| 73 | + |
| 74 | + // Assert |
| 75 | + const renderedOptions = checkBoxGroupComponent.find(LabeledCheckbox) |
| 76 | + renderedOptions.forEach((option) => { |
| 77 | + expect(option.prop('checked')).toBe(false) |
| 78 | + }) |
| 79 | + }) |
| 80 | + it('Should render a list of unchecked LabeledCheckbox components', () => { |
| 81 | + // Arrange |
| 82 | + checkBoxGroupComponent = mountComponent(defaultProps) |
| 83 | + |
| 84 | + // Assert |
| 85 | + const renderedOptions = checkBoxGroupComponent.find(LabeledCheckbox) |
| 86 | + renderedOptions.forEach((option) => { |
| 87 | + expect(option.prop('checked')).toBe(false) |
| 88 | + }) |
| 89 | + }) |
| 90 | + it('Should render the given checkedOptions as a checked LabeledCheckbox components', () => { |
| 91 | + // Arrange |
| 92 | + const givenCheckedOption = OPTIONS[0] |
| 93 | + const props = Object.assign({}, defaultProps, { checkedOptions: [givenCheckedOption.key] }) |
| 94 | + checkBoxGroupComponent = mountComponent(props) |
| 95 | + |
| 96 | + // Assert |
| 97 | + const renderedOptions = checkBoxGroupComponent.find(LabeledCheckbox) |
| 98 | + renderedOptions.forEach((option) => { |
| 99 | + const isGivenCheckedOption = givenCheckedOption.value.includes(option.text()) |
| 100 | + expect(option.prop('checked')).toBe(isGivenCheckedOption) |
| 101 | + }) |
| 102 | + }) |
| 103 | + }) |
| 104 | + |
| 105 | + describe('ClassName', () => { |
| 106 | + it('Should NOT pass any className property to the wrapper component div if className is not given', () => { |
| 107 | + // Arrange |
| 108 | + checkBoxGroupComponent = mountComponent(defaultProps) |
| 109 | + |
| 110 | + // Assert |
| 111 | + const optionsGroupComponent = checkBoxGroupComponent.find('[data-test="checkbox-group"]') |
| 112 | + expect(optionsGroupComponent.prop('className')).toBe(undefined) |
| 113 | + }) |
| 114 | + |
| 115 | + it('Should pass the given className to the wrapper component div', () => { |
| 116 | + // Arrange |
| 117 | + const givenClassName = 'this-is-className' |
| 118 | + const props = Object.assign({}, defaultProps, { className: givenClassName }) |
| 119 | + checkBoxGroupComponent = mountComponent(props) |
| 120 | + |
| 121 | + // Assert |
| 122 | + const optionsGroupComponent = checkBoxGroupComponent.find('[data-test="checkbox-group"]') |
| 123 | + expect(optionsGroupComponent.prop('className')).toBe(givenClassName) |
| 124 | + }) |
| 125 | + }) |
| 126 | + |
| 127 | + describe('OnChange', () => { |
| 128 | + it('Should call onChange when option checked status is changed - Selected', () => { |
| 129 | + // Arrange |
| 130 | + const checkedOptionIndex = 0 |
| 131 | + const props = Object.assign({}, defaultProps, { onChange: jest.fn() }) |
| 132 | + checkBoxGroupComponent = mountComponent(props) |
| 133 | + |
| 134 | + // Act - check the first option |
| 135 | + const renderedOptions = checkBoxGroupComponent.find(LabeledCheckbox) |
| 136 | + renderedOptions.at(checkedOptionIndex).simulate('click') |
| 137 | + |
| 138 | + // Assert - onChange is called |
| 139 | + expect(props.onChange.mock.calls.length).toBe(1) |
| 140 | + expect(props.onChange.mock.calls[0][0]).toEqual([OPTIONS[checkedOptionIndex].key]) |
| 141 | + }) |
| 142 | + it('Should call onChange when option checked status is changed - Unselected', () => { |
| 143 | + // Arrange |
| 144 | + const checkedOptionIndex = 0 |
| 145 | + const props = Object.assign({}, defaultProps, { |
| 146 | + checkedOptions: [OPTIONS[checkedOptionIndex].key], |
| 147 | + onChange: jest.fn() |
| 148 | + }) |
| 149 | + checkBoxGroupComponent = mountComponent(props) |
| 150 | + |
| 151 | + // Act - un-check the first option |
| 152 | + const renderedOptions = checkBoxGroupComponent.find(LabeledCheckbox) |
| 153 | + expect(renderedOptions.at(checkedOptionIndex).prop('checked')).toBe(true) |
| 154 | + renderedOptions.at(checkedOptionIndex).simulate('click') |
| 155 | + |
| 156 | + // Assert - onChange is called |
| 157 | + expect(props.onChange.mock.calls.length).toBe(1) |
| 158 | + expect(props.onChange.mock.calls[0][0]).toEqual([]) |
| 159 | + }) |
| 160 | + }) |
| 161 | + }) |
| 162 | +}) |
0 commit comments