|
1 | 1 | import React from 'react';
|
| 2 | +import { act } from 'react-dom/test-utils'; |
2 | 3 | import { mount } from 'enzyme';
|
3 | 4 |
|
4 | 5 | import { FormRenderer, componentTypes } from '@data-driven-forms/react-form-renderer';
|
@@ -47,4 +48,97 @@ describe('<Checkbox />', () => {
|
47 | 48 | .props().labelText
|
48 | 49 | ).toEqual('option 2');
|
49 | 50 | });
|
| 51 | + |
| 52 | + it('selects item in multiple checkbox', async () => { |
| 53 | + const schema = { |
| 54 | + fields: [ |
| 55 | + { |
| 56 | + component: componentTypes.CHECKBOX, |
| 57 | + name: 'check', |
| 58 | + label: 'Please select on of options', |
| 59 | + options: [ |
| 60 | + { |
| 61 | + label: 'option 1', |
| 62 | + value: 'option-1' |
| 63 | + }, |
| 64 | + { |
| 65 | + label: 'option 2', |
| 66 | + value: 'option-2' |
| 67 | + } |
| 68 | + ] |
| 69 | + } |
| 70 | + ] |
| 71 | + }; |
| 72 | + const eventCheck = { |
| 73 | + target: { |
| 74 | + checked: true, |
| 75 | + type: 'checkbox' |
| 76 | + } |
| 77 | + }; |
| 78 | + const eventUncheck = { |
| 79 | + target: { |
| 80 | + checked: false, |
| 81 | + type: 'checkbox' |
| 82 | + } |
| 83 | + }; |
| 84 | + |
| 85 | + const submitSpy = jest.fn(); |
| 86 | + |
| 87 | + const wrapper = mount( |
| 88 | + <FormRenderer |
| 89 | + onSubmit={(values) => submitSpy(values)} |
| 90 | + FormTemplate={(props) => <FormTemplate {...props} />} |
| 91 | + schema={schema} |
| 92 | + componentMapper={componentMapper} |
| 93 | + /> |
| 94 | + ); |
| 95 | + |
| 96 | + await act(async () => { |
| 97 | + wrapper |
| 98 | + .find('input') |
| 99 | + .first() |
| 100 | + .simulate('change', eventCheck); |
| 101 | + }); |
| 102 | + wrapper.update(); |
| 103 | + |
| 104 | + await act(async () => { |
| 105 | + wrapper.find('form').simulate('submit'); |
| 106 | + }); |
| 107 | + wrapper.update(); |
| 108 | + |
| 109 | + expect(submitSpy).toHaveBeenCalledWith({ check: ['option-1'] }); |
| 110 | + submitSpy.mockClear(); |
| 111 | + |
| 112 | + await act(async () => { |
| 113 | + wrapper |
| 114 | + .find('input') |
| 115 | + .last() |
| 116 | + .simulate('change', eventCheck); |
| 117 | + }); |
| 118 | + wrapper.update(); |
| 119 | + |
| 120 | + await act(async () => { |
| 121 | + wrapper.find('form').simulate('submit'); |
| 122 | + }); |
| 123 | + wrapper.update(); |
| 124 | + |
| 125 | + expect(submitSpy).toHaveBeenCalledWith({ check: ['option-1', 'option-2'] }); |
| 126 | + submitSpy.mockClear(); |
| 127 | + |
| 128 | + await act(async () => { |
| 129 | + wrapper |
| 130 | + .find('input') |
| 131 | + .first() |
| 132 | + .simulate('change', eventUncheck); |
| 133 | + }); |
| 134 | + wrapper.update(); |
| 135 | + |
| 136 | + await act(async () => { |
| 137 | + wrapper.find('form').simulate('submit'); |
| 138 | + }); |
| 139 | + wrapper.update(); |
| 140 | + |
| 141 | + expect(submitSpy).toHaveBeenCalledWith({ check: ['option-2'] }); |
| 142 | + submitSpy.mockClear(); |
| 143 | + }); |
50 | 144 | });
|
0 commit comments