-
Notifications
You must be signed in to change notification settings - Fork 180
/
radio.test.tsx
102 lines (88 loc) · 2.49 KB
/
radio.test.tsx
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import React, {ComponentRef, Ref} from 'react';
import {Simulate} from 'react-dom/test-utils';
import {shallow, mount} from 'enzyme';
import {RadioProps} from './radio__item';
import Radio from './radio';
describe('Radio', () => {
const factory = (
props?: RadioProps,
refOne?: Ref<ComponentRef<typeof Radio.Item>>,
refTwo?: Ref<ComponentRef<typeof Radio.Item>>,
) => (
<Radio {...props}>
<Radio.Item
ref={refOne}
value="one"
>
{'One'}
</Radio.Item>
<Radio.Item
ref={refTwo}
value="two"
>
{'Two'}
</Radio.Item>
<Radio.Item value="three">{'Three'}</Radio.Item>
</Radio>
);
const shallowRadio = (
props?: RadioProps,
refOne?: Ref<ComponentRef<typeof Radio.Item>>,
refTwo?: Ref<ComponentRef<typeof Radio.Item>>,
) => shallow(factory(props, refOne, refTwo));
it('should create component', () => {
shallowRadio().should.exist;
});
it('should pass only child as is', () => {
const radio = shallow(
<Radio>
<section/>
</Radio>
);
radio.should.have.tagName('section');
});
describe('refs', () => {
const mountRadio = (
props?: RadioProps,
refOne?: Ref<ComponentRef<typeof Radio.Item>>,
refTwo?: Ref<ComponentRef<typeof Radio.Item>>,
) => mount(factory(props, refOne, refTwo));
it('should generate same name for items', () => {
let item1: ComponentRef<typeof Radio.Item>;
let item2: ComponentRef<typeof Radio.Item>;
mountRadio(
{},
itemRef => {
item1 = itemRef!;
},
itemRef2 => {
item2 = itemRef2!;
}
);
const name = item1!.input!.getAttribute('name') ?? undefined;
item2!.input!.should.have.attribute('name', name);
});
it('should select item with value equal to one provided to group', () => {
let item: ComponentRef<typeof Radio.Item>;
mountRadio(
{
onChange: () => {}, // avoid "checked without onChange" warning
value: 'one'
},
itemRef => {
item = itemRef!;
}
);
item!.input!.should.have.property('checked', true);
});
it('should call handler for onChange event', () => {
const onChange = sandbox.spy();
let item: ComponentRef<typeof Radio.Item>;
mountRadio({onChange}, itemRef => {
item = itemRef!;
});
Simulate.change(item!.input!);
onChange.should.have.been.called;
});
});
});