|
| 1 | +import { mount } from '@vue/test-utils'; |
| 2 | +import RadioInput from './RadioInput.vue'; |
| 3 | +import { FieldControl, RadioField, Validator, required } from '../../index'; |
| 4 | + |
| 5 | +describe('RadioInput', () => { |
| 6 | + let cmp; |
| 7 | + |
| 8 | + beforeEach(() => { |
| 9 | + cmp = mount(RadioInput, { |
| 10 | + props: { |
| 11 | + control: FieldControl({ |
| 12 | + name: 'test-radio', |
| 13 | + ...RadioField({ |
| 14 | + label: 'Test Radio', |
| 15 | + options: [ |
| 16 | + { |
| 17 | + key: 'mario', |
| 18 | + value: 'Mario', |
| 19 | + }, |
| 20 | + { |
| 21 | + key: 'crash-bandicoot', |
| 22 | + value: 'Crash Bandicoot', |
| 23 | + }, |
| 24 | + { |
| 25 | + key: 'sonic', |
| 26 | + value: 'Sonic', |
| 27 | + }, |
| 28 | + { |
| 29 | + key: 'banjo-kazooie', |
| 30 | + value: 'Banjo Kazooie', |
| 31 | + disabled: true, |
| 32 | + }, |
| 33 | + ], |
| 34 | + }), |
| 35 | + }), |
| 36 | + }, |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + it('renders an group of inputs of type radio', () => { |
| 41 | + const group = cmp.find('.radio-group'); |
| 42 | + const radios = cmp.findAll('.radio-control'); |
| 43 | + expect(group.exists()).toBe(true); |
| 44 | + expect(radios.length).toBeGreaterThan(0); |
| 45 | + }); |
| 46 | + |
| 47 | + it(`renders an input with class 'radio-control'`, () => { |
| 48 | + const input = cmp.find('input'); |
| 49 | + expect(input.classes()).toContain('radio-control'); |
| 50 | + }); |
| 51 | + |
| 52 | + it(`renders an input with id equal to field name`, () => { |
| 53 | + const input = cmp.find('[type=radio][value=crash-bandicoot]'); |
| 54 | + expect(input.attributes('id')).toBe('crash-bandicoot'); |
| 55 | + }); |
| 56 | + |
| 57 | + // TODO: Open ticket with reproduction link on https://github.com/vuejs/vue-test-utils-next/issues |
| 58 | + /* |
| 59 | + it(`input gets checked when value is true`, async () => { |
| 60 | + await cmp.setProps({ |
| 61 | + control: FieldControl({ |
| 62 | + name: 'test-input', |
| 63 | + ...RadioField({ |
| 64 | + label: 'Test Radio', |
| 65 | + value: true, |
| 66 | + }), |
| 67 | + }), |
| 68 | + }); |
| 69 | + const input = cmp.find('.radio-control'); |
| 70 | + expect(input.attributes('checked')).toBe(''); |
| 71 | + }); |
| 72 | +
|
| 73 | + it(`input gets disabled when form control does it`, async () => { |
| 74 | + await cmp.setProps({ |
| 75 | + control: FieldControl({ |
| 76 | + name: 'test-input', |
| 77 | + ...RadioField({ |
| 78 | + label: 'Test Input', |
| 79 | + disabled: true, |
| 80 | + }), |
| 81 | + }), |
| 82 | + }); |
| 83 | + const input = cmp.find('.radio-control'); |
| 84 | + expect(input.attributes('disabled')).toBe(''); |
| 85 | + }); |
| 86 | +
|
| 87 | + it(`renders a required input`, async () => { |
| 88 | + await cmp.setProps({ |
| 89 | + control: FieldControl({ |
| 90 | + name: 'test-input', |
| 91 | + ...RadioField({ |
| 92 | + label: 'Test Input', |
| 93 | + validations: [ |
| 94 | + Validator({ validator: required, text: 'This field is required' }), |
| 95 | + ], |
| 96 | + }), |
| 97 | + }), |
| 98 | + }); |
| 99 | + const input = cmp.find('.radio-control'); |
| 100 | + expect(input.attributes('required')).toBe(''); |
| 101 | + }); |
| 102 | +
|
| 103 | + it(`sets ariaRequired when required`, async () => { |
| 104 | + await cmp.setProps({ |
| 105 | + control: FieldControl({ |
| 106 | + name: 'test-input', |
| 107 | + ...RadioField({ |
| 108 | + label: 'Test Input', |
| 109 | + validations: [ |
| 110 | + Validator({ validator: required, text: 'This field is required' }), |
| 111 | + ], |
| 112 | + }), |
| 113 | + }), |
| 114 | + }); |
| 115 | + const input = cmp.find('.radio-control'); |
| 116 | + expect(input.attributes('ariarequired')).toBe('true'); |
| 117 | + }); |
| 118 | +
|
| 119 | + it(`renders a readonly input`, async () => { |
| 120 | + await cmp.setProps({ |
| 121 | + control: FieldControl({ |
| 122 | + name: 'test-input', |
| 123 | + ...RadioField({ |
| 124 | + label: 'Test Input', |
| 125 | + readonly: true, |
| 126 | + }), |
| 127 | + }), |
| 128 | + }); |
| 129 | + const input = cmp.find('.radio-control'); |
| 130 | + expect(input.attributes('readonly')).toBe(''); |
| 131 | + }); |
| 132 | +
|
| 133 | + it(`renders an input with aria labels`, async () => { |
| 134 | + await cmp.setProps({ |
| 135 | + control: FieldControl({ |
| 136 | + name: 'test-input', |
| 137 | + ...RadioField({ |
| 138 | + label: 'Test Input', |
| 139 | + ariaLabel: 'Im a test input', |
| 140 | + }), |
| 141 | + }), |
| 142 | + }); |
| 143 | + const input = cmp.find('.radio-control'); |
| 144 | + expect(input.attributes('arialabel')).toBe('Im a test input'); |
| 145 | + }); |
| 146 | +
|
| 147 | + it('emits an event when value changed', async () => { |
| 148 | + const input = cmp.find('input[type=radio][value=crash-bandicoot]'); |
| 149 | + await input.setValue(); |
| 150 | +
|
| 151 | + expect(cmp.emitted()).toHaveProperty('change'); |
| 152 | + expect(cmp.emitted('change')[0][0].value).toBe(true); |
| 153 | + }); |
| 154 | +
|
| 155 | + it('emits the control name when value change', async () => { |
| 156 | + const input = cmp.find('input[type=radio][value=crash-bandicoot]'); |
| 157 | + await input.setValue(); |
| 158 | +
|
| 159 | + expect(cmp.emitted('change')[0][0].name).toBe('test-radio'); |
| 160 | + }); */ |
| 161 | + |
| 162 | + it('emits an event when blur', async () => { |
| 163 | + const input = cmp.find('input[type=radio][value=crash-bandicoot]'); |
| 164 | + await input.trigger('blur'); |
| 165 | + |
| 166 | + expect(cmp.emitted()).toHaveProperty('blur'); |
| 167 | + }); |
| 168 | + |
| 169 | + it('emits an event when focus', async () => { |
| 170 | + const input = cmp.find('input[type=radio][value=crash-bandicoot]'); |
| 171 | + await input.trigger('focus'); |
| 172 | + |
| 173 | + expect(cmp.emitted()).toHaveProperty('focus'); |
| 174 | + }); |
| 175 | +}); |
0 commit comments