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