@instructure/ui-date-time-input 10.19.2-snapshot-3 → 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.
- package/CHANGELOG.md +1 -1
- package/package.json +17 -17
- package/tsconfig.build.tsbuildinfo +1 -1
- package/es/DateTimeInput/__new-tests__/DateTimeInput.test.js +0 -739
- package/lib/DateTimeInput/__new-tests__/DateTimeInput.test.js +0 -742
- package/src/DateTimeInput/__new-tests__/DateTimeInput.test.tsx +0 -795
- package/types/DateTimeInput/__new-tests__/DateTimeInput.test.d.ts +0 -2
- package/types/DateTimeInput/__new-tests__/DateTimeInput.test.d.ts.map +0 -1
|
@@ -1,795 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* The MIT License (MIT)
|
|
3
|
-
*
|
|
4
|
-
* Copyright (c) 2018 - 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
|
-
|
|
25
|
-
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
|
|
26
|
-
import { vi } from 'vitest'
|
|
27
|
-
import userEvent from '@testing-library/user-event'
|
|
28
|
-
import '@testing-library/jest-dom'
|
|
29
|
-
|
|
30
|
-
import { DateTimeInput } from '../index'
|
|
31
|
-
import { ApplyLocale, DateTime } from '@instructure/ui-i18n'
|
|
32
|
-
|
|
33
|
-
describe('<DateTimeInput />', () => {
|
|
34
|
-
let consoleWarningMock: ReturnType<typeof vi.spyOn>
|
|
35
|
-
let consoleErrorMock: ReturnType<typeof vi.spyOn>
|
|
36
|
-
|
|
37
|
-
beforeEach(() => {
|
|
38
|
-
// Mocking console to prevent test output pollution and expect for messages
|
|
39
|
-
consoleWarningMock = vi
|
|
40
|
-
.spyOn(console, 'warn')
|
|
41
|
-
.mockImplementation(() => {}) as any
|
|
42
|
-
consoleErrorMock = vi
|
|
43
|
-
.spyOn(console, 'error')
|
|
44
|
-
.mockImplementation(() => {}) as any
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
afterEach(() => {
|
|
48
|
-
consoleWarningMock.mockRestore()
|
|
49
|
-
consoleErrorMock.mockRestore()
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
it('should use the default value', () => {
|
|
53
|
-
const locale = 'en-US'
|
|
54
|
-
const timezone = 'US/Eastern'
|
|
55
|
-
const dateTime = DateTime.parse('2017-05-01T17:30Z', locale, timezone)
|
|
56
|
-
|
|
57
|
-
render(
|
|
58
|
-
<DateTimeInput
|
|
59
|
-
description="date_time"
|
|
60
|
-
prevMonthLabel="Previous month"
|
|
61
|
-
nextMonthLabel="Next month"
|
|
62
|
-
dateRenderLabel="date-input"
|
|
63
|
-
timeRenderLabel="time-input"
|
|
64
|
-
invalidDateTimeMessage="whoops"
|
|
65
|
-
locale={locale}
|
|
66
|
-
timezone={timezone}
|
|
67
|
-
defaultValue={dateTime.toISOString()}
|
|
68
|
-
/>
|
|
69
|
-
)
|
|
70
|
-
const dateInput = screen.getByLabelText('date-input')
|
|
71
|
-
const timeInput = screen.getByLabelText('time-input')
|
|
72
|
-
|
|
73
|
-
expect(dateInput).toHaveValue(dateTime.format('LL'))
|
|
74
|
-
expect(timeInput).toHaveValue(dateTime.format('LT'))
|
|
75
|
-
})
|
|
76
|
-
|
|
77
|
-
it('should use the value', () => {
|
|
78
|
-
const onChange = vi.fn()
|
|
79
|
-
const locale = 'en-US'
|
|
80
|
-
const timezone = 'US/Eastern'
|
|
81
|
-
const dateTime = DateTime.parse('2017-05-01T23:30Z', locale, timezone)
|
|
82
|
-
|
|
83
|
-
render(
|
|
84
|
-
<DateTimeInput
|
|
85
|
-
description="date_time"
|
|
86
|
-
prevMonthLabel="Previous month"
|
|
87
|
-
nextMonthLabel="Next month"
|
|
88
|
-
dateRenderLabel="date-input"
|
|
89
|
-
timeRenderLabel="time-input"
|
|
90
|
-
invalidDateTimeMessage="whoops"
|
|
91
|
-
locale={locale}
|
|
92
|
-
timezone={timezone}
|
|
93
|
-
value={dateTime.toISOString()}
|
|
94
|
-
onChange={onChange}
|
|
95
|
-
/>
|
|
96
|
-
)
|
|
97
|
-
const dateInput = screen.getByLabelText('date-input')
|
|
98
|
-
const timeInput = screen.getByLabelText('time-input')
|
|
99
|
-
|
|
100
|
-
expect(dateInput).toHaveValue(dateTime.format('LL'))
|
|
101
|
-
expect(timeInput).toHaveValue(dateTime.format('LT'))
|
|
102
|
-
})
|
|
103
|
-
|
|
104
|
-
it('should prefer value to defaultValue', () => {
|
|
105
|
-
const locale = 'en-US'
|
|
106
|
-
const timezone = 'US/Eastern'
|
|
107
|
-
const value = DateTime.parse('2017-05-01T17:30Z', locale, timezone)
|
|
108
|
-
const defaultValue = DateTime.parse('2016-04-01T17:00Z', locale, timezone)
|
|
109
|
-
const onChange = vi.fn()
|
|
110
|
-
|
|
111
|
-
render(
|
|
112
|
-
<DateTimeInput
|
|
113
|
-
description="date_time"
|
|
114
|
-
prevMonthLabel="Previous month"
|
|
115
|
-
nextMonthLabel="Next month"
|
|
116
|
-
dateRenderLabel="date-input"
|
|
117
|
-
timeRenderLabel="time-input"
|
|
118
|
-
invalidDateTimeMessage="whoops"
|
|
119
|
-
locale={locale}
|
|
120
|
-
timezone={timezone}
|
|
121
|
-
value={value.toISOString()}
|
|
122
|
-
defaultValue={defaultValue.toISOString()}
|
|
123
|
-
onChange={onChange}
|
|
124
|
-
/>
|
|
125
|
-
)
|
|
126
|
-
const dateInput = screen.getByLabelText('date-input')
|
|
127
|
-
const timeInput = screen.getByLabelText('time-input')
|
|
128
|
-
|
|
129
|
-
expect(dateInput).toHaveValue(value.format('LL'))
|
|
130
|
-
expect(timeInput).toHaveValue(value.format('LT'))
|
|
131
|
-
})
|
|
132
|
-
|
|
133
|
-
it('should set time to local midnight when only date is set', () => {
|
|
134
|
-
const locale = 'en-US'
|
|
135
|
-
const timezone = 'US/Eastern'
|
|
136
|
-
const dateObj = DateTime.parse('2017-04-01T18:30Z', locale, timezone)
|
|
137
|
-
const date = dateObj.toISOString().split('T')[0]
|
|
138
|
-
|
|
139
|
-
render(
|
|
140
|
-
<DateTimeInput
|
|
141
|
-
description="date_time"
|
|
142
|
-
dateRenderLabel="date-input"
|
|
143
|
-
prevMonthLabel="Previous month"
|
|
144
|
-
nextMonthLabel="Next month"
|
|
145
|
-
timeRenderLabel="time-input"
|
|
146
|
-
invalidDateTimeMessage="whoops"
|
|
147
|
-
locale={locale}
|
|
148
|
-
timezone={timezone}
|
|
149
|
-
defaultValue={date}
|
|
150
|
-
/>
|
|
151
|
-
)
|
|
152
|
-
const dateInput = screen.getByLabelText('date-input')
|
|
153
|
-
const timeInput = screen.getByLabelText('time-input')
|
|
154
|
-
|
|
155
|
-
expect(dateInput).toHaveValue(dateObj.format('LL'))
|
|
156
|
-
expect(timeInput).toHaveValue('12:00 AM')
|
|
157
|
-
})
|
|
158
|
-
|
|
159
|
-
it('should call invalidDateTimeMessage if time is set w/o a date and is required', async () => {
|
|
160
|
-
const invalidDateTimeMessage = vi.fn((_rawd) => 'whoops')
|
|
161
|
-
|
|
162
|
-
const { container } = render(
|
|
163
|
-
<DateTimeInput
|
|
164
|
-
description="date_time"
|
|
165
|
-
dateRenderLabel="date-input"
|
|
166
|
-
prevMonthLabel="Previous month"
|
|
167
|
-
nextMonthLabel="Next month"
|
|
168
|
-
timeRenderLabel="time-input"
|
|
169
|
-
locale="en-US"
|
|
170
|
-
timezone="US/Eastern"
|
|
171
|
-
isRequired
|
|
172
|
-
invalidDateTimeMessage={invalidDateTimeMessage}
|
|
173
|
-
/>
|
|
174
|
-
)
|
|
175
|
-
const timeInput = screen.getByLabelText('time-input *')
|
|
176
|
-
|
|
177
|
-
await userEvent.type(timeInput, '1:00 PM')
|
|
178
|
-
fireEvent.blur(timeInput)
|
|
179
|
-
|
|
180
|
-
await waitFor(() => {
|
|
181
|
-
expect(container).toHaveTextContent('whoops')
|
|
182
|
-
expect(invalidDateTimeMessage).toHaveBeenCalled()
|
|
183
|
-
})
|
|
184
|
-
})
|
|
185
|
-
|
|
186
|
-
it('should not call invalidDateTimeMessage if time is set w/o a date', async () => {
|
|
187
|
-
const invalidDateTimeMessage = vi.fn((_rawd) => 'whoops')
|
|
188
|
-
|
|
189
|
-
render(
|
|
190
|
-
<DateTimeInput
|
|
191
|
-
description="date_time"
|
|
192
|
-
dateRenderLabel="date-input"
|
|
193
|
-
prevMonthLabel="Previous month"
|
|
194
|
-
nextMonthLabel="Next month"
|
|
195
|
-
timeRenderLabel="time-input"
|
|
196
|
-
locale="en-US"
|
|
197
|
-
timezone="US/Eastern"
|
|
198
|
-
invalidDateTimeMessage={invalidDateTimeMessage}
|
|
199
|
-
/>
|
|
200
|
-
)
|
|
201
|
-
const timeInput = screen.getByLabelText('time-input')
|
|
202
|
-
|
|
203
|
-
await userEvent.type(timeInput, '1:00 PM{enter}')
|
|
204
|
-
|
|
205
|
-
await waitFor(() => {
|
|
206
|
-
const errorMessages = screen.queryByText('whoops')
|
|
207
|
-
|
|
208
|
-
expect(errorMessages).not.toBeInTheDocument()
|
|
209
|
-
expect(invalidDateTimeMessage).not.toHaveBeenCalled()
|
|
210
|
-
})
|
|
211
|
-
})
|
|
212
|
-
|
|
213
|
-
it('should fire the onChange event when TimeInput value changes', async () => {
|
|
214
|
-
const onChange = vi.fn()
|
|
215
|
-
|
|
216
|
-
const locale = 'en-US'
|
|
217
|
-
const timezone = 'US/Eastern'
|
|
218
|
-
const defaultValue = DateTime.parse('2017-05-01T17:00', locale, timezone)
|
|
219
|
-
|
|
220
|
-
render(
|
|
221
|
-
<DateTimeInput
|
|
222
|
-
description="date_time"
|
|
223
|
-
dateRenderLabel="date-input"
|
|
224
|
-
prevMonthLabel="Previous month"
|
|
225
|
-
nextMonthLabel="Next month"
|
|
226
|
-
timeRenderLabel="time-input"
|
|
227
|
-
invalidDateTimeMessage="whoops"
|
|
228
|
-
locale={locale}
|
|
229
|
-
timezone={timezone}
|
|
230
|
-
onChange={onChange}
|
|
231
|
-
defaultValue={defaultValue.toISOString()}
|
|
232
|
-
/>
|
|
233
|
-
)
|
|
234
|
-
const timeInput = screen.getByLabelText('time-input')
|
|
235
|
-
|
|
236
|
-
fireEvent.change(timeInput, { target: { value: '3:00 AM' } })
|
|
237
|
-
fireEvent.keyDown(timeInput, { key: 'Enter', code: 'Enter' })
|
|
238
|
-
fireEvent.blur(timeInput)
|
|
239
|
-
|
|
240
|
-
await waitFor(() => {
|
|
241
|
-
expect(onChange).toHaveBeenCalled()
|
|
242
|
-
expect(onChange.mock.calls[0][0].target.value).toMatch('3:00 AM')
|
|
243
|
-
})
|
|
244
|
-
})
|
|
245
|
-
|
|
246
|
-
it('should show correct message when TimeInput value changes', () => {
|
|
247
|
-
const { container } = render(
|
|
248
|
-
<DateTimeInput
|
|
249
|
-
description="date_time"
|
|
250
|
-
dateRenderLabel="date-input"
|
|
251
|
-
prevMonthLabel="Previous month"
|
|
252
|
-
nextMonthLabel="Next month"
|
|
253
|
-
timeRenderLabel="time-input"
|
|
254
|
-
invalidDateTimeMessage="whoops"
|
|
255
|
-
locale="en-US"
|
|
256
|
-
timezone="US/Eastern"
|
|
257
|
-
defaultValue="2018-01-18T13:00"
|
|
258
|
-
/>
|
|
259
|
-
)
|
|
260
|
-
const timeInput = screen.getByLabelText('time-input')
|
|
261
|
-
|
|
262
|
-
fireEvent.change(timeInput, { target: { value: '5:00 PM' } })
|
|
263
|
-
fireEvent.keyDown(timeInput, { key: 'Enter', code: 'Enter' })
|
|
264
|
-
fireEvent.blur(timeInput)
|
|
265
|
-
|
|
266
|
-
expect(container).toHaveTextContent('Thursday, January 18, 2018 5:00 PM')
|
|
267
|
-
})
|
|
268
|
-
|
|
269
|
-
it('should show the formatted date-time message', () => {
|
|
270
|
-
const locale = 'en-US'
|
|
271
|
-
const timezone = 'US/Eastern'
|
|
272
|
-
const defaultValue = DateTime.parse('2017-05-01T17:30Z', locale, timezone)
|
|
273
|
-
|
|
274
|
-
const { container } = render(
|
|
275
|
-
<DateTimeInput
|
|
276
|
-
description="date_time"
|
|
277
|
-
dateRenderLabel="date-input"
|
|
278
|
-
prevMonthLabel="Previous month"
|
|
279
|
-
nextMonthLabel="Next month"
|
|
280
|
-
timeRenderLabel="time-input"
|
|
281
|
-
invalidDateTimeMessage="whoops"
|
|
282
|
-
locale={locale}
|
|
283
|
-
timezone={timezone}
|
|
284
|
-
defaultValue={defaultValue.toISOString()}
|
|
285
|
-
/>
|
|
286
|
-
)
|
|
287
|
-
|
|
288
|
-
expect(container).toHaveTextContent('Monday, May 1, 2017 1:30 PM')
|
|
289
|
-
})
|
|
290
|
-
|
|
291
|
-
it('should show the formatted date-time message in the proper locale', () => {
|
|
292
|
-
const timezone = 'US/Eastern'
|
|
293
|
-
const locale = 'fr'
|
|
294
|
-
const defaultValue = DateTime.parse('2017-05-01T17:30Z', locale, timezone)
|
|
295
|
-
|
|
296
|
-
const { container } = render(
|
|
297
|
-
<DateTimeInput
|
|
298
|
-
description="date_time"
|
|
299
|
-
dateRenderLabel="date-input"
|
|
300
|
-
prevMonthLabel="Previous month"
|
|
301
|
-
nextMonthLabel="Next month"
|
|
302
|
-
timeRenderLabel="time-input"
|
|
303
|
-
invalidDateTimeMessage="whoops"
|
|
304
|
-
locale={locale}
|
|
305
|
-
timezone={timezone}
|
|
306
|
-
defaultValue={defaultValue.toISOString()}
|
|
307
|
-
/>
|
|
308
|
-
)
|
|
309
|
-
expect(container).toHaveTextContent('1 mai 2017 13:30')
|
|
310
|
-
})
|
|
311
|
-
|
|
312
|
-
it('should provide the html elements for date and time input', () => {
|
|
313
|
-
let dref
|
|
314
|
-
let tref
|
|
315
|
-
|
|
316
|
-
render(
|
|
317
|
-
<DateTimeInput
|
|
318
|
-
description="date_time"
|
|
319
|
-
dateRenderLabel="date-input"
|
|
320
|
-
prevMonthLabel="Previous month"
|
|
321
|
-
nextMonthLabel="Next month"
|
|
322
|
-
timeRenderLabel="time-input"
|
|
323
|
-
invalidDateTimeMessage="whoops"
|
|
324
|
-
locale="en-US"
|
|
325
|
-
timezone="US/Eastern"
|
|
326
|
-
dateInputRef={(el) => {
|
|
327
|
-
dref = el
|
|
328
|
-
}}
|
|
329
|
-
timeInputRef={(el) => {
|
|
330
|
-
tref = el
|
|
331
|
-
}}
|
|
332
|
-
/>
|
|
333
|
-
)
|
|
334
|
-
const dateInput = screen.getByLabelText('date-input')
|
|
335
|
-
const timeInput = screen.getByLabelText('time-input')
|
|
336
|
-
|
|
337
|
-
expect(dateInput).toEqual(dref)
|
|
338
|
-
expect(timeInput).toEqual(tref)
|
|
339
|
-
})
|
|
340
|
-
|
|
341
|
-
it('should update message when value prop changes', () => {
|
|
342
|
-
const onChange = vi.fn()
|
|
343
|
-
const locale = 'en-US'
|
|
344
|
-
const timezone = 'US/Eastern'
|
|
345
|
-
const dateTime = DateTime.parse('2017-05-01T17:30Z', locale, timezone)
|
|
346
|
-
|
|
347
|
-
const props = {
|
|
348
|
-
description: 'date_time',
|
|
349
|
-
prevMonthLabel: 'Previous month',
|
|
350
|
-
nextMonthLabel: 'Next month',
|
|
351
|
-
dateRenderLabel: 'date',
|
|
352
|
-
timeRenderLabel: 'time',
|
|
353
|
-
invalidDateTimeMessage: 'whoops',
|
|
354
|
-
locale,
|
|
355
|
-
timezone,
|
|
356
|
-
value: dateTime.toISOString(),
|
|
357
|
-
onChange
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
const { container, rerender } = render(<DateTimeInput {...props} />)
|
|
361
|
-
expect(container).toHaveTextContent('May 1, 2017 1:30 PM')
|
|
362
|
-
|
|
363
|
-
rerender(<DateTimeInput {...props} value="2018-03-29T16:30Z" />)
|
|
364
|
-
expect(container).toHaveTextContent('March 29, 2018 12:30 PM')
|
|
365
|
-
})
|
|
366
|
-
|
|
367
|
-
it('should update message when locale changed', () => {
|
|
368
|
-
const onChange = vi.fn()
|
|
369
|
-
const locale = 'en-US'
|
|
370
|
-
const timezone = 'US/Eastern'
|
|
371
|
-
const dateTime = DateTime.parse('2017-05-01T17:30Z', locale, timezone)
|
|
372
|
-
|
|
373
|
-
const props = {
|
|
374
|
-
description: 'date_time',
|
|
375
|
-
dateRenderLabel: 'date-input',
|
|
376
|
-
prevMonthLabel: 'Previous month',
|
|
377
|
-
nextMonthLabel: 'Next month',
|
|
378
|
-
timeRenderLabel: 'time-input',
|
|
379
|
-
invalidDateTimeMessage: 'whoops',
|
|
380
|
-
locale,
|
|
381
|
-
timezone,
|
|
382
|
-
onChange,
|
|
383
|
-
value: dateTime.toISOString()
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
const { container, rerender } = render(<DateTimeInput {...props} />)
|
|
387
|
-
expect(container).toHaveTextContent('May 1, 2017 1:30 PM')
|
|
388
|
-
|
|
389
|
-
rerender(<DateTimeInput {...props} locale="fr" />)
|
|
390
|
-
expect(container).toHaveTextContent('1 mai 2017 13:30')
|
|
391
|
-
})
|
|
392
|
-
|
|
393
|
-
it('should update message when timezone changed', () => {
|
|
394
|
-
const onChange = vi.fn()
|
|
395
|
-
const locale = 'en-US'
|
|
396
|
-
const timezone = 'US/Eastern'
|
|
397
|
-
const dateTime = DateTime.parse('2017-05-01T17:30Z', locale, timezone)
|
|
398
|
-
const props = {
|
|
399
|
-
description: 'date_time',
|
|
400
|
-
dateRenderLabel: 'date-input',
|
|
401
|
-
prevMonthLabel: 'Previous month',
|
|
402
|
-
nextMonthLabel: 'Next month',
|
|
403
|
-
timeRenderLabel: 'time-input',
|
|
404
|
-
invalidDateTimeMessage: 'whoops',
|
|
405
|
-
locale,
|
|
406
|
-
timezone,
|
|
407
|
-
onChange,
|
|
408
|
-
value: dateTime.toISOString()
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
const { container, rerender } = render(<DateTimeInput {...props} />)
|
|
412
|
-
expect(container).toHaveTextContent('May 1, 2017 1:30 PM')
|
|
413
|
-
|
|
414
|
-
rerender(<DateTimeInput {...props} timezone="Europe/Paris" />)
|
|
415
|
-
expect(container).toHaveTextContent('May 1, 2017 7:30 PM')
|
|
416
|
-
})
|
|
417
|
-
|
|
418
|
-
it('should show error message if initial value is invalid', async () => {
|
|
419
|
-
const { container } = render(
|
|
420
|
-
<DateTimeInput
|
|
421
|
-
description="date_time"
|
|
422
|
-
dateRenderLabel="date-input"
|
|
423
|
-
prevMonthLabel="Previous month"
|
|
424
|
-
nextMonthLabel="Next month"
|
|
425
|
-
timeRenderLabel="time-input"
|
|
426
|
-
invalidDateTimeMessage="whoops"
|
|
427
|
-
value="totally not a date"
|
|
428
|
-
/>
|
|
429
|
-
)
|
|
430
|
-
const dateInput = screen.getByLabelText('date-input')
|
|
431
|
-
fireEvent.blur(dateInput)
|
|
432
|
-
|
|
433
|
-
await waitFor(() => {
|
|
434
|
-
expect(container).toHaveTextContent('whoops')
|
|
435
|
-
})
|
|
436
|
-
})
|
|
437
|
-
|
|
438
|
-
it('should show error message if initial defaultValue is invalid', async () => {
|
|
439
|
-
const { container } = render(
|
|
440
|
-
<DateTimeInput
|
|
441
|
-
description="date_time"
|
|
442
|
-
dateRenderLabel="date-input"
|
|
443
|
-
prevMonthLabel="Previous month"
|
|
444
|
-
nextMonthLabel="Next month"
|
|
445
|
-
timeRenderLabel="time-input"
|
|
446
|
-
invalidDateTimeMessage="whoops"
|
|
447
|
-
defaultValue="totally not a date"
|
|
448
|
-
/>
|
|
449
|
-
)
|
|
450
|
-
const dateInput = screen.getByLabelText('date-input')
|
|
451
|
-
fireEvent.blur(dateInput)
|
|
452
|
-
|
|
453
|
-
await waitFor(() => {
|
|
454
|
-
expect(container).toHaveTextContent('whoops')
|
|
455
|
-
})
|
|
456
|
-
})
|
|
457
|
-
|
|
458
|
-
it('should update error message when value is invalid', async () => {
|
|
459
|
-
const locale = 'en-US'
|
|
460
|
-
const timezone = 'US/Eastern'
|
|
461
|
-
const dateTime = DateTime.parse('2017-05-01T17:30Z', locale, timezone)
|
|
462
|
-
const props = {
|
|
463
|
-
description: 'date_time',
|
|
464
|
-
dateRenderLabel: 'date-input',
|
|
465
|
-
prevMonthLabel: 'Previous month',
|
|
466
|
-
nextMonthLabel: 'Next month',
|
|
467
|
-
timeRenderLabel: 'time-input',
|
|
468
|
-
invalidDateTimeMessage: 'whoops',
|
|
469
|
-
locale,
|
|
470
|
-
timezone,
|
|
471
|
-
value: dateTime.toISOString()
|
|
472
|
-
}
|
|
473
|
-
const { container, rerender } = render(<DateTimeInput {...props} />)
|
|
474
|
-
expect(container).toHaveTextContent('May 1, 2017 1:30 PM')
|
|
475
|
-
|
|
476
|
-
rerender(<DateTimeInput {...props} value="A very invalid date" />)
|
|
477
|
-
const dateInput = screen.getByLabelText('date-input')
|
|
478
|
-
fireEvent.blur(dateInput)
|
|
479
|
-
|
|
480
|
-
await waitFor(() => {
|
|
481
|
-
expect(container).toHaveTextContent('whoops')
|
|
482
|
-
})
|
|
483
|
-
})
|
|
484
|
-
|
|
485
|
-
it('should show supplied message', async () => {
|
|
486
|
-
const locale = 'en-US'
|
|
487
|
-
const timezone = 'US/Eastern'
|
|
488
|
-
const dateTime = DateTime.parse('2017-05-01T17:30Z', locale, timezone)
|
|
489
|
-
const props = {
|
|
490
|
-
description: 'date_time',
|
|
491
|
-
dateRenderLabel: 'date-input',
|
|
492
|
-
prevMonthLabel: 'Previous month',
|
|
493
|
-
nextMonthLabel: 'Next month',
|
|
494
|
-
timeRenderLabel: 'time-input',
|
|
495
|
-
invalidDateTimeMessage: 'whoops',
|
|
496
|
-
locale,
|
|
497
|
-
timezone,
|
|
498
|
-
value: dateTime.toISOString()
|
|
499
|
-
}
|
|
500
|
-
const { container, rerender } = render(<DateTimeInput {...props} />)
|
|
501
|
-
expect(container).toHaveTextContent('May 1, 2017 1:30 PM')
|
|
502
|
-
|
|
503
|
-
rerender(
|
|
504
|
-
<DateTimeInput
|
|
505
|
-
{...props}
|
|
506
|
-
messages={[{ text: 'message_text', type: 'success' }]}
|
|
507
|
-
/>
|
|
508
|
-
)
|
|
509
|
-
const dateInput = screen.getByLabelText('date-input')
|
|
510
|
-
fireEvent.blur(dateInput)
|
|
511
|
-
|
|
512
|
-
await waitFor(() => {
|
|
513
|
-
expect(container).toHaveTextContent('May 1, 2017 1:30 PM')
|
|
514
|
-
expect(container).toHaveTextContent('message_text')
|
|
515
|
-
})
|
|
516
|
-
})
|
|
517
|
-
|
|
518
|
-
it('should not show built in message when `showMessages` is false', async () => {
|
|
519
|
-
const locale = 'en-US'
|
|
520
|
-
const timezone = 'US/Eastern'
|
|
521
|
-
const dateTime = DateTime.parse('2017-05-01T17:30Z', locale, timezone)
|
|
522
|
-
const props = {
|
|
523
|
-
description: 'date_time',
|
|
524
|
-
dateRenderLabel: 'date-input',
|
|
525
|
-
prevMonthLabel: 'Previous month',
|
|
526
|
-
nextMonthLabel: 'Next month',
|
|
527
|
-
timeRenderLabel: 'time-input',
|
|
528
|
-
invalidDateTimeMessage: 'whoops',
|
|
529
|
-
locale,
|
|
530
|
-
timezone,
|
|
531
|
-
value: dateTime.toISOString()
|
|
532
|
-
}
|
|
533
|
-
const { container, rerender } = render(
|
|
534
|
-
<DateTimeInput {...props} showMessages={false} />
|
|
535
|
-
)
|
|
536
|
-
expect(container).not.toHaveTextContent('2017')
|
|
537
|
-
|
|
538
|
-
rerender(
|
|
539
|
-
<DateTimeInput
|
|
540
|
-
{...props}
|
|
541
|
-
showMessages={false}
|
|
542
|
-
messages={[{ text: 'message_text', type: 'success' }]}
|
|
543
|
-
/>
|
|
544
|
-
)
|
|
545
|
-
const dateInput = screen.getByLabelText('date-input')
|
|
546
|
-
|
|
547
|
-
fireEvent.blur(dateInput)
|
|
548
|
-
|
|
549
|
-
await waitFor(() => {
|
|
550
|
-
expect(container).not.toHaveTextContent('2017')
|
|
551
|
-
expect(container).toHaveTextContent('message_text')
|
|
552
|
-
})
|
|
553
|
-
|
|
554
|
-
rerender(
|
|
555
|
-
<DateTimeInput
|
|
556
|
-
{...props}
|
|
557
|
-
messages={[{ text: 'message_text', type: 'success' }]}
|
|
558
|
-
showMessages={true}
|
|
559
|
-
/>
|
|
560
|
-
)
|
|
561
|
-
|
|
562
|
-
fireEvent.blur(screen.getByLabelText('date-input'))
|
|
563
|
-
|
|
564
|
-
await waitFor(() => {
|
|
565
|
-
expect(container).toHaveTextContent('2017')
|
|
566
|
-
expect(container).toHaveTextContent('message_text')
|
|
567
|
-
})
|
|
568
|
-
})
|
|
569
|
-
|
|
570
|
-
it('should read locale and timezone from context', () => {
|
|
571
|
-
const onChange = vi.fn()
|
|
572
|
-
const dateTime = DateTime.parse('2017-05-01T17:30Z', 'en-US', 'GMT')
|
|
573
|
-
const { container } = render(
|
|
574
|
-
// Africa/Nairobi is GMT +3
|
|
575
|
-
<ApplyLocale locale="fr" timezone="Africa/Nairobi">
|
|
576
|
-
<DateTimeInput
|
|
577
|
-
description="date_time"
|
|
578
|
-
dateRenderLabel="date-input"
|
|
579
|
-
prevMonthLabel="Previous month"
|
|
580
|
-
nextMonthLabel="Next month"
|
|
581
|
-
timeRenderLabel="time-input"
|
|
582
|
-
invalidDateTimeMessage="whoops"
|
|
583
|
-
value={dateTime.toISOString()}
|
|
584
|
-
onChange={onChange}
|
|
585
|
-
/>
|
|
586
|
-
</ApplyLocale>
|
|
587
|
-
)
|
|
588
|
-
|
|
589
|
-
expect(container).toHaveTextContent('lundi 1 mai 2017 20:30')
|
|
590
|
-
})
|
|
591
|
-
|
|
592
|
-
it('should format date according to dateFormat', async () => {
|
|
593
|
-
const onChange = vi.fn()
|
|
594
|
-
const dateTime = DateTime.parse('2017-05-01T17:30Z', 'en-US', 'GMT')
|
|
595
|
-
const props = {
|
|
596
|
-
description: 'date_time',
|
|
597
|
-
dateRenderLabel: 'date-input',
|
|
598
|
-
prevMonthLabel: 'Previous month',
|
|
599
|
-
nextMonthLabel: 'Next month',
|
|
600
|
-
timeRenderLabel: 'time-input',
|
|
601
|
-
invalidDateTimeMessage: 'whoops',
|
|
602
|
-
value: dateTime.toISOString(),
|
|
603
|
-
onChange
|
|
604
|
-
}
|
|
605
|
-
|
|
606
|
-
const { rerender } = render(
|
|
607
|
-
<DateTimeInput
|
|
608
|
-
{...props}
|
|
609
|
-
dateFormat="l" // localized numeric date like 9/4/2017
|
|
610
|
-
/>
|
|
611
|
-
)
|
|
612
|
-
const dateInput = screen.getByLabelText('date-input')
|
|
613
|
-
|
|
614
|
-
expect(dateInput).toHaveValue('5/1/2017')
|
|
615
|
-
|
|
616
|
-
rerender(<DateTimeInput {...props} dateFormat="yyyy MMMM" />)
|
|
617
|
-
|
|
618
|
-
fireEvent.blur(screen.getByLabelText('date-input'))
|
|
619
|
-
|
|
620
|
-
await waitFor(() => {
|
|
621
|
-
expect(dateInput).toHaveValue('2017 May')
|
|
622
|
-
})
|
|
623
|
-
})
|
|
624
|
-
|
|
625
|
-
it('should format bottom message according to messageFormat', async () => {
|
|
626
|
-
const onChange = vi.fn()
|
|
627
|
-
const dateTime = DateTime.parse('2017-05-01T17:30Z', 'en-US', 'GMT')
|
|
628
|
-
const props = {
|
|
629
|
-
description: 'date_time',
|
|
630
|
-
dateRenderLabel: 'date-input',
|
|
631
|
-
prevMonthLabel: 'Previous month',
|
|
632
|
-
nextMonthLabel: 'Next month',
|
|
633
|
-
timeRenderLabel: 'time-input',
|
|
634
|
-
invalidDateTimeMessage: 'whoops',
|
|
635
|
-
value: dateTime.toISOString(),
|
|
636
|
-
onChange,
|
|
637
|
-
locale: 'en-US',
|
|
638
|
-
timezone: 'US/Eastern'
|
|
639
|
-
}
|
|
640
|
-
|
|
641
|
-
const { container, rerender } = render(<DateTimeInput {...props} />)
|
|
642
|
-
expect(container).toHaveTextContent('Monday, May 1, 2017 1:30 PM')
|
|
643
|
-
|
|
644
|
-
rerender(<DateTimeInput {...props} messageFormat="l, LT" />)
|
|
645
|
-
fireEvent.blur(screen.getByLabelText('date-input'))
|
|
646
|
-
|
|
647
|
-
await waitFor(() => {
|
|
648
|
-
expect(container).toHaveTextContent('5/1/2017, 1:30 PM')
|
|
649
|
-
})
|
|
650
|
-
})
|
|
651
|
-
|
|
652
|
-
it('should update Date and Time inputs when value prop changes', async () => {
|
|
653
|
-
const locale = 'en-US'
|
|
654
|
-
const timezone = 'US/Eastern'
|
|
655
|
-
const dateTime = DateTime.parse('2017-05-01T17:30Z', locale, timezone)
|
|
656
|
-
const props = {
|
|
657
|
-
description: 'date_time',
|
|
658
|
-
dateRenderLabel: 'date-input',
|
|
659
|
-
prevMonthLabel: 'Previous month',
|
|
660
|
-
nextMonthLabel: 'Next month',
|
|
661
|
-
timeRenderLabel: 'time-input',
|
|
662
|
-
invalidDateTimeMessage: 'whoops',
|
|
663
|
-
locale,
|
|
664
|
-
timezone,
|
|
665
|
-
value: dateTime.toISOString()
|
|
666
|
-
}
|
|
667
|
-
const { container, rerender } = render(<DateTimeInput {...props} />)
|
|
668
|
-
expect(container).toHaveTextContent('May 1, 2017 1:30 PM')
|
|
669
|
-
|
|
670
|
-
const newDateStr = '2022-03-29T19:00Z'
|
|
671
|
-
const newDateTime = DateTime.parse(newDateStr, locale, timezone)
|
|
672
|
-
|
|
673
|
-
rerender(<DateTimeInput {...props} value={newDateStr} />)
|
|
674
|
-
const dateInput = screen.getByLabelText('date-input')
|
|
675
|
-
const timeInput = screen.getByLabelText('time-input')
|
|
676
|
-
|
|
677
|
-
fireEvent.blur(dateInput)
|
|
678
|
-
|
|
679
|
-
await waitFor(() => {
|
|
680
|
-
expect(dateInput).toHaveValue(newDateTime.format('LL'))
|
|
681
|
-
expect(timeInput).toHaveValue(newDateTime.format('LT'))
|
|
682
|
-
})
|
|
683
|
-
})
|
|
684
|
-
|
|
685
|
-
it("should throw warning if initialTimeForNewDate prop's value is not HH:MM", async () => {
|
|
686
|
-
const initialTimeForNewDate = 'WRONG_FORMAT'
|
|
687
|
-
|
|
688
|
-
render(
|
|
689
|
-
<DateTimeInput
|
|
690
|
-
description="date_time"
|
|
691
|
-
dateRenderLabel="date-input"
|
|
692
|
-
prevMonthLabel="Previous month"
|
|
693
|
-
nextMonthLabel="Next month"
|
|
694
|
-
timeRenderLabel="time-input"
|
|
695
|
-
invalidDateTimeMessage="whoops"
|
|
696
|
-
locale={'en-US'}
|
|
697
|
-
timezone={'US/Eastern'}
|
|
698
|
-
initialTimeForNewDate={initialTimeForNewDate}
|
|
699
|
-
/>
|
|
700
|
-
)
|
|
701
|
-
|
|
702
|
-
expect(consoleErrorMock.mock.calls[0][2]).toEqual(
|
|
703
|
-
expect.stringContaining(
|
|
704
|
-
`Invalid prop \`initialTimeForNewDate\` \`${initialTimeForNewDate}\` supplied to \`DateTimeInput\`, expected a HH:MM formatted string.`
|
|
705
|
-
)
|
|
706
|
-
)
|
|
707
|
-
})
|
|
708
|
-
|
|
709
|
-
it("should throw warning if date select and initialTimeForNewDate prop's value is not HH:MM", async () => {
|
|
710
|
-
const initialTimeForNewDate = 'WRONG_FORMAT'
|
|
711
|
-
|
|
712
|
-
render(
|
|
713
|
-
<DateTimeInput
|
|
714
|
-
description="date_time"
|
|
715
|
-
dateRenderLabel="date-input"
|
|
716
|
-
prevMonthLabel="Previous month"
|
|
717
|
-
nextMonthLabel="Next month"
|
|
718
|
-
timeRenderLabel="time-input"
|
|
719
|
-
invalidDateTimeMessage="whoops"
|
|
720
|
-
locale={'en-US'}
|
|
721
|
-
timezone={'US/Eastern'}
|
|
722
|
-
initialTimeForNewDate={initialTimeForNewDate}
|
|
723
|
-
/>
|
|
724
|
-
)
|
|
725
|
-
|
|
726
|
-
const dateInput = screen.getByLabelText('date-input')
|
|
727
|
-
|
|
728
|
-
fireEvent.change(dateInput, { target: { value: 'May 1, 2017' } })
|
|
729
|
-
fireEvent.keyDown(dateInput, { key: 'Enter', code: 'Enter' })
|
|
730
|
-
fireEvent.blur(dateInput)
|
|
731
|
-
|
|
732
|
-
await waitFor(() => {
|
|
733
|
-
expect(consoleErrorMock.mock.calls[0][0]).toBe(
|
|
734
|
-
`Warning: [DateTimeInput] initialTimeForNewDate prop is not in the correct format. Please use HH:MM format.`
|
|
735
|
-
)
|
|
736
|
-
})
|
|
737
|
-
})
|
|
738
|
-
|
|
739
|
-
it('should throw warning if initialTimeForNewDate prop hour and minute values are not in interval', async () => {
|
|
740
|
-
const initialTimeForNewDate = '99:99'
|
|
741
|
-
|
|
742
|
-
render(
|
|
743
|
-
<DateTimeInput
|
|
744
|
-
description="date_time"
|
|
745
|
-
dateRenderLabel="date-input"
|
|
746
|
-
prevMonthLabel="Previous month"
|
|
747
|
-
nextMonthLabel="Next month"
|
|
748
|
-
timeRenderLabel="time-input"
|
|
749
|
-
invalidDateTimeMessage="whoops"
|
|
750
|
-
locale={'en-US'}
|
|
751
|
-
timezone={'US/Eastern'}
|
|
752
|
-
initialTimeForNewDate={initialTimeForNewDate}
|
|
753
|
-
/>
|
|
754
|
-
)
|
|
755
|
-
const dateInput = screen.getByLabelText('date-input')
|
|
756
|
-
|
|
757
|
-
fireEvent.change(dateInput, { target: { value: 'May 1, 2017' } })
|
|
758
|
-
fireEvent.keyDown(dateInput, { key: 'Enter', code: 'Enter' })
|
|
759
|
-
fireEvent.blur(dateInput)
|
|
760
|
-
|
|
761
|
-
await waitFor(() => {
|
|
762
|
-
expect(consoleErrorMock.mock.calls[0][0]).toEqual(
|
|
763
|
-
expect.stringContaining(
|
|
764
|
-
`Warning: [DateTimeInput] 0 <= hour < 24 and 0 <= minute < 60 for initialTimeForNewDate prop.`
|
|
765
|
-
)
|
|
766
|
-
)
|
|
767
|
-
})
|
|
768
|
-
})
|
|
769
|
-
|
|
770
|
-
it('should initial render prefer defaultValue time over initialTimeForNewDate', async () => {
|
|
771
|
-
const locale = 'en-US'
|
|
772
|
-
const timezone = 'US/Eastern'
|
|
773
|
-
const initialTimeForNewDate = '16:16'
|
|
774
|
-
const defaultValue = '2018-01-18T13:30'
|
|
775
|
-
|
|
776
|
-
const { container } = render(
|
|
777
|
-
<DateTimeInput
|
|
778
|
-
description="date_time"
|
|
779
|
-
dateRenderLabel="date-input"
|
|
780
|
-
prevMonthLabel="Previous month"
|
|
781
|
-
nextMonthLabel="Next month"
|
|
782
|
-
timeRenderLabel="time-input"
|
|
783
|
-
invalidDateTimeMessage="whoops"
|
|
784
|
-
defaultValue={defaultValue}
|
|
785
|
-
locale={locale}
|
|
786
|
-
timezone={timezone}
|
|
787
|
-
initialTimeForNewDate={initialTimeForNewDate}
|
|
788
|
-
/>
|
|
789
|
-
)
|
|
790
|
-
const timeInput = screen.getByLabelText('time-input')
|
|
791
|
-
|
|
792
|
-
expect(timeInput).toHaveValue('1:30 PM')
|
|
793
|
-
expect(container).toHaveTextContent('Thursday, January 18, 2018 1:30 PM')
|
|
794
|
-
})
|
|
795
|
-
})
|