|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { act, create } from 'react-test-renderer'; |
| 3 | +import PropTypes from 'prop-types'; |
| 4 | + |
| 5 | +import CheckboxButtonGroup from 'src/CheckboxButtonGroup'; |
| 6 | +import CheckboxButton from 'src/CheckboxButton'; |
| 7 | + |
| 8 | +const CheckboxButtonGroupComponent = ({ children, defaultValues }) => { |
| 9 | + const [value, setValue] = useState(defaultValues); |
| 10 | + return ( |
| 11 | + <CheckboxButtonGroup id="checkbox-question" value={value} onChange={setValue}> |
| 12 | + {children} |
| 13 | + </CheckboxButtonGroup> |
| 14 | + ); |
| 15 | +}; |
| 16 | + |
| 17 | +CheckboxButtonGroupComponent.propTypes = { |
| 18 | + defaultValues: PropTypes.array.isRequired, |
| 19 | +}; |
| 20 | + |
| 21 | +describe('CheckboxButtonGroup', () => { |
| 22 | + test('sets initial value properly', () => { |
| 23 | + const checkboxButtonGroup = create( |
| 24 | + <CheckboxButtonGroupComponent defaultValues={[1, 2]}> |
| 25 | + <CheckboxButton id="first" label="First value" value={1} /> |
| 26 | + <CheckboxButton id="second" label="Second value" value={2} /> |
| 27 | + </CheckboxButtonGroupComponent>, |
| 28 | + ); |
| 29 | + |
| 30 | + expect(checkboxButtonGroup).toMatchSnapshot(); |
| 31 | + }); |
| 32 | + |
| 33 | + test('will update array state to check a new item', async () => { |
| 34 | + const checkboxButtonGroup = create( |
| 35 | + <CheckboxButtonGroupComponent defaultValues={[]}> |
| 36 | + <CheckboxButton id="first" label="First value" value={1} /> |
| 37 | + <CheckboxButton id="second" label="Second value" value={2} /> |
| 38 | + </CheckboxButtonGroupComponent>, |
| 39 | + ); |
| 40 | + |
| 41 | + const event = { target: { value: 1, checked: true } }; |
| 42 | + const firstButton = checkboxButtonGroup.root.findAllByType((CheckboxButton))[0]; |
| 43 | + |
| 44 | + await act(async () => firstButton.props.onChange(event)); |
| 45 | + expect(checkboxButtonGroup).toMatchSnapshot(); |
| 46 | + }); |
| 47 | + |
| 48 | + test('will update array state to uncheck an item', async () => { |
| 49 | + const checkboxButtonGroup = create( |
| 50 | + <CheckboxButtonGroupComponent defaultValues={[1]}> |
| 51 | + <CheckboxButton id="first" label="First value" value={1} /> |
| 52 | + <CheckboxButton id="second" label="Second value" value={2} /> |
| 53 | + </CheckboxButtonGroupComponent>, |
| 54 | + ); |
| 55 | + |
| 56 | + const event = { target: { value: 1, checked: false } }; |
| 57 | + const firstButton = checkboxButtonGroup.root.findAllByType(CheckboxButton)[0]; |
| 58 | + |
| 59 | + await act(async () => firstButton.props.onChange(event)); |
| 60 | + expect(checkboxButtonGroup).toMatchSnapshot(); |
| 61 | + }); |
| 62 | +}); |
0 commit comments