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