-
Notifications
You must be signed in to change notification settings - Fork 32
/
TextArea.test.tsx
219 lines (185 loc) · 5.33 KB
/
TextArea.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
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
import React from 'react'
import '@testing-library/jest-dom/extend-expect'
import { render, RenderResult } from '@testing-library/react'
import withFormik from '../../enhancers/withFormik'
import FieldProps from '../../types/FieldProps'
import FormProps from '../../types/FormProps'
import { TextArea } from './index'
describe('TextArea', () => {
let field: FieldProps
let form: FormProps
let label = ''
let textInput: RenderResult
beforeEach(() => {
field = {
name: 'colour',
value: '',
onBlur: null,
onChange: jest.fn()
}
form = {
errors: {},
touched: {},
}
})
describe('when a field has no errors', () => {
beforeEach(() => {
form.errors = {}
})
describe('and the field has a label', () => {
beforeEach(() => {
label = 'Colour'
})
describe('and the field has a value', () => {
beforeEach(() => {
field.value = 'Green'
textInput = render(
<TextArea
value={field.value}
name={field.name}
onChange={field.onChange}
label={label}
/>
)
})
it('should render a field with a label', () => {
expect(textInput.getByTestId('textarea-label')).toHaveTextContent(
'Colour'
)
})
it('should indicate that the field has content so the field is shrunk', () => {
expect(textInput.queryByTestId('textarea-container')).toHaveClass(
'has-content'
)
})
it('should populate the field value', () => {
expect(textInput.queryByTestId('textarea-input')).toHaveTextContent(
'Green'
)
})
})
})
describe('and the field has no label but a placeholder', () => {
beforeEach(() => {
textInput = render(
<TextArea
value={field.value}
name={field.name}
onChange={field.onChange}
placeholder="Search"
/>
)
})
it('should not include a label', () => {
expect(textInput.queryByTestId('textarea-label')).toBeNull()
})
it('should adjust the styles to signify there is no label', () => {
expect(textInput.queryByTestId('textarea-container')).toHaveClass(
'no-label'
)
})
it('should include the placeholder in the field', () => {
expect(textInput.getByTestId('textarea-input')).toHaveAttribute(
'placeholder',
'Search'
)
})
})
})
describe('when a Formik enhanced field has an error', () => {
beforeEach(() => {
form.errors = {
colour: 'Something bad',
}
})
describe('and the form has not been touched', () => {
beforeEach(() => {
form.touched = {}
const FormikTextInput = withFormik(TextArea)
textInput = render(<FormikTextInput field={field} form={form} />)
})
it('should not indicate the field has an error', () => {
expect(textInput.queryByTestId('textarea-container')).not.toHaveClass(
'is-invalid'
)
})
})
describe('and the form has been touched', () => {
beforeEach(() => {
form.touched.colour = true
const FormikTextInput = withFormik(TextArea)
textInput = render(<FormikTextInput field={field} form={form} />)
})
it('should indicate the field has an error', () => {
expect(textInput.queryByTestId('textarea-container')).toHaveClass(
'is-invalid'
)
})
})
})
describe('when an additional class it provided', () => {
beforeEach(() => {
textInput = render(
<TextArea
className="test"
value={field.value}
name={field.name}
onChange={field.onChange}
label={label}
/>
)
})
it('should attach the class to the wrapper', () => {
expect(textInput.queryByTestId('textarea-container')).toHaveClass('test')
})
})
describe('when an id is provided', () => {
beforeEach(() => {
textInput = render(
<TextArea
id="test"
value={field.value}
name={field.name}
onChange={field.onChange}
label={label}
/>
)
})
it('should attach the id to the field', () => {
expect(textInput.queryByTestId('textarea-input')).toHaveAttribute(
'id',
'test'
)
})
it('should associate the label to the field with the custom id', () => {
expect(textInput.queryByTestId('textarea-label')).toHaveAttribute(
'for',
'test'
)
})
})
describe('when the onBlur callback is provided', () => {
let onBlurSpy: jest.SpyInstance
beforeEach(() => {
field.onBlur = () => {
return true
}
onBlurSpy = jest.spyOn(field, 'onBlur')
textInput = render(
<div>
<TextArea {...field} />
<input type="text" data-testid="next-field" />
</div>
)
})
describe('when the text area loses focus', () => {
beforeEach(() => {
textInput.getByTestId('textarea-input').focus()
textInput.getByTestId('next-field').focus()
})
it('should call the onBlur callback once', () => {
expect(onBlurSpy).toHaveBeenCalledTimes(1)
})
})
})
})