|
| 1 | +import React from 'react'; |
| 2 | +import { mount } from 'enzyme'; |
| 3 | +import { act } from 'react-dom/test-utils'; |
| 4 | + |
| 5 | +import { FormRenderer, componentTypes } from '@data-driven-forms/react-form-renderer'; |
| 6 | + |
| 7 | +import FormTemplate from '../form-template'; |
| 8 | +import componentMapper from '../component-mapper'; |
| 9 | +import { TextInput, NumberInput } from 'carbon-components-react'; |
| 10 | + |
| 11 | +describe('<TextInput and NumberInput />', () => { |
| 12 | + let onChange; |
| 13 | + let wrapper; |
| 14 | + let submitSpy; |
| 15 | + beforeEach(() => { |
| 16 | + submitSpy = jest.fn(); |
| 17 | + onChange = jest.fn(); |
| 18 | + }); |
| 19 | + afterEach(() => { |
| 20 | + onChange.mockReset(); |
| 21 | + submitSpy.mockReset(); |
| 22 | + }); |
| 23 | + it('renders NumberInput', () => { |
| 24 | + const schema = { |
| 25 | + fields: [ |
| 26 | + { |
| 27 | + component: componentTypes.TEXT_FIELD, |
| 28 | + name: 'input', |
| 29 | + label: 'Please enter a value', |
| 30 | + type: 'number', |
| 31 | + step: 1, |
| 32 | + initialValue: 1 |
| 33 | + } |
| 34 | + ] |
| 35 | + }; |
| 36 | + |
| 37 | + const wrapper = mount( |
| 38 | + <FormRenderer |
| 39 | + onSubmit={(values) => submitSpy(values)} |
| 40 | + FormTemplate={(props) => <FormTemplate {...props} />} |
| 41 | + schema={schema} |
| 42 | + componentMapper={componentMapper} |
| 43 | + /> |
| 44 | + ); |
| 45 | + |
| 46 | + expect(wrapper.find(NumberInput)).toHaveLength(1); |
| 47 | + }); |
| 48 | + |
| 49 | + it('NumberInput - click on increment button', async () => { |
| 50 | + const schema = { |
| 51 | + fields: [ |
| 52 | + { |
| 53 | + component: componentTypes.TEXT_FIELD, |
| 54 | + name: 'input', |
| 55 | + label: 'Please enter a value', |
| 56 | + type: 'number', |
| 57 | + step: 1, |
| 58 | + initialValue: 1 |
| 59 | + } |
| 60 | + ] |
| 61 | + }; |
| 62 | + await act(async () => { |
| 63 | + wrapper = mount( |
| 64 | + <FormRenderer |
| 65 | + onSubmit={(values) => submitSpy(values)} |
| 66 | + FormTemplate={(props) => <FormTemplate {...props} />} |
| 67 | + schema={schema} |
| 68 | + componentMapper={componentMapper} |
| 69 | + /> |
| 70 | + ); |
| 71 | + }); |
| 72 | + |
| 73 | + expect(wrapper.find(NumberInput)).toHaveLength(1); |
| 74 | + expect(wrapper.find('input')).toHaveLength(1); |
| 75 | + |
| 76 | + await act(async () => { |
| 77 | + wrapper |
| 78 | + .find('button.up-icon') |
| 79 | + .first() |
| 80 | + .simulate('click'); |
| 81 | + }); |
| 82 | + await act(async () => { |
| 83 | + wrapper.find('form').simulate('submit'); |
| 84 | + }); |
| 85 | + wrapper.update(); |
| 86 | + expect(submitSpy).toHaveBeenCalledWith({ input: '2' }); |
| 87 | + }); |
| 88 | + |
| 89 | + it('NumberInput - click on decrement button ', async () => { |
| 90 | + const schema = { |
| 91 | + fields: [ |
| 92 | + { |
| 93 | + component: componentTypes.TEXT_FIELD, |
| 94 | + name: 'input', |
| 95 | + label: 'Please enter a value', |
| 96 | + type: 'number', |
| 97 | + step: 1, |
| 98 | + initialValue: 5 |
| 99 | + } |
| 100 | + ] |
| 101 | + }; |
| 102 | + await act(async () => { |
| 103 | + wrapper = mount( |
| 104 | + <FormRenderer |
| 105 | + onSubmit={(values) => submitSpy(values)} |
| 106 | + FormTemplate={(props) => <FormTemplate {...props} />} |
| 107 | + schema={schema} |
| 108 | + componentMapper={componentMapper} |
| 109 | + /> |
| 110 | + ); |
| 111 | + }); |
| 112 | + |
| 113 | + expect(wrapper.find(NumberInput)).toHaveLength(1); |
| 114 | + expect(wrapper.find('input')).toHaveLength(1); |
| 115 | + |
| 116 | + await act(async () => { |
| 117 | + wrapper |
| 118 | + .find('button.down-icon') |
| 119 | + .first() |
| 120 | + .simulate('click'); |
| 121 | + }); |
| 122 | + await act(async () => { |
| 123 | + wrapper.find('form').simulate('submit'); |
| 124 | + }); |
| 125 | + wrapper.update(); |
| 126 | + expect(submitSpy).toHaveBeenCalledWith({ input: '4' }); |
| 127 | + }); |
| 128 | + |
| 129 | + it('renders TextInput', () => { |
| 130 | + const schema = { |
| 131 | + fields: [ |
| 132 | + { |
| 133 | + component: componentTypes.TEXT_FIELD, |
| 134 | + name: 'input', |
| 135 | + label: 'Please enter a value', |
| 136 | + initialValue: 'test' |
| 137 | + } |
| 138 | + ] |
| 139 | + }; |
| 140 | + |
| 141 | + const wrapper = mount( |
| 142 | + <FormRenderer |
| 143 | + onSubmit={(values) => submitSpy(values)} |
| 144 | + FormTemplate={(props) => <FormTemplate {...props} />} |
| 145 | + schema={schema} |
| 146 | + componentMapper={componentMapper} |
| 147 | + /> |
| 148 | + ); |
| 149 | + |
| 150 | + expect(wrapper.find(TextInput)).toHaveLength(1); |
| 151 | + }); |
| 152 | +}); |
0 commit comments