-
Notifications
You must be signed in to change notification settings - Fork 181
/
radio__item.test.tsx
100 lines (78 loc) · 2.65 KB
/
radio__item.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
import React, {InputHTMLAttributes} from 'react';
import {Simulate} from 'react-dom/test-utils';
import {shallow, mount} from 'enzyme';
import {Radio} from './radio__item';
describe('Radio Item', () => {
function noop() {}
const factory = (props?: InputHTMLAttributes<HTMLInputElement>) => (
<Radio
checked={false}
onChange={noop}
value="test"
{...props}
>
{'test'}
</Radio>
);
const mountRadioItem = (props?: InputHTMLAttributes<HTMLInputElement>) =>
mount<Radio>(factory(props));
const shallowRadioItem = (props?: InputHTMLAttributes<HTMLInputElement>) =>
shallow(factory(props));
it('should create component', () => {
shallowRadioItem().should.exist;
});
it('should render radio item', () => {
mountRadioItem().instance().input!.should.have.property('type', 'radio');
});
it('should generate id if not passed', () => {
mountRadioItem().instance().input!.should.have.property('id');
});
it('should generate unique id', () => {
const firstRadioItem = mountRadioItem();
const secondRadioItem = mountRadioItem();
const secondRadioId = secondRadioItem.instance().input!.getAttribute('id') ?? '';
firstRadioItem.instance().input!.should.not.have.id(secondRadioId);
});
it('should set custom id', () => {
const radioItem = mountRadioItem({
id: 'test'
});
radioItem.instance().input!.should.have.id('test');
});
it('should set name', () => {
const radioItem = mountRadioItem({
name: 'test'
});
radioItem.instance().input!.should.have.property('name', 'test');
});
it('should call handler for click event', () => {
const clickHandler = sandbox.stub();
const radioItem = mountRadioItem({
onClick: clickHandler
});
Simulate.click(radioItem.instance().input!);
clickHandler.should.have.been.called;
});
it('should be unchecked by default', () => {
const radioItem = mountRadioItem();
radioItem.instance().input!.should.not.have.property('checked', true);
});
it('should check control', () => {
const radioItem = mountRadioItem({
checked: true,
onChange: () => {} // avoid "checked without onChange" warning
});
radioItem.instance().input!.should.have.property('checked', true);
});
it('should be disabled', () => {
const radioItem = mountRadioItem({
disabled: true
});
radioItem.instance().input!.should.be.disabled;
});
it('should connect labels with input by id', () => {
const radioItem = mountRadioItem();
const id = radioItem.instance().input!.getAttribute('id') ?? '';
radioItem.instance().label!.should.have.attribute('for', id);
});
});