@instructure/ui-time-select 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,352 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
- var _react = require("@testing-library/react");
5
- var _vitest = require("vitest");
6
- var _userEvent = _interopRequireDefault(require("@testing-library/user-event"));
7
- var _momentTimezone = _interopRequireDefault(require("moment-timezone"));
8
- require("@testing-library/jest-dom");
9
- var _generateA11yTests = require("@instructure/ui-scripts/lib/test/generateA11yTests");
10
- var _runAxeCheck = require("@instructure/ui-axe-check/lib/runAxeCheck.js");
11
- var _ApplyLocale = require("@instructure/ui-i18n/lib/ApplyLocale");
12
- var _index = require("../index");
13
- var _TimeSelect7 = _interopRequireDefault(require("../__examples__/TimeSelect.examples"));
14
- var _jsxRuntime = require("@emotion/react/jsx-runtime");
15
- var _TimeSelect, _TimeSelect2, _TimeSelect3, _TimeSelect4, _TimeSelect5, _TimeSelect6;
16
- /*
17
- * The MIT License (MIT)
18
- *
19
- * Copyright (c) 2015 - present Instructure, Inc.
20
- *
21
- * Permission is hereby granted, free of charge, to any person obtaining a copy
22
- * of this software and associated documentation files (the "Software"), to deal
23
- * in the Software without restriction, including without limitation the rights
24
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25
- * copies of the Software, and to permit persons to whom the Software is
26
- * furnished to do so, subject to the following conditions:
27
- *
28
- * The above copyright notice and this permission notice shall be included in all
29
- * copies or substantial portions of the Software.
30
- *
31
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37
- * SOFTWARE.
38
- */
39
- // eslint-disable-next-line no-restricted-imports
40
- describe('<TimeSelect />', () => {
41
- let consoleWarningMock;
42
- let consoleErrorMock;
43
- beforeEach(() => {
44
- // Mocking console to prevent test output pollution
45
- consoleWarningMock = _vitest.vi.spyOn(console, 'warn').mockImplementation(() => {});
46
- consoleErrorMock = _vitest.vi.spyOn(console, 'error').mockImplementation(() => {});
47
- });
48
- afterEach(() => {
49
- consoleWarningMock.mockRestore();
50
- consoleErrorMock.mockRestore();
51
- });
52
- it('should fire onFocus when input gains focus', async () => {
53
- const onFocus = _vitest.vi.fn();
54
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.TimeSelect, {
55
- renderLabel: "Choose a time",
56
- onFocus: onFocus
57
- }));
58
- const input = _react.screen.getByRole('combobox');
59
- input.focus();
60
- await (0, _react.waitFor)(() => {
61
- expect(onFocus).toHaveBeenCalled();
62
- });
63
- });
64
- it('should render a default value', async () => {
65
- const defaultValue = _momentTimezone.default.tz('1986-05-17T18:00:00.000Z', _momentTimezone.default.ISO_8601, 'en', 'US/Eastern');
66
- const onChange = _vitest.vi.fn();
67
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.TimeSelect, {
68
- renderLabel: "Choose a time",
69
- onChange: onChange,
70
- timezone: "US/Eastern",
71
- defaultValue: defaultValue.toISOString()
72
- }));
73
- const input = _react.screen.getByRole('combobox');
74
- expect(input).toHaveValue('2:00 PM');
75
- });
76
- it('should display value when both defaultValue and value are set', async () => {
77
- const value = _momentTimezone.default.tz('1986-05-17T18:00:00.000Z', _momentTimezone.default.ISO_8601, 'en', 'US/Eastern');
78
- const defaultValue = _momentTimezone.default.tz('1986-05-25T19:00:00.000Z', _momentTimezone.default.ISO_8601, 'en', 'US/Eastern');
79
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.TimeSelect, {
80
- renderLabel: "Choose a time",
81
- timezone: "US/Eastern",
82
- value: value.toISOString(),
83
- defaultValue: defaultValue.toISOString()
84
- }));
85
- const input = _react.screen.getByRole('combobox');
86
- expect(input).toHaveValue(value.format('LT'));
87
- });
88
- it('should default to the first option if defaultToFirstOption is true', async () => {
89
- (0, _react.render)(_TimeSelect || (_TimeSelect = (0, _jsxRuntime.jsx)(_index.TimeSelect, {
90
- renderLabel: "Choose a time",
91
- defaultToFirstOption: true
92
- })));
93
- const input = _react.screen.getByRole('combobox');
94
- expect(input).toHaveValue('12:00 AM');
95
- });
96
- it('should use the specified timezone', async () => {
97
- const value = _momentTimezone.default.tz('2024-01-11T13:00:00.000Z', _momentTimezone.default.ISO_8601, 'fr', 'UTC');
98
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.TimeSelect, {
99
- renderLabel: "Choose a time",
100
- locale: "fr",
101
- timezone: "Africa/Nairobi" // UTC + 3
102
- ,
103
- value: value.toISOString()
104
- }));
105
- const input = _react.screen.getByRole('combobox');
106
- expect(input).toHaveValue('16:00');
107
- });
108
- it('should use the specified locale', async () => {
109
- const value = _momentTimezone.default.tz('2024-01-11T13:00:00.000Z', _momentTimezone.default.ISO_8601, 'fr',
110
- // 24-hour clock
111
- 'UTC');
112
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.TimeSelect, {
113
- renderLabel: "Choose a time",
114
- locale: "en" // 12-hour clock
115
- ,
116
- timezone: "UTC",
117
- value: value.toISOString()
118
- }));
119
- const input = _react.screen.getByRole('combobox');
120
- expect(input).toHaveValue('1:00 PM');
121
- });
122
- it('should handle winter and summer time based on timezone', async () => {
123
- const valueSummer = _momentTimezone.default.tz('2024-07-11T13:00:00.000Z', _momentTimezone.default.ISO_8601, 'en', 'UTC' // no time offset
124
- );
125
- const valueWinter = _momentTimezone.default.tz('2024-01-11T13:00:00.000Z', _momentTimezone.default.ISO_8601, 'en', 'UTC' // no time offset
126
- );
127
- const _render = (0, _react.render)((0, _jsxRuntime.jsx)(_index.TimeSelect, {
128
- renderLabel: "Choose a time",
129
- locale: "en",
130
- timezone: "Europe/London" // summer time offset
131
- ,
132
- value: valueSummer.toISOString()
133
- })),
134
- rerender = _render.rerender;
135
- const input = _react.screen.getByRole('combobox');
136
- expect(input).toHaveValue('2:00 PM');
137
- rerender((0, _jsxRuntime.jsx)(_index.TimeSelect, {
138
- renderLabel: "Choose a time",
139
- locale: "en",
140
- timezone: "Europe/London" // summer time offset
141
- ,
142
- value: valueWinter.toISOString()
143
- }));
144
- const inputUpdated = _react.screen.getByRole('combobox');
145
- expect(inputUpdated).toHaveValue('1:00 PM');
146
- });
147
- it('should read locale and timezone from context', async () => {
148
- const value = _momentTimezone.default.tz('2017-05-01T17:30Z', _momentTimezone.default.ISO_8601, 'en',
149
- // 12-hour clock format
150
- 'UTC' // no time offset
151
- );
152
- (0, _react.render)((0, _jsxRuntime.jsx)(_ApplyLocale.ApplyLocale, {
153
- locale: "fr" // 24-hour clock format
154
- ,
155
- timezone: "Africa/Nairobi" // UTC + 3
156
- ,
157
- children: (0, _jsxRuntime.jsx)(_index.TimeSelect, {
158
- renderLabel: "Choose a time",
159
- step: 15,
160
- value: value.toISOString()
161
- })
162
- }));
163
- const input = _react.screen.getByRole('combobox');
164
- expect(input).toHaveValue('20:30');
165
- });
166
- it('adding event listeners does not break functionality', async () => {
167
- const onChange = _vitest.vi.fn();
168
- const onKeyDown = _vitest.vi.fn();
169
- const handleInputChange = _vitest.vi.fn();
170
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.TimeSelect, {
171
- renderLabel: "Choose a time",
172
- allowNonStepInput: true,
173
- locale: "en_AU",
174
- timezone: "US/Eastern",
175
- onChange: onChange,
176
- onInputChange: handleInputChange,
177
- onKeyDown: onKeyDown
178
- }));
179
- const input = _react.screen.getByRole('combobox');
180
- await _userEvent.default.type(input, '7:45 PM');
181
- _react.fireEvent.blur(input); // sends onChange event
182
-
183
- expect(onChange).toHaveBeenCalledWith(expect.anything(), {
184
- inputText: '7:45 PM',
185
- value: expect.anything()
186
- });
187
- expect(onKeyDown).toHaveBeenCalled();
188
- expect(handleInputChange).toHaveBeenCalled();
189
- expect(input).toHaveValue('7:45 PM');
190
- });
191
- it('allowClearingSelection allows to clear the value', async () => {
192
- const defaultValue = _momentTimezone.default.tz('1986-05-17T18:00:00.000Z',
193
- // 2:00 PM in US/Eastern
194
- _momentTimezone.default.ISO_8601, 'en', 'US/Eastern');
195
- const onChange = _vitest.vi.fn();
196
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.TimeSelect, {
197
- allowClearingSelection: true,
198
- renderLabel: "Choose a time",
199
- allowNonStepInput: true,
200
- locale: "en_AU",
201
- timezone: "US/Eastern",
202
- defaultValue: defaultValue.toISOString(),
203
- onChange: onChange
204
- }));
205
- const input = _react.screen.getByRole('combobox');
206
- await _userEvent.default.type(input, '{Backspace}{Backspace}{Backspace}{Backspace}{Backspace}{Backspace}{Backspace}');
207
- _react.fireEvent.blur(input); // sends onChange event
208
-
209
- expect(onChange).toHaveBeenCalledWith(expect.anything(), {
210
- inputText: '',
211
- value: ''
212
- });
213
- expect(input).toHaveValue('');
214
- });
215
- it('Can change from defaultValue', async () => {
216
- const defaultValue = _momentTimezone.default.tz('1986-05-17T18:00:00.000Z',
217
- // 2:00 PM in US/Eastern
218
- _momentTimezone.default.ISO_8601, 'en', 'US/Eastern');
219
- const onChange = _vitest.vi.fn();
220
- const onKeyDown = _vitest.vi.fn();
221
- const handleInputChange = _vitest.vi.fn();
222
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.TimeSelect, {
223
- renderLabel: "Choose a time",
224
- allowNonStepInput: true,
225
- locale: "en_AU",
226
- timezone: "US/Eastern",
227
- defaultValue: defaultValue.toISOString(),
228
- onChange: onChange,
229
- onInputChange: handleInputChange,
230
- onKeyDown: onKeyDown
231
- }));
232
- const input = _react.screen.getByRole('combobox');
233
- await _userEvent.default.type(input, '{Backspace}{Backspace}{Backspace}{Backspace}{Backspace}{Backspace}{Backspace}7:45 PM');
234
- _react.fireEvent.blur(input); // sends onChange event
235
- expect(onChange).toHaveBeenCalledWith(expect.anything(), {
236
- inputText: '7:45 PM',
237
- value: '1986-05-17T23:45:00.000Z'
238
- });
239
- expect(onKeyDown).toHaveBeenCalled();
240
- expect(handleInputChange).toHaveBeenCalled();
241
- expect(input).toHaveValue('7:45 PM');
242
- });
243
- it('Reverts to a set value if the current one is invalid', async () => {
244
- const defaultValue = _momentTimezone.default.tz('1986-05-17T18:00:00.000Z',
245
- // 2:00 PM in US/Eastern
246
- _momentTimezone.default.ISO_8601, 'en', 'US/Eastern');
247
- const onChange = _vitest.vi.fn();
248
- const onKeyDown = _vitest.vi.fn();
249
- const handleInputChange = _vitest.vi.fn();
250
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.TimeSelect, {
251
- renderLabel: "Choose a time",
252
- allowNonStepInput: true,
253
- locale: "en_AU",
254
- timezone: "US/Eastern",
255
- defaultValue: defaultValue.toISOString(),
256
- onChange: onChange,
257
- onInputChange: handleInputChange,
258
- onKeyDown: onKeyDown
259
- }));
260
- const input = _react.screen.getByRole('combobox');
261
- await _userEvent.default.type(input, '{Backspace}{Backspace}{Backspace}{Backspace}{Backspace}{Backspace}{Backspace}7:45 PM');
262
- _react.fireEvent.blur(input); // sends onChange event
263
- await _userEvent.default.type(input, 'asdf');
264
- _react.fireEvent.blur(input);
265
- expect(onChange).toHaveBeenCalledWith(expect.anything(), {
266
- inputText: '7:45 PM',
267
- value: '1986-05-17T23:45:00.000Z'
268
- });
269
- expect(onKeyDown).toHaveBeenCalled();
270
- expect(handleInputChange).toHaveBeenCalled();
271
- expect(input).toHaveValue('7:45 PM');
272
- });
273
- describe('input', () => {
274
- it('should render with a custom id if given', async () => {
275
- (0, _react.render)(_TimeSelect2 || (_TimeSelect2 = (0, _jsxRuntime.jsx)(_index.TimeSelect, {
276
- renderLabel: "Choose a time",
277
- id: "timeSelect"
278
- })));
279
- const input = _react.screen.getByRole('combobox');
280
- expect(input).toHaveAttribute('id', 'timeSelect');
281
- });
282
- it('should render readonly when interaction="readonly"', async () => {
283
- (0, _react.render)(_TimeSelect3 || (_TimeSelect3 = (0, _jsxRuntime.jsx)(_index.TimeSelect, {
284
- renderLabel: "Choose a time",
285
- interaction: "readonly"
286
- })));
287
- const input = _react.screen.getByRole('combobox');
288
- expect(input).toHaveAttribute('readonly');
289
- expect(input).not.toHaveAttribute('disabled');
290
- });
291
- it('should render disabled when interaction="disabled"', async () => {
292
- (0, _react.render)(_TimeSelect4 || (_TimeSelect4 = (0, _jsxRuntime.jsx)(_index.TimeSelect, {
293
- renderLabel: "Choose a time",
294
- interaction: "disabled"
295
- })));
296
- const input = _react.screen.getByRole('combobox');
297
- expect(input).toHaveAttribute('disabled');
298
- expect(input).not.toHaveAttribute('readonly');
299
- });
300
- it('should render required when isRequired is true', async () => {
301
- (0, _react.render)(_TimeSelect5 || (_TimeSelect5 = (0, _jsxRuntime.jsx)(_index.TimeSelect, {
302
- renderLabel: "Choose a time",
303
- isRequired: true
304
- })));
305
- const input = _react.screen.getByRole('combobox');
306
- expect(input).toHaveAttribute('required');
307
- });
308
- it('should allow custom props to pass through', async () => {
309
- (0, _react.render)(_TimeSelect6 || (_TimeSelect6 = (0, _jsxRuntime.jsx)(_index.TimeSelect, {
310
- renderLabel: "Choose a time",
311
- "data-custom-attr": "true"
312
- })));
313
- const input = _react.screen.getByRole('combobox');
314
- expect(input).toHaveAttribute('data-custom-attr', 'true');
315
- });
316
- it('should provide a ref to the input element', async () => {
317
- const inputRef = _vitest.vi.fn();
318
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.TimeSelect, {
319
- renderLabel: "Choose a time",
320
- inputRef: inputRef
321
- }));
322
- const input = _react.screen.getByRole('combobox');
323
- expect(inputRef).toHaveBeenCalledWith(input);
324
- });
325
- });
326
- describe('list', () => {
327
- it('should provide a ref to the list element', async () => {
328
- const listRef = _vitest.vi.fn();
329
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.TimeSelect, {
330
- renderLabel: "Choose a time",
331
- listRef: listRef
332
- }));
333
- const input = _react.screen.getByRole('combobox');
334
- await _userEvent.default.click(input);
335
- await (0, _react.waitFor)(() => {
336
- const listbox = _react.screen.getByRole('listbox');
337
- expect(listRef).toHaveBeenCalledWith(listbox);
338
- });
339
- });
340
- });
341
- describe('with generated examples', () => {
342
- const generatedComponents = (0, _generateA11yTests.generateA11yTests)(_index.TimeSelect, _TimeSelect7.default);
343
- it.each(generatedComponents)('should be accessible with example: $description', async ({
344
- content
345
- }) => {
346
- const _render2 = (0, _react.render)(content),
347
- container = _render2.container;
348
- const axeCheck = await (0, _runAxeCheck.runAxeCheck)(container);
349
- expect(axeCheck).toBe(true);
350
- });
351
- });
352
- });