1 | import React from 'react';
|
2 | import { render, screen } from '@testing-library/react';
|
3 | import '@testing-library/jest-dom';
|
4 | import { Input } from '..';
|
5 | import { testForDefaultTag } from '../testUtils';
|
6 |
|
7 | describe('Input', () => {
|
8 | it('should render with "input" tag when no type is provided', () => {
|
9 | testForDefaultTag(Input, 'input');
|
10 | });
|
11 |
|
12 | it('should render with "type" tag when type is "select"', () => {
|
13 | const { container } = render(<Input type="select">Yo!</Input>);
|
14 | expect(container.querySelector('select')).toBeInTheDocument();
|
15 | });
|
16 |
|
17 | it('should render with "textarea" tag when type is "textarea"', () => {
|
18 | render(<Input type="textarea" data-testid="input" />);
|
19 |
|
20 | expect(screen.getByTestId('input').tagName.toLowerCase()).toMatch(
|
21 | 'textarea',
|
22 | );
|
23 | });
|
24 |
|
25 | it('should render with "input" tag when plaintext prop is truthy', () => {
|
26 | render(<Input type="select" plaintext data-testid="input" />);
|
27 |
|
28 | expect(screen.getByTestId('input').tagName.toLowerCase()).toMatch('input');
|
29 | });
|
30 |
|
31 | it('should render with "form-control-plaintext" class when plaintext prop is truthy', () => {
|
32 | render(<Input type="select" plaintext data-testid="input" />);
|
33 |
|
34 | expect(screen.getByTestId('input')).toHaveClass('form-control-plaintext');
|
35 | });
|
36 |
|
37 | it('should not render with "form-control" class when plaintext prop is truthy', () => {
|
38 | render(<Input type="select" plaintext data-testid="input" />);
|
39 | expect(screen.getByTestId('input')).not.toHaveClass('form-control');
|
40 | });
|
41 |
|
42 | it('should render with custom tag when plaintext prop is truthy and tag is provided', () => {
|
43 | render(<Input type="select" plaintext tag="div" data-testid="input" />);
|
44 | expect(screen.getByTestId('input').tagName.toLowerCase()).toMatch('div');
|
45 | });
|
46 |
|
47 | it('should render with custom tag when plaintext prop is not truthy and tag is provided', () => {
|
48 | render(<Input tag="div" data-testid="input" />);
|
49 | expect(screen.getByTestId('input').tagName.toLowerCase()).toMatch('div');
|
50 | });
|
51 |
|
52 | it('should render with "input" tag when type is not a special case', () => {
|
53 | render(<Input type="email" data-testid="input" />);
|
54 | expect(screen.getByTestId('input').tagName.toLowerCase()).toMatch('input');
|
55 | });
|
56 |
|
57 | it('should not render children', () => {
|
58 | render(<Input>Yo!</Input>);
|
59 | expect(screen.queryByText('Yo!')).not.toBeInTheDocument();
|
60 | });
|
61 |
|
62 | it('should render without children when type is "textarea"', () => {
|
63 | render(<Input type="textarea">Yo!</Input>);
|
64 | expect(screen.queryByText('Yo!')).not.toBeInTheDocument();
|
65 | });
|
66 |
|
67 | it('should render children when type is select', () => {
|
68 | render(<Input type="select">Yo!</Input>);
|
69 | expect(screen.getByText('Yo!')).toBeInTheDocument();
|
70 | });
|
71 |
|
72 | it('should render children when tag is select', () => {
|
73 | render(<Input tag="select">Yo!</Input>);
|
74 | expect(screen.getByText('Yo!')).toBeInTheDocument();
|
75 | });
|
76 |
|
77 | it('should pass children when tag is a custom component', () => {
|
78 | render(<Input tag={(props) => props.children}>Yo!</Input>);
|
79 | expect(screen.getByText('Yo!')).toBeInTheDocument();
|
80 | });
|
81 |
|
82 | it('should not render with "is-invalid" class when valid is false', () => {
|
83 | render(<Input valid={false} data-testid="input" />);
|
84 | expect(screen.getByTestId('input')).not.toHaveClass('is-invalid');
|
85 | });
|
86 |
|
87 | it('should not render with "is-valid" class when invalid is false', () => {
|
88 | render(<Input invalid={false} data-testid="input" />);
|
89 | expect(screen.getByTestId('input')).not.toHaveClass('is-valid');
|
90 | });
|
91 |
|
92 | it('should render with "is-invalid" class when invalid is true', () => {
|
93 | render(<Input invalid data-testid="input" />);
|
94 | expect(screen.getByTestId('input')).toHaveClass('is-invalid');
|
95 | });
|
96 |
|
97 | it('should render with "is-valid" class when valid is true', () => {
|
98 | render(<Input valid data-testid="input" />);
|
99 | expect(screen.getByTestId('input')).toHaveClass('is-valid');
|
100 | });
|
101 |
|
102 | it('should render with "form-control-${bsSize}" class when bsSize is "lg" or "sm"', () => {
|
103 | render(<Input bsSize="lg" data-testid="input" />);
|
104 | expect(screen.getByTestId('input')).toHaveClass('form-control-lg');
|
105 | });
|
106 |
|
107 | it('should render with "form-select-${bsSize}" class when bsSize is "lg" or "sm" and type is select', () => {
|
108 | render(<Input type="select" bsSize="lg" data-testid="input" />);
|
109 | expect(screen.getByTestId('input')).toHaveClass('form-select-lg');
|
110 | });
|
111 |
|
112 | it('should render with "form-control" class when size is nor "lg" nor "sm"', () => {
|
113 | render(<Input bsSize="5" data-testid="input" />);
|
114 | expect(screen.getByTestId('input')).toHaveClass('form-control');
|
115 | expect(screen.getByTestId('input')).not.toHaveClass('form-control-sm');
|
116 | expect(screen.getByTestId('input')).not.toHaveClass('form-control-lg');
|
117 | });
|
118 |
|
119 | it('should render with "form-select" class when size is nor "lg" nor "sm" and type is select', () => {
|
120 | render(<Input type="select" bsSize="5" data-testid="input" />);
|
121 | expect(screen.getByTestId('input')).toHaveClass('form-select');
|
122 | expect(screen.getByTestId('input')).not.toHaveClass(
|
123 | 'form-select-sm form-select-lg',
|
124 | );
|
125 | });
|
126 |
|
127 | it('should render with "form-control-${bsSize}" class when bsSize is provided', () => {
|
128 | render(<Input bsSize="sm" data-testid="input" />);
|
129 | expect(screen.getByTestId('input')).toHaveClass('form-control-sm');
|
130 | });
|
131 |
|
132 | it('should render with "form-select-${bsSize}" class when bsSize is provided and type is select', () => {
|
133 | render(<Input type="select" bsSize="sm" data-testid="input" />);
|
134 | expect(screen.getByTestId('input')).toHaveClass('form-select-sm');
|
135 | });
|
136 |
|
137 | it('should render with "form-control" class by default', () => {
|
138 | render(<Input data-testid="input" />);
|
139 | expect(screen.getByTestId('input')).toHaveClass('form-control');
|
140 | });
|
141 |
|
142 | it('should not render with "form-control-plaintext" nor "form-check-input" class by default', () => {
|
143 | render(<Input data-testid="input" />);
|
144 | expect(screen.getByTestId('input')).not.toHaveClass(
|
145 | 'form-control-plaintext',
|
146 | );
|
147 | expect(screen.getByTestId('input')).not.toHaveClass('form-check-input');
|
148 | });
|
149 |
|
150 | it('should not render with "form-control-plaintext" nor "form-check-input" class when type is file', () => {
|
151 | render(<Input type="file" data-testid="input" />);
|
152 | expect(screen.getByTestId('input')).not.toHaveClass(
|
153 | 'form-control-plaintext',
|
154 | );
|
155 | expect(screen.getByTestId('input')).not.toHaveClass('form-check-input');
|
156 | });
|
157 |
|
158 | it('should not render with "form-control" nor "form-control-plaintext" nor "form-check-input" class when type is select', () => {
|
159 | render(<Input type="select" data-testid="input" />);
|
160 | expect(screen.getByTestId('input')).not.toHaveClass('form-control');
|
161 | expect(screen.getByTestId('input')).not.toHaveClass(
|
162 | 'form-control-plaintext',
|
163 | );
|
164 | expect(screen.getByTestId('input')).not.toHaveClass('form-check-input');
|
165 | });
|
166 |
|
167 | it('should not render with "form-control" nor "form-check-input" class when plaintext prop is truthy', () => {
|
168 | render(<Input type="file" plaintext data-testid="input" />);
|
169 | expect(screen.getByTestId('input')).not.toHaveClass('form-control');
|
170 | expect(screen.getByTestId('input')).not.toHaveClass('form-check-input');
|
171 | });
|
172 | it('should not render nor "form-control-plaintext" nor "form-control" class when type is radio', () => {
|
173 | render(<Input type="radio" data-testid="input" />);
|
174 | expect(screen.getByTestId('input')).not.toHaveClass('form-control');
|
175 | expect(screen.getByTestId('input')).not.toHaveClass(
|
176 | 'form-control-plaintext',
|
177 | );
|
178 | });
|
179 |
|
180 | it('should not render nor "form-control-plaintext" nor "form-control" class when type is checkbox', () => {
|
181 | render(<Input type="checkbox" data-testid="input" />);
|
182 |
|
183 | expect(screen.getByTestId('input')).not.toHaveClass('form-control');
|
184 | expect(screen.getByTestId('input')).not.toHaveClass(
|
185 | 'form-control-plaintext',
|
186 | );
|
187 | });
|
188 |
|
189 | it('should render with "form-check-input" class when type is checkbox', () => {
|
190 | render(<Input type="checkbox" data-testid="input" />);
|
191 | expect(screen.getByTestId('input')).toHaveClass('form-check-input');
|
192 | });
|
193 |
|
194 | it('should render with "form-check-input" class when type is radio', () => {
|
195 | render(<Input type="radio" data-testid="input" />);
|
196 | expect(screen.getByTestId('input')).toHaveClass('form-check-input');
|
197 | });
|
198 |
|
199 | it('should render with "form-check-input" class when type is switch', () => {
|
200 | render(<Input type="switch" data-testid="input" />);
|
201 | expect(screen.getByTestId('input')).toHaveClass('form-check-input');
|
202 | });
|
203 |
|
204 | it('should not render with "form-check-input" nor "form-control" class when type is checkbox and addon is truthy', () => {
|
205 | render(<Input addon type="checkbox" data-testid="input" />);
|
206 | expect(screen.getByTestId('input')).not.toHaveClass('form-check-input');
|
207 | expect(screen.getByTestId('input')).not.toHaveClass('form-control');
|
208 | });
|
209 |
|
210 | it('should not render with "form-check-input" nor "form-control" class when type is radio and addon is truthy', () => {
|
211 | render(<Input addon type="radio" data-testid="input" />);
|
212 | expect(screen.getByTestId('input')).not.toHaveClass('form-check-input');
|
213 | expect(screen.getByTestId('input')).not.toHaveClass('form-control');
|
214 | });
|
215 |
|
216 | it('should render with "form-select" class when type is select', () => {
|
217 | render(<Input type="select" data-testid="input" />);
|
218 | expect(screen.getByTestId('input')).toHaveClass('form-select');
|
219 | });
|
220 |
|
221 | it('should render with "form-control" class when type is file', () => {
|
222 | render(<Input type="file" data-testid="input" />);
|
223 | expect(screen.getByTestId('input')).toHaveClass('form-control');
|
224 | });
|
225 |
|
226 | it('should render additional classes', () => {
|
227 | render(<Input className="other" data-testid="input" />);
|
228 | expect(screen.getByTestId('input')).toHaveClass('other');
|
229 | });
|
230 |
|
231 | it('should render checkbox type when type is switch', () => {
|
232 | render(<Input type="switch" data-testid="input" />);
|
233 | expect(screen.getByTestId('input')).toHaveAttribute('type', 'checkbox');
|
234 | });
|
235 |
|
236 | it('should render "select" and "textarea" without type property', () => {
|
237 | render(<Input type="select">Yo!</Input>);
|
238 | render(<Input type="textarea" data-testid="input" />);
|
239 |
|
240 | expect(screen.getByTestId('input')).not.toHaveAttribute('type');
|
241 | expect(screen.getByText('Yo!')).not.toHaveAttribute('type');
|
242 | });
|
243 |
|
244 | it('should render with "form-range" not "form-control" class when type is range', () => {
|
245 | render(<Input type="range" data-testid="input" />);
|
246 |
|
247 | expect(screen.getByTestId('input')).toHaveClass('form-range');
|
248 | expect(screen.getByTestId('input')).not.toHaveClass('form-control');
|
249 | });
|
250 | });
|