/
checkbox-radio.js
55 lines (44 loc) · 1.74 KB
/
checkbox-radio.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//Test that components work with fields that pass target.checked instead of target.value
import React from 'react';
import Form from '../src/form';
import { mount } from 'enzyme';
const Input = ({ error, ...props }) => (
<div>
{error ? <p className="error">{error}</p> : null}
<input {...props} />
</div>
);
test('Input works with checkbox that has no value', () => {
const wrapper = mount(
<Form>
<Input name="Awesome" type="checkbox" />
</Form>
);
wrapper.find('input').simulate('change', {
target: { name: 'Awesome', checked: true, type: 'checkbox' },
});
expect(wrapper.find(Input).props().value).toEqual(true);
wrapper.find('input').simulate('change', {
target: { name: 'Awesome', checked: false, type: 'checkbox' },
});
expect(wrapper.find(Input).props().value).toEqual(false);
});
test('Input works with checkbox that has value', () => {
const wrapper = mount(
<Form>
<Input name="Awesome" type="checkbox" value="impressive" />
<Input name="Awesome" type="checkbox" value="incredible" />
</Form>
);
wrapper.find('input').first().simulate('change', {
target: { name: 'Awesome', checked: true, type: 'checkbox', value: 'impressive' },
});
expect(wrapper.find(Input).first().props().value).toEqual('impressive');
// because we arent intelligently managing this group of inputs the values get overwritten
expect(wrapper.find(Input).last().props().value).toEqual('impressive');
wrapper.find('input').first().simulate('change', {
target: { name: 'Awesome', checked: false, type: 'checkbox', value: 'impressive' },
});
expect(wrapper.find(Input).first().props().value).toEqual('impressive');
expect(wrapper.find(Input).last().props().value).toEqual('impressive');
})