-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
FormControlSpec.tsx
157 lines (128 loc) · 4.31 KB
/
FormControlSpec.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
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
import * as React from 'react';
import { render } from '@testing-library/react';
import FormControl from '../src/FormControl';
import FormGroup from '../src/FormGroup';
import { shouldWarn } from './helpers';
describe('<FormControl>', () => {
it('should render correctly', () => {
const { getByTestId } = render(
<FormControl
type="text"
id="foo"
name="bar"
className="my-control"
data-testid="test-id"
/>,
);
const element = getByTestId('test-id');
element.tagName.toLowerCase().should.equal('input');
element.id.should.equal('foo');
element.classList.length.should.equal(2);
element.classList.contains('form-control').should.be.true;
element.classList.contains('my-control').should.be.true;
element.getAttribute('name')!.should.equal('bar');
element.getAttribute('type')!.should.equal('text');
});
it('should support textarea', () => {
const { getByTestId } = render(
<FormControl as="textarea" data-testid="test-id" />,
);
getByTestId('test-id').tagName.toLowerCase().should.equal('textarea');
});
it('should support plaintext inputs', () => {
const { getByTestId } = render(
<FormControl plaintext data-testid="test-id" />,
);
const element = getByTestId('test-id');
element.classList.length.should.equal(1);
element.classList.contains('form-control-plaintext').should.be.true;
});
it('should support plaintext inputs with size', () => {
const { getByTestId } = render(
<FormControl plaintext size="sm" data-testid="test-id" />,
);
const element = getByTestId('test-id');
element.classList.length.should.equal(2);
element.classList.contains('form-control-sm').should.be.true;
});
it('should support type=color', () => {
const { getByTestId } = render(
<FormControl type="color" data-testid="test-id" />,
);
getByTestId('test-id').getAttribute('type')!.should.equal('color');
});
it('should use controlId for id', () => {
const { getByTestId } = render(
<FormGroup controlId="foo">
<FormControl type="text" data-testid="test-id" />
</FormGroup>,
);
getByTestId('test-id').id.should.equal('foo');
});
it('should prefer explicit id', () => {
shouldWarn('ignored');
const { getByTestId } = render(
<FormGroup controlId="foo">
<FormControl type="text" id="bar" data-testid="test-id" />
</FormGroup>,
);
getByTestId('test-id').id.should.equal('bar');
});
it('should support ref forwarding', () => {
let input;
class Container extends React.Component {
render() {
return (
<FormGroup controlId="foo">
<FormControl
type="text"
ref={(ref) => {
input = ref;
}}
/>
</FormGroup>
);
}
}
render(<Container />);
input.tagName.toLowerCase().should.to.equal('input');
});
it('should properly display size of FormControl', () => {
const { getByTestId } = render(
<FormControl type="text" size="lg" data-testid="test-id" />,
);
const element = getByTestId('test-id');
element.classList.length.should.equal(2);
element.classList.contains('form-control-lg').should.be.true;
});
it('should properly display html size of FormControl', () => {
const { getByTestId } = render(
<FormControl type="text" htmlSize={42} data-testid="test-id" />,
);
getByTestId('test-id').getAttribute('size')!.should.equal('42');
});
it('Should have input as default component', () => {
const { getByTestId } = render(<FormControl data-testid="test-id" />);
getByTestId('test-id').tagName.toLowerCase().should.equal('input');
});
it('should support numbers as values', () => {
const { getByTestId } = render(
<FormControl
value={10}
onChange={() => undefined}
data-testid="test-id"
/>,
);
getByTestId('test-id').getAttribute('value')!.should.equal('10');
});
it('should support an array of strings as values', () => {
const { getByTestId } = render(
<FormControl
value={['hello', 'world']}
onChange={() => undefined}
data-testid="test-id"
/>,
);
getByTestId('test-id').getAttribute('value')!.should.equal('hello,world');
});
});