-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathDateInput2.test.tsx
More file actions
419 lines (359 loc) · 12.2 KB
/
DateInput2.test.tsx
File metadata and controls
419 lines (359 loc) · 12.2 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 - present Instructure, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import { useState } from 'react'
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
import { vi, MockInstance } from 'vitest'
import userEvent from '@testing-library/user-event'
import '@testing-library/jest-dom'
import { IconHeartLine } from '@instructure/ui-icons'
import { DateInput2 } from '../index'
import { TextInput } from '@instructure/ui-text-input'
const LABEL_TEXT = 'Choose a date'
const DateInputExample = () => {
const [inputValue, setInputValue] = useState('')
return (
<DateInput2
renderLabel={LABEL_TEXT}
screenReaderLabels={{
calendarIcon: 'Calendar',
nextMonthButton: 'Next month',
prevMonthButton: 'Previous month'
}}
value={inputValue}
onChange={(_e, inputValue, _dateString) => {
setInputValue(inputValue)
}}
/>
)
}
describe('<DateInput2 />', () => {
let consoleWarningMock: MockInstance<typeof console.error>
let consoleErrorMock: MockInstance<typeof console.error>
beforeEach(() => {
// Mocking console to prevent test output pollution
consoleWarningMock = vi.spyOn(console, 'warn').mockImplementation(() => {})
consoleErrorMock = vi.spyOn(console, 'error').mockImplementation(() => {})
})
afterEach(() => {
consoleWarningMock.mockRestore()
consoleErrorMock.mockRestore()
})
it('should render an input', async () => {
const { container } = render(<DateInputExample />)
const dateInput = container.querySelector('input')
expect(dateInput).toBeInTheDocument()
expect(dateInput).toHaveAttribute('type', 'text')
})
it('should render an input label', async () => {
const { container } = render(<DateInputExample />)
const label = container.querySelector('label')
expect(label).toBeInTheDocument()
expect(label).toHaveTextContent(LABEL_TEXT)
})
it('should render an input placeholder', async () => {
const placeholder = 'Placeholder'
render(
<DateInput2
renderLabel="Choose a date"
screenReaderLabels={{
calendarIcon: 'Calendar',
nextMonthButton: 'Next month',
prevMonthButton: 'Previous month'
}}
placeholder={placeholder}
value=""
/>
)
const dateInput = screen.getByLabelText('Choose a date')
expect(dateInput).toHaveAttribute('placeholder', placeholder)
})
it('should render a calendar icon with screen reader label', async () => {
const iconLabel = 'Calendar icon Label'
const { container } = render(
<DateInput2
renderLabel="Choose a date"
screenReaderLabels={{
calendarIcon: iconLabel,
nextMonthButton: 'Next month',
prevMonthButton: 'Previous month'
}}
value=""
/>
)
const calendarIcon = container.querySelector(
'svg[name="IconCalendarMonth"]'
)
const calendarLabel = screen.getByText(iconLabel)
expect(calendarIcon).toBeInTheDocument()
expect(calendarLabel).toBeInTheDocument()
})
it('refs should return the underlying component', async () => {
const inputRef = vi.fn()
const ref: React.Ref<TextInput> = { current: null }
const { container } = render(
<DateInput2
id="dateInput2"
inputRef={inputRef}
ref={ref}
renderLabel={LABEL_TEXT}
screenReaderLabels={{
calendarIcon: 'Calendar',
nextMonthButton: 'Next month',
prevMonthButton: 'Previous month'
}}
/>
)
const dateInput = container.querySelector('input')
expect(inputRef).toHaveBeenCalledWith(dateInput)
expect(ref.current!.props.id).toBe('dateInput2')
expect(dateInput).toBeInTheDocument()
})
it('should render a custom calendar icon with screen reader label', async () => {
const iconLabel = 'Calendar icon Label'
const { container } = render(
<DateInput2
renderLabel="Choose a date"
screenReaderLabels={{
calendarIcon: iconLabel,
nextMonthButton: 'Next month',
prevMonthButton: 'Previous month'
}}
value=""
renderCalendarIcon={<IconHeartLine />}
/>
)
const calendarIcon = container.querySelector('svg[name="IconHeart"]')
const calendarLabel = screen.getByText(iconLabel)
expect(calendarIcon).toBeInTheDocument()
expect(calendarLabel).toBeInTheDocument()
})
it('should not show calendar table by default', async () => {
render(<DateInputExample />)
const calendarTable = screen.queryByRole('table')
expect(calendarTable).not.toBeInTheDocument()
})
it('should show calendar table when calendar button is clicked', async () => {
render(<DateInputExample />)
const calendarButton = screen.getByRole('button')
expect(calendarButton).toBeInTheDocument()
await userEvent.click(calendarButton)
await waitFor(() => {
const calendarTable = screen.queryByRole('table')
expect(calendarTable).toBeInTheDocument()
})
})
it('should render navigation arrow buttons with screen reader labels', async () => {
const nextMonthLabel = 'Next month'
const prevMonthLabel = 'Previous month'
render(
<DateInput2
renderLabel="Choose a date"
screenReaderLabels={{
calendarIcon: 'Calendar',
nextMonthButton: nextMonthLabel,
prevMonthButton: prevMonthLabel
}}
value=""
/>
)
const calendarButton = screen.getByRole('button')
await userEvent.click(calendarButton)
await waitFor(() => {
const prevMonthButton = screen.getByRole('button', {
name: prevMonthLabel
})
const nextMonthButton = screen.getByRole('button', {
name: nextMonthLabel
})
expect(prevMonthButton).toBeInTheDocument()
expect(nextMonthButton).toBeInTheDocument()
const prevButtonLabel = screen.getByText(prevMonthLabel)
const nextButtonLabel = screen.getByText(nextMonthLabel)
expect(prevButtonLabel).toBeInTheDocument()
expect(nextButtonLabel).toBeInTheDocument()
const prevMonthIcon = prevMonthButton.querySelector(
'svg[name="IconArrowOpenStart"]'
)
const nextMonthIcon = nextMonthButton.querySelector(
'svg[name="IconArrowOpenEnd"]'
)
expect(prevMonthIcon).toBeInTheDocument()
expect(nextMonthIcon).toBeInTheDocument()
})
})
it('should programmatically set and render the initial value', async () => {
const value = '26/03/2024'
render(
<DateInput2
renderLabel="Choose a date"
screenReaderLabels={{
calendarIcon: 'Calendar',
nextMonthButton: 'Next month',
prevMonthButton: 'Previous month'
}}
locale="en-GB"
timezone="UTC"
value={value}
/>
)
const dateInput = screen.getByLabelText('Choose a date')
expect(dateInput).toHaveValue(value)
expect(dateInput).toBeInTheDocument()
})
it('should set interaction type to disabled', async () => {
const interactionDisabled = 'disabled'
const { container } = render(
<DateInput2
renderLabel="Choose a date"
screenReaderLabels={{
calendarIcon: 'Calendar',
nextMonthButton: 'Next month',
prevMonthButton: 'Previous month'
}}
value=""
interaction={interactionDisabled}
/>
)
const dateInput = container.querySelector('input')
expect(dateInput).toHaveAttribute(interactionDisabled)
})
it('should set interaction type to readonly', async () => {
const interactionReadOnly = 'readonly'
const { container } = render(
<DateInput2
renderLabel="Choose a date"
screenReaderLabels={{
calendarIcon: 'Calendar',
nextMonthButton: 'Next month',
prevMonthButton: 'Previous month'
}}
value=""
interaction={interactionReadOnly}
/>
)
const dateInput = container.querySelector('input')
const calendarButton = screen.getByRole('button')
expect(dateInput).toHaveAttribute(interactionReadOnly)
expect(calendarButton).toBeInTheDocument()
await userEvent.click(calendarButton)
await waitFor(() => {
const calendarTable = screen.queryByRole('table')
expect(calendarTable).not.toBeInTheDocument()
})
})
it('should set required', async () => {
const { container } = render(
<DateInput2
renderLabel="Choose a date"
screenReaderLabels={{
calendarIcon: 'Calendar',
nextMonthButton: 'Next month',
prevMonthButton: 'Previous month'
}}
value=""
isRequired
/>
)
const dateInput = container.querySelector('input')
expect(dateInput).toHaveAttribute('required')
})
it('should call onBlur', async () => {
const onBlur = vi.fn()
render(
<DateInput2
renderLabel="Choose a date"
screenReaderLabels={{
calendarIcon: 'Calendar',
nextMonthButton: 'Next month',
prevMonthButton: 'Previous month'
}}
value=""
onBlur={onBlur}
/>
)
const dateInput = screen.getByLabelText('Choose a date')
fireEvent.blur(dateInput)
await waitFor(() => {
expect(onBlur).toHaveBeenCalled()
})
})
it('should validate if the invalidDateErrorMessage prop is provided', async () => {
const errorMsg = 'errorMsg'
const Example = () => {
const [inputValue, setInputValue] = useState('')
return (
<DateInput2
renderLabel={LABEL_TEXT}
screenReaderLabels={{
calendarIcon: 'Calendar',
nextMonthButton: 'Next month',
prevMonthButton: 'Previous month'
}}
value={inputValue}
onChange={(_e, inputValue, _dateString) => {
setInputValue(inputValue)
}}
invalidDateErrorMessage={errorMsg}
/>
)
}
render(<Example />)
expect(screen.queryByText(errorMsg)).not.toBeInTheDocument()
const dateInput = screen.getByLabelText(LABEL_TEXT)
await userEvent.click(dateInput)
await userEvent.type(dateInput, 'Not a date')
dateInput.blur()
await waitFor(() => {
expect(screen.getByText(errorMsg)).toBeInTheDocument()
})
})
it('should show form field messages', async () => {
const messages: any = [
{ text: 'TypeLess' },
{ type: 'error', text: 'Error' },
{ type: 'success', text: 'Success' },
{ type: 'hint', text: 'Hint' },
{ type: 'screenreader-only', text: 'Screenreader' }
]
render(
<DateInput2
renderLabel="Choose a date"
screenReaderLabels={{
calendarIcon: 'Calendar',
nextMonthButton: 'Next month',
prevMonthButton: 'Previous month'
}}
value=""
messages={messages}
/>
)
expect(screen.getByText('TypeLess')).toBeVisible()
expect(screen.getByText('Error')).toBeVisible()
expect(screen.getByText('Success')).toBeVisible()
expect(screen.getByText('Hint')).toBeVisible()
const screenreaderMessage = screen.getByText('Screenreader')
expect(screenreaderMessage).toBeInTheDocument()
expect(screenreaderMessage).toHaveClass(/screenReaderContent/)
})
})