|
1 | 1 | import React from 'react'; |
2 | | -import {configure, mount, shallow} from 'enzyme'; |
3 | | -import Adapter from '@wojtekmaj/enzyme-adapter-react-17'; |
| 2 | +import { render, fireEvent } from '@testing-library/react'; |
| 3 | +import '@testing-library/jest-dom'; |
4 | 4 | import EasyCheckbox from "./EasyCheckbox"; |
5 | | -import EasyEdit from "./EasyEdit"; |
6 | | -import Globals from './globals'; |
7 | | - |
8 | | -configure({adapter: new Adapter()}); |
9 | 5 |
|
10 | 6 | describe('EasyCheckbox', () => { |
11 | | - let wrapper; |
12 | | - const onChange = jest.fn(); |
13 | | - const options = [{label: 'Test One', value: '1'}, |
14 | | - {label: 'Test Two', value: '2'}]; |
| 7 | + const options = [ |
| 8 | + { label: 'Option 1', value: 'option1' }, |
| 9 | + { label: 'Option 2', value: 'option2' }, |
| 10 | + { label: 'Option 3', value: 'option3' }, |
| 11 | + ]; |
15 | 12 |
|
16 | | - beforeEach(() => { |
17 | | - wrapper = shallow( |
18 | | - <EasyCheckbox |
19 | | - options={options} |
20 | | - onChange={onChange} |
21 | | - value={options} |
22 | | - /> |
23 | | - ); |
24 | | - }); |
| 13 | + it('renders checkboxes with labels', () => { |
| 14 | + const { getByLabelText } = render(<EasyCheckbox options={options} />); |
25 | 15 |
|
26 | | - it('should render two checkboxes', () => { |
27 | | - expect(wrapper.find('input')).toHaveLength(2); |
| 16 | + options.forEach(option => { |
| 17 | + const checkbox = getByLabelText(option.label); |
| 18 | + expect(checkbox).toBeInTheDocument(); |
| 19 | + expect(checkbox).toHaveAttribute('type', 'checkbox'); |
| 20 | + expect(checkbox).toHaveAttribute('value', option.value); |
| 21 | + }); |
28 | 22 | }); |
29 | 23 |
|
30 | | - it("should render a placeholder", () => { |
31 | | - let wrapper = shallow( |
32 | | - <EasyEdit |
33 | | - type="checkbox" |
34 | | - options={options} |
35 | | - onSave={jest.fn()} |
36 | | - /> |
37 | | - ); |
38 | | - expect(wrapper.find('div.easy-edit-wrapper').text()).toEqual(Globals.DEFAULT_PLACEHOLDER); |
39 | | - }); |
| 24 | + it('renders checked checkboxes', () => { |
| 25 | + const value = ['option1', 'option3']; |
| 26 | + const { getByLabelText } = render(<EasyCheckbox options={options} value={value} />); |
40 | 27 |
|
41 | | - it("should render the selected label for a given value", () => { |
42 | | - let wrapper = shallow( |
43 | | - <EasyEdit |
44 | | - type="checkbox" |
45 | | - options={options} |
46 | | - onSave={jest.fn()} |
47 | | - value="1" |
48 | | - /> |
49 | | - ); |
50 | | - expect(wrapper.find('div.easy-edit-wrapper').text()).toEqual("Test One"); |
| 28 | + options.forEach(option => { |
| 29 | + const checkbox = getByLabelText(option.label); |
| 30 | + if (value.includes(option.value)) { |
| 31 | + expect(checkbox).toBeChecked(); |
| 32 | + } else { |
| 33 | + expect(checkbox).not.toBeChecked(); |
| 34 | + } |
| 35 | + }); |
51 | 36 | }); |
52 | 37 |
|
53 | | - it("should render the value of the checkbox even if it's not part ", () => { |
54 | | - const val = "I am not part of the options list"; |
55 | | - let wrapper = shallow( |
56 | | - <EasyEdit |
57 | | - type="checkbox" |
58 | | - options={options} |
59 | | - onSave={jest.fn()} |
60 | | - value={val} |
61 | | - /> |
62 | | - ); |
63 | | - expect(wrapper.find('div.easy-edit-wrapper').text()).toEqual(val); |
64 | | - }); |
| 38 | + it('calls onChange handler when a checkbox is clicked', () => { |
| 39 | + const onChange = jest.fn(); |
| 40 | + const { getByLabelText } = render(<EasyCheckbox options={options} onChange={onChange} />); |
65 | 41 |
|
66 | | - it("should render all selected values as a comma separated list", () => { |
67 | | - let wrapper = shallow( |
68 | | - <EasyEdit |
69 | | - type="checkbox" |
70 | | - options={options} |
71 | | - onSave={jest.fn()} |
72 | | - value="1,2" |
73 | | - /> |
74 | | - ); |
75 | | - expect(wrapper.find('div.easy-edit-wrapper').text()).toEqual("Test One, Test Two"); |
| 42 | + const checkbox = getByLabelText('Option 1'); |
| 43 | + fireEvent.click(checkbox); |
| 44 | + expect(onChange).toHaveBeenCalledWith(expect.objectContaining({target: expect.objectContaining({value: 'option1'})})); |
76 | 45 | }); |
77 | 46 |
|
78 | | - it("uncheck all options", () => { |
79 | | - wrapper = mount( |
80 | | - <EasyEdit |
81 | | - type="checkbox" |
82 | | - options={options} |
83 | | - onSave={jest.fn()} |
84 | | - value={["1","2"]} |
85 | | - />); |
86 | | - wrapper.simulate('click'); |
87 | | - wrapper.find('input[type="checkbox"]').at(0).simulate('change', {currentTarget: {checked: false}}); |
88 | | - wrapper.find('input[type="checkbox"]').at(1).simulate('change', {currentTarget: {checked: false}}); |
89 | | - expect(wrapper.state('tempValue')).toEqual([]); |
90 | | - }); |
| 47 | + it('calls onFocus and onBlur handlers when a checkbox is focused and blurred', () => { |
| 48 | + const onFocus = jest.fn(); |
| 49 | + const onBlur = jest.fn(); |
| 50 | + const { getByLabelText } = render(<EasyCheckbox options={options} onFocus={onFocus} onBlur={onBlur} />); |
| 51 | + |
| 52 | + const checkbox = getByLabelText('Option 1'); |
| 53 | + fireEvent.focus(checkbox); |
| 54 | + expect(onFocus).toHaveBeenCalled(); |
91 | 55 |
|
92 | | - it("should remove (on onSave) any values that are not part of the option list", () => { |
93 | | - wrapper = mount( |
94 | | - <EasyEdit |
95 | | - type="checkbox" |
96 | | - options={options} |
97 | | - onSave={jest.fn()} |
98 | | - value={["1", "3", "4"]} |
99 | | - />); |
100 | | - wrapper.simulate('click'); |
101 | | - wrapper.find('input[type="checkbox"]').at(0).simulate('change', {currentTarget: {checked: false}}); |
102 | | - wrapper.find('button[name="save"]').simulate('click'); |
103 | | - expect(wrapper.state('value')).toEqual([]); |
| 56 | + fireEvent.blur(checkbox); |
| 57 | + expect(onBlur).toHaveBeenCalled(); |
104 | 58 | }); |
105 | 59 | }); |
0 commit comments