@instructure/ui-date-input 10.19.2-snapshot-2 → 10.19.2-snapshot-4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,419 +0,0 @@
1
- /*
2
- * The MIT License (MIT)
3
- *
4
- * Copyright (c) 2015 - present Instructure, Inc.
5
- *
6
- * Permission is hereby granted, free of charge, to any person obtaining a copy
7
- * of this software and associated documentation files (the "Software"), to deal
8
- * in the Software without restriction, including without limitation the rights
9
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
- * copies of the Software, and to permit persons to whom the Software is
11
- * furnished to do so, subject to the following conditions:
12
- *
13
- * The above copyright notice and this permission notice shall be included in all
14
- * copies or substantial portions of the Software.
15
- *
16
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
- * SOFTWARE.
23
- */
24
- import { useState } from 'react'
25
- import { fireEvent, render, screen, waitFor } from '@testing-library/react'
26
- import { vi, MockInstance } from 'vitest'
27
- import userEvent from '@testing-library/user-event'
28
- import '@testing-library/jest-dom'
29
- import { IconHeartLine } from '@instructure/ui-icons'
30
-
31
- import { DateInput2 } from '../index'
32
- import { TextInput } from '@instructure/ui-text-input'
33
-
34
- const LABEL_TEXT = 'Choose a date'
35
-
36
- const DateInputExample = () => {
37
- const [inputValue, setInputValue] = useState('')
38
-
39
- return (
40
- <DateInput2
41
- renderLabel={LABEL_TEXT}
42
- screenReaderLabels={{
43
- calendarIcon: 'Calendar',
44
- nextMonthButton: 'Next month',
45
- prevMonthButton: 'Previous month'
46
- }}
47
- value={inputValue}
48
- onChange={(_e, inputValue, _dateString) => {
49
- setInputValue(inputValue)
50
- }}
51
- />
52
- )
53
- }
54
-
55
- describe('<DateInput2 />', () => {
56
- let consoleWarningMock: MockInstance<typeof console.error>
57
- let consoleErrorMock: MockInstance<typeof console.error>
58
-
59
- beforeEach(() => {
60
- // Mocking console to prevent test output pollution
61
- consoleWarningMock = vi.spyOn(console, 'warn').mockImplementation(() => {})
62
- consoleErrorMock = vi.spyOn(console, 'error').mockImplementation(() => {})
63
- })
64
-
65
- afterEach(() => {
66
- consoleWarningMock.mockRestore()
67
- consoleErrorMock.mockRestore()
68
- })
69
-
70
- it('should render an input', async () => {
71
- const { container } = render(<DateInputExample />)
72
- const dateInput = container.querySelector('input')
73
-
74
- expect(dateInput).toBeInTheDocument()
75
- expect(dateInput).toHaveAttribute('type', 'text')
76
- })
77
-
78
- it('should render an input label', async () => {
79
- const { container } = render(<DateInputExample />)
80
-
81
- const label = container.querySelector('label')
82
-
83
- expect(label).toBeInTheDocument()
84
- expect(label).toHaveTextContent(LABEL_TEXT)
85
- })
86
-
87
- it('should render an input placeholder', async () => {
88
- const placeholder = 'Placeholder'
89
- render(
90
- <DateInput2
91
- renderLabel="Choose a date"
92
- screenReaderLabels={{
93
- calendarIcon: 'Calendar',
94
- nextMonthButton: 'Next month',
95
- prevMonthButton: 'Previous month'
96
- }}
97
- placeholder={placeholder}
98
- value=""
99
- />
100
- )
101
- const dateInput = screen.getByLabelText('Choose a date')
102
-
103
- expect(dateInput).toHaveAttribute('placeholder', placeholder)
104
- })
105
-
106
- it('should render a calendar icon with screen reader label', async () => {
107
- const iconLabel = 'Calendar icon Label'
108
- const { container } = render(
109
- <DateInput2
110
- renderLabel="Choose a date"
111
- screenReaderLabels={{
112
- calendarIcon: iconLabel,
113
- nextMonthButton: 'Next month',
114
- prevMonthButton: 'Previous month'
115
- }}
116
- value=""
117
- />
118
- )
119
- const calendarIcon = container.querySelector(
120
- 'svg[name="IconCalendarMonth"]'
121
- )
122
- const calendarLabel = screen.getByText(iconLabel)
123
-
124
- expect(calendarIcon).toBeInTheDocument()
125
- expect(calendarLabel).toBeInTheDocument()
126
- })
127
-
128
- it('refs should return the underlying component', async () => {
129
- const inputRef = vi.fn()
130
- const ref: React.Ref<TextInput> = { current: null }
131
- const { container } = render(
132
- <DateInput2
133
- id="dateInput2"
134
- inputRef={inputRef}
135
- ref={ref}
136
- renderLabel={LABEL_TEXT}
137
- screenReaderLabels={{
138
- calendarIcon: 'Calendar',
139
- nextMonthButton: 'Next month',
140
- prevMonthButton: 'Previous month'
141
- }}
142
- />
143
- )
144
- const dateInput = container.querySelector('input')
145
- expect(inputRef).toHaveBeenCalledWith(dateInput)
146
- expect(ref.current!.props.id).toBe('dateInput2')
147
- expect(dateInput).toBeInTheDocument()
148
- })
149
-
150
- it('should render a custom calendar icon with screen reader label', async () => {
151
- const iconLabel = 'Calendar icon Label'
152
- const { container } = render(
153
- <DateInput2
154
- renderLabel="Choose a date"
155
- screenReaderLabels={{
156
- calendarIcon: iconLabel,
157
- nextMonthButton: 'Next month',
158
- prevMonthButton: 'Previous month'
159
- }}
160
- value=""
161
- renderCalendarIcon={<IconHeartLine />}
162
- />
163
- )
164
- const calendarIcon = container.querySelector('svg[name="IconHeart"]')
165
- const calendarLabel = screen.getByText(iconLabel)
166
-
167
- expect(calendarIcon).toBeInTheDocument()
168
- expect(calendarLabel).toBeInTheDocument()
169
- })
170
-
171
- it('should not show calendar table by default', async () => {
172
- render(<DateInputExample />)
173
- const calendarTable = screen.queryByRole('table')
174
-
175
- expect(calendarTable).not.toBeInTheDocument()
176
- })
177
-
178
- it('should show calendar table when calendar button is clicked', async () => {
179
- render(<DateInputExample />)
180
- const calendarButton = screen.getByRole('button')
181
-
182
- expect(calendarButton).toBeInTheDocument()
183
-
184
- await userEvent.click(calendarButton)
185
-
186
- await waitFor(() => {
187
- const calendarTable = screen.queryByRole('table')
188
- expect(calendarTable).toBeInTheDocument()
189
- })
190
- })
191
-
192
- it('should render navigation arrow buttons with screen reader labels', async () => {
193
- const nextMonthLabel = 'Next month'
194
- const prevMonthLabel = 'Previous month'
195
-
196
- render(
197
- <DateInput2
198
- renderLabel="Choose a date"
199
- screenReaderLabels={{
200
- calendarIcon: 'Calendar',
201
- nextMonthButton: nextMonthLabel,
202
- prevMonthButton: prevMonthLabel
203
- }}
204
- value=""
205
- />
206
- )
207
- const calendarButton = screen.getByRole('button')
208
-
209
- await userEvent.click(calendarButton)
210
-
211
- await waitFor(() => {
212
- const prevMonthButton = screen.getByRole('button', {
213
- name: prevMonthLabel
214
- })
215
- const nextMonthButton = screen.getByRole('button', {
216
- name: nextMonthLabel
217
- })
218
-
219
- expect(prevMonthButton).toBeInTheDocument()
220
- expect(nextMonthButton).toBeInTheDocument()
221
-
222
- const prevButtonLabel = screen.getByText(prevMonthLabel)
223
- const nextButtonLabel = screen.getByText(nextMonthLabel)
224
-
225
- expect(prevButtonLabel).toBeInTheDocument()
226
- expect(nextButtonLabel).toBeInTheDocument()
227
-
228
- const prevMonthIcon = prevMonthButton.querySelector(
229
- 'svg[name="IconArrowOpenStart"]'
230
- )
231
- const nextMonthIcon = nextMonthButton.querySelector(
232
- 'svg[name="IconArrowOpenEnd"]'
233
- )
234
-
235
- expect(prevMonthIcon).toBeInTheDocument()
236
- expect(nextMonthIcon).toBeInTheDocument()
237
- })
238
- })
239
-
240
- it('should programmatically set and render the initial value', async () => {
241
- const value = '26/03/2024'
242
- render(
243
- <DateInput2
244
- renderLabel="Choose a date"
245
- screenReaderLabels={{
246
- calendarIcon: 'Calendar',
247
- nextMonthButton: 'Next month',
248
- prevMonthButton: 'Previous month'
249
- }}
250
- locale="en-GB"
251
- timezone="UTC"
252
- value={value}
253
- />
254
- )
255
- const dateInput = screen.getByLabelText('Choose a date')
256
-
257
- expect(dateInput).toHaveValue(value)
258
- expect(dateInput).toBeInTheDocument()
259
- })
260
-
261
- it('should set interaction type to disabled', async () => {
262
- const interactionDisabled = 'disabled'
263
- const { container } = render(
264
- <DateInput2
265
- renderLabel="Choose a date"
266
- screenReaderLabels={{
267
- calendarIcon: 'Calendar',
268
- nextMonthButton: 'Next month',
269
- prevMonthButton: 'Previous month'
270
- }}
271
- value=""
272
- interaction={interactionDisabled}
273
- />
274
- )
275
- const dateInput = container.querySelector('input')
276
-
277
- expect(dateInput).toHaveAttribute(interactionDisabled)
278
- })
279
-
280
- it('should set interaction type to readonly', async () => {
281
- const interactionReadOnly = 'readonly'
282
- const { container } = render(
283
- <DateInput2
284
- renderLabel="Choose a date"
285
- screenReaderLabels={{
286
- calendarIcon: 'Calendar',
287
- nextMonthButton: 'Next month',
288
- prevMonthButton: 'Previous month'
289
- }}
290
- value=""
291
- interaction={interactionReadOnly}
292
- />
293
- )
294
- const dateInput = container.querySelector('input')
295
- const calendarButton = screen.getByRole('button')
296
-
297
- expect(dateInput).toHaveAttribute(interactionReadOnly)
298
- expect(calendarButton).toBeInTheDocument()
299
-
300
- await userEvent.click(calendarButton)
301
-
302
- await waitFor(() => {
303
- const calendarTable = screen.queryByRole('table')
304
-
305
- expect(calendarTable).not.toBeInTheDocument()
306
- })
307
- })
308
-
309
- it('should set required', async () => {
310
- const { container } = render(
311
- <DateInput2
312
- renderLabel="Choose a date"
313
- screenReaderLabels={{
314
- calendarIcon: 'Calendar',
315
- nextMonthButton: 'Next month',
316
- prevMonthButton: 'Previous month'
317
- }}
318
- value=""
319
- isRequired
320
- />
321
- )
322
- const dateInput = container.querySelector('input')
323
-
324
- expect(dateInput).toHaveAttribute('required')
325
- })
326
-
327
- it('should call onBlur', async () => {
328
- const onBlur = vi.fn()
329
- render(
330
- <DateInput2
331
- renderLabel="Choose a date"
332
- screenReaderLabels={{
333
- calendarIcon: 'Calendar',
334
- nextMonthButton: 'Next month',
335
- prevMonthButton: 'Previous month'
336
- }}
337
- value=""
338
- onBlur={onBlur}
339
- />
340
- )
341
- const dateInput = screen.getByLabelText('Choose a date')
342
-
343
- fireEvent.blur(dateInput)
344
-
345
- await waitFor(() => {
346
- expect(onBlur).toHaveBeenCalled()
347
- })
348
- })
349
-
350
- it('should validate if the invalidDateErrorMessage prop is provided', async () => {
351
- const errorMsg = 'errorMsg'
352
- const Example = () => {
353
- const [inputValue, setInputValue] = useState('')
354
-
355
- return (
356
- <DateInput2
357
- renderLabel={LABEL_TEXT}
358
- screenReaderLabels={{
359
- calendarIcon: 'Calendar',
360
- nextMonthButton: 'Next month',
361
- prevMonthButton: 'Previous month'
362
- }}
363
- value={inputValue}
364
- onChange={(_e, inputValue, _dateString) => {
365
- setInputValue(inputValue)
366
- }}
367
- invalidDateErrorMessage={errorMsg}
368
- />
369
- )
370
- }
371
-
372
- render(<Example />)
373
-
374
- expect(screen.queryByText(errorMsg)).not.toBeInTheDocument()
375
-
376
- const dateInput = screen.getByLabelText(LABEL_TEXT)
377
-
378
- await userEvent.click(dateInput)
379
- await userEvent.type(dateInput, 'Not a date')
380
-
381
- dateInput.blur()
382
-
383
- await waitFor(() => {
384
- expect(screen.getByText(errorMsg)).toBeInTheDocument()
385
- })
386
- })
387
-
388
- it('should show form field messages', async () => {
389
- const messages: any = [
390
- { text: 'TypeLess' },
391
- { type: 'error', text: 'Error' },
392
- { type: 'success', text: 'Success' },
393
- { type: 'hint', text: 'Hint' },
394
- { type: 'screenreader-only', text: 'Screenreader' }
395
- ]
396
-
397
- render(
398
- <DateInput2
399
- renderLabel="Choose a date"
400
- screenReaderLabels={{
401
- calendarIcon: 'Calendar',
402
- nextMonthButton: 'Next month',
403
- prevMonthButton: 'Previous month'
404
- }}
405
- value=""
406
- messages={messages}
407
- />
408
- )
409
-
410
- expect(screen.getByText('TypeLess')).toBeVisible()
411
- expect(screen.getByText('Error')).toBeVisible()
412
- expect(screen.getByText('Success')).toBeVisible()
413
- expect(screen.getByText('Hint')).toBeVisible()
414
-
415
- const screenreaderMessage = screen.getByText('Screenreader')
416
- expect(screenreaderMessage).toBeInTheDocument()
417
- expect(screenreaderMessage).toHaveClass(/screenReaderContent/)
418
- })
419
- })
@@ -1,2 +0,0 @@
1
- import '@testing-library/jest-dom';
2
- //# sourceMappingURL=DateInput.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"DateInput.test.d.ts","sourceRoot":"","sources":["../../../src/DateInput/__new-tests__/DateInput.test.tsx"],"names":[],"mappings":"AA2BA,OAAO,2BAA2B,CAAA"}
@@ -1,2 +0,0 @@
1
- import '@testing-library/jest-dom';
2
- //# sourceMappingURL=DateInput2.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"DateInput2.test.d.ts","sourceRoot":"","sources":["../../../src/DateInput2/__new-tests__/DateInput2.test.tsx"],"names":[],"mappings":"AA2BA,OAAO,2BAA2B,CAAA"}