/
Input.spec.js
272 lines (189 loc) · 9.47 KB
/
Input.spec.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import React from 'react';
import { shallow, mount } from 'enzyme';
import { Input } from '../';
describe('Input', () => {
it('should render with "input" tag when no type is provided', () => {
const wrapper = shallow(<Input />);
expect(wrapper.type()).toBe('input');
});
it('should render with "type" tag when type is "select"', () => {
const wrapper = shallow(<Input type="select">Yo!</Input>);
expect(wrapper.type()).toBe('select');
});
it('should render with "textarea" tag when type is "textarea"', () => {
const wrapper = shallow(<Input type="textarea" />);
expect(wrapper.type()).toBe('textarea');
});
it('should render with "input" tag when plaintext prop is truthy', () => {
const wrapper = shallow(<Input type="select" plaintext />);
expect(wrapper.type()).toBe('input');
});
it('should render with "form-control-plaintext" class when plaintext prop is truthy', () => {
const wrapper = shallow(<Input type="select" plaintext />);
expect(wrapper.hasClass('form-control-plaintext')).toBe(true);
});
it('should not render with "form-control" class when plaintext prop is truthy', () => {
const wrapper = shallow(<Input type="select" plaintext />);
expect(wrapper.hasClass('form-control')).toBe(false);
});
it('should render with custom tag when plaintext prop is truthy and tag is provided', () => {
const wrapper = shallow(<Input type="select" plaintext tag="div" />);
expect(wrapper.type()).toBe('div');
});
it('should render with custom tag when plaintext prop is not truthy and tag is provided', () => {
const wrapper = shallow(<Input tag="div" />);
expect(wrapper.type()).toBe('div');
});
it('should render with "input" tag when type is not a special case', () => {
const wrapper = shallow(<Input type="email" />);
expect(wrapper.type()).toBe('input');
});
it('should not render children', () => {
const wrapper = shallow(<Input>Yo!</Input>);
expect(wrapper.text()).toBe('');
});
it('should render without children when type is "textarea"', () => {
const wrapper = shallow(<Input type="textarea">Yo!</Input>);
expect(wrapper.text()).toBe('');
});
it('should render children when type is select', () => {
const wrapper = shallow(<Input type="select">Yo!</Input>);
expect(wrapper.text()).toBe('Yo!');
});
it('should render children when tag is select', () => {
const wrapper = shallow(<Input tag="select">Yo!</Input>);
expect(wrapper.text()).toBe('Yo!');
});
it('should pass children when tag is a custom component', () => {
const wrapper = mount(<Input tag={props => props.children}>Yo!</Input>);
expect(wrapper.text()).toBe('Yo!');
});
it('should not render with "is-invalid" class when valid is false', () => {
const wrapper = shallow(<Input valid={false} />);
expect(wrapper.hasClass('is-invalid')).toBe(false);
});
it('should not render with "is-valid" class when invalid is false', () => {
const wrapper = shallow(<Input invalid={false} />);
expect(wrapper.hasClass('is-valid')).toBe(false);
});
it('should render with "is-invalid" class when invalid is true', () => {
const wrapper = shallow(<Input invalid />);
expect(wrapper.hasClass('is-invalid')).toBe(true);
});
it('should render with "is-valid" class when valid is true', () => {
const wrapper = shallow(<Input valid />);
expect(wrapper.hasClass('is-valid')).toBe(true);
});
it('should render with "form-control-${bsSize}" class when bsSize is "lg" or "sm"', () => {
const wrapper = shallow(<Input bsSize="lg" />);
expect(wrapper.hasClass('form-control-lg')).toBe(true);
});
it('should render with "form-select-${bsSize}" class when bsSize is "lg" or "sm" and type is select', () => {
const wrapper = shallow(<Input type="select" bsSize="lg" />);
expect(wrapper.hasClass('form-select-lg')).toBe(true);
});
it('should render with "form-control" class when size is nor "lg" nor "sm"', () => {
const wrapper = shallow(<Input bsSize="5" />);
expect(wrapper.hasClass('form-control-sm')).toBe(false);
expect(wrapper.hasClass('form-control-lg')).toBe(false);
expect(wrapper.hasClass('form-control')).toBe(true);
});
it('should render with "form-select" class when size is nor "lg" nor "sm" and type is select', () => {
const wrapper = shallow(<Input type="select" bsSize="5" />);
expect(wrapper.hasClass('form-select-sm')).toBe(false);
expect(wrapper.hasClass('form-select-lg')).toBe(false);
expect(wrapper.hasClass('form-select')).toBe(true);
});
it('should render with "form-control-${bsSize}" class when bsSize is provided', () => {
const wrapper = shallow(<Input bsSize="sm" />);
expect(wrapper.hasClass('form-control-sm')).toBe(true);
});
it('should render with "form-select-${bsSize}" class when bsSize is provided and type is select', () => {
const wrapper = shallow(<Input type="select" bsSize="sm" />);
expect(wrapper.hasClass('form-select-sm')).toBe(true);
});
it('should render with "form-control" class by default', () => {
const wrapper = shallow(<Input />);
expect(wrapper.hasClass('form-control')).toBe(true);
});
it('should not render with "form-control-plaintext" nor "form-check-input" class by default', () => {
const wrapper = shallow(<Input />);
expect(wrapper.hasClass('form-control-plaintext')).toBe(false);
expect(wrapper.hasClass('form-check-input')).toBe(false);
});
it('should not render with "form-control-plaintext" nor "form-check-input" class when type is file', () => {
const wrapper = shallow(<Input type="file" />);
expect(wrapper.hasClass('form-control-plaintext')).toBe(false);
expect(wrapper.hasClass('form-check-input')).toBe(false);
});
it('should not render with "form-control" nor "form-control-plaintext" nor "form-check-input" class when type is select', () => {
const wrapper = shallow(<Input type="select" />);
expect(wrapper.hasClass('form-control')).toBe(false);
expect(wrapper.hasClass('form-control-plaintext')).toBe(false);
expect(wrapper.hasClass('form-check-input')).toBe(false);
});
it('should not render with "form-control" nor "form-check-input" class when plaintext prop is truthy', () => {
const wrapper = shallow(<Input type="file" plaintext />);
expect(wrapper.hasClass('form-control')).toBe(false);
expect(wrapper.hasClass('form-check-input')).toBe(false);
});
it('should not render nor "form-control-plaintext" nor "form-control" class when type is radio', () => {
const wrapper = shallow(<Input type="radio" />);
expect(wrapper.hasClass('form-control-plaintext')).toBe(false);
expect(wrapper.hasClass('form-control')).toBe(false);
});
it('should not render nor "form-control-plaintext" nor "form-control" class when type is checkbox', () => {
const wrapper = shallow(<Input type="checkbox" />);
expect(wrapper.hasClass('form-control-plaintext')).toBe(false);
expect(wrapper.hasClass('form-control')).toBe(false);
});
it('should render with "form-check-input" class when type is checkbox', () => {
const wrapper = shallow(<Input type="checkbox" />);
expect(wrapper.hasClass('form-check-input')).toBe(true);
});
it('should render with "form-check-input" class when type is radio', () => {
const wrapper = shallow(<Input type="radio" />);
expect(wrapper.hasClass('form-check-input')).toBe(true);
});
it('should render with "form-check-input" class when type is switch', () => {
const wrapper = shallow(<Input type="switch" />);
expect(wrapper.hasClass('form-check-input')).toBe(true);
});
it('should not render with "form-check-input" nor "form-control" class when type is checkbox and addon is truthy', () => {
const wrapper = shallow(<Input addon type="checkbox" />);
expect(wrapper.hasClass('form-check-input')).toBe(false);
expect(wrapper.hasClass('form-control')).toBe(false);
});
it('should not render with "form-check-input" nor "form-control" class when type is radio and addon is truthy', () => {
const wrapper = shallow(<Input addon type="radio" />);
expect(wrapper.hasClass('form-check-input')).toBe(false);
expect(wrapper.hasClass('form-control')).toBe(false);
});
it('should render with "form-select" class when type is select', () => {
const wrapper = shallow(<Input type="select" />);
expect(wrapper.hasClass('form-select')).toBe(true);
});
it('should render with "form-control" class when type is file', () => {
const wrapper = shallow(<Input type="file" />);
expect(wrapper.hasClass('form-control')).toBe(true);
});
it('should render additional classes', () => {
const wrapper = shallow(<Input className="other" />);
expect(wrapper.hasClass('other')).toBe(true);
});
it('should render checkbox type when type is switch', () => {
const input = shallow(<Input type="switch" />);
expect(input.find('[type="checkbox"]').exists()).toBe(true);
});
it('should render "select" and "textarea" without type property', () => {
const input = shallow(<Input type="select">Yo!</Input>);
const textarea = shallow(<Input type="textarea" />);
expect(input.find('[type="select"]').exists()).toBe(false);
expect(textarea.find('[type="textarea"]').exists()).toBe(false);
});
it('should render with "form-range" not "form-control" class when type is range', () => {
const wrapper = shallow(<Input type="range" />);
expect(wrapper.hasClass('form-range')).toBe(true);
expect(wrapper.hasClass('form-control')).toBe(false);
});
});