@instructure/ui-time-select 10.2.2-snapshot-14 → 10.2.2-snapshot-16

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