@instructure/ui-color-picker 11.7.2 → 11.7.3-snapshot-2

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,618 +0,0 @@
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
-
25
- import { render, screen, waitFor, fireEvent } from '@testing-library/react'
26
- import { userEvent } from '@testing-library/user-event'
27
- import { vi } from 'vitest'
28
- import '@testing-library/jest-dom'
29
-
30
- import { runAxeCheck } from '@instructure/ui-axe-check'
31
- import conversions from '@instructure/ui-color-utils'
32
-
33
- import type { ColorPickerProps } from '../v2/props'
34
- import { ContrastStrength } from '../v2/props'
35
- import { ColorPicker } from '../v2'
36
-
37
- const SimpleExample = (props: Partial<ColorPickerProps>) => {
38
- return (
39
- <ColorPicker
40
- onChange={props.onChange}
41
- value={props.value}
42
- placeholderText="Enter HEX"
43
- label="Color Input"
44
- {...props}
45
- />
46
- )
47
- }
48
-
49
- describe('<ColorPicker />', () => {
50
- let consoleErrorMock: ReturnType<typeof vi.spyOn>
51
- let consoleWarningMock: ReturnType<typeof vi.spyOn>
52
-
53
- beforeEach(() => {
54
- // Mocking console to prevent test output pollution and expect for messages
55
- consoleErrorMock = vi
56
- .spyOn(console, 'error')
57
- .mockImplementation(() => {}) as any
58
- consoleWarningMock = vi
59
- .spyOn(console, 'warn')
60
- .mockImplementation(() => {}) as any
61
- })
62
-
63
- afterEach(() => {
64
- consoleErrorMock.mockRestore()
65
- consoleWarningMock.mockRestore()
66
- })
67
-
68
- describe('simple input mode', () => {
69
- it('should render correctly', async () => {
70
- const { container } = render(<SimpleExample />)
71
- expect(container.firstChild).toBeInTheDocument()
72
- })
73
-
74
- it('should work controlled', async () => {
75
- const color = '#FFF'
76
- const onChange = vi.fn()
77
-
78
- const { rerender } = render(
79
- <SimpleExample value={color} onChange={onChange} />
80
- )
81
-
82
- const input = screen.getByRole('textbox')
83
- expect(input).toHaveValue('FFF')
84
-
85
- // set new value
86
- rerender(<SimpleExample value={`${color}555`} onChange={onChange} />)
87
-
88
- const inputUpdated = screen.getByRole('textbox')
89
- expect(inputUpdated).toHaveValue('FFF555')
90
- })
91
-
92
- it('should accept 3 digit hex code', async () => {
93
- const color = '0CB'
94
- render(<SimpleExample />)
95
-
96
- const input = screen.getByRole('textbox')
97
-
98
- await userEvent.type(input, color)
99
- fireEvent.blur(input)
100
-
101
- await waitFor(() => {
102
- expect(input).toHaveValue(color)
103
- })
104
- })
105
-
106
- it('should accept 6 digit hex code', async () => {
107
- const color = '0CBF2D'
108
- render(<SimpleExample />)
109
-
110
- const input = screen.getByRole('textbox')
111
-
112
- await userEvent.type(input, color)
113
- fireEvent.blur(input)
114
-
115
- await waitFor(() => {
116
- expect(input).toHaveValue(color)
117
- })
118
- })
119
-
120
- it('should not accept not valid hex code', async () => {
121
- const color = 'WWWZZZ'
122
- render(<SimpleExample />)
123
-
124
- const input = screen.getByRole('textbox')
125
-
126
- await userEvent.type(input, color)
127
- fireEvent.blur(input)
128
-
129
- await waitFor(() => {
130
- expect(input).not.toHaveValue(color)
131
- })
132
- })
133
-
134
- it('should not allow more than 6 characters', async () => {
135
- const color = '0CBF2D1234567'
136
- render(<SimpleExample />)
137
-
138
- const input = screen.getByRole('textbox')
139
-
140
- await userEvent.type(input, color)
141
- fireEvent.blur(input)
142
-
143
- await waitFor(() => {
144
- expect(input).toHaveValue('0CBF2D')
145
- })
146
- })
147
-
148
- it('should not allow input when disabled', async () => {
149
- render(<SimpleExample disabled />)
150
-
151
- const input = screen.getByRole('textbox')
152
- expect(input).toHaveAttribute('disabled')
153
- })
154
-
155
- for (const contrastStrength of [
156
- 'min',
157
- 'mid',
158
- 'max'
159
- ] as ContrastStrength[]) {
160
- it(`should check contrast correctly when color has enough contrast [contrastStrength=${contrastStrength}]`, async () => {
161
- //oxford in canvas color palette, should be valid with all contrast strenght checkers
162
- const colorToCheck = '394B58'
163
- const { container } = render(
164
- <SimpleExample
165
- checkContrast={{
166
- isStrict: false,
167
- contrastStrength: contrastStrength
168
- }}
169
- />
170
- )
171
- const input = screen.getByRole('textbox')
172
-
173
- await userEvent.type(input, colorToCheck)
174
- fireEvent.blur(input)
175
-
176
- await waitFor(() => {
177
- expect(input).toHaveValue(colorToCheck)
178
-
179
- const successIconWrapper = container.querySelector(
180
- 'div[class$="-colorPicker__successIcon"]'
181
- )
182
- const successIcon = container.querySelector(
183
- 'svg[name="Check"]'
184
- )
185
-
186
- expect(successIconWrapper).toBeInTheDocument()
187
- expect(successIcon).toBeInTheDocument()
188
- })
189
- })
190
-
191
- it(`should check contrast correctly when color does not have enough contrast [contrastStrength=${contrastStrength}, isStrict=false]`, async () => {
192
- //porcelain in canvas color palette, it should be failing even the min check
193
- const colorToCheck = 'F5F5F5'
194
- const { container } = render(
195
- <SimpleExample
196
- checkContrast={{
197
- isStrict: false,
198
- contrastStrength: contrastStrength
199
- }}
200
- />
201
- )
202
- const input = screen.getByRole('textbox')
203
-
204
- await userEvent.type(input, colorToCheck)
205
- fireEvent.blur(input)
206
-
207
- await waitFor(() => {
208
- expect(input).toHaveValue(colorToCheck)
209
-
210
- const warningIconWrapper = container.querySelector(
211
- 'div[class$="-colorPicker__errorIcons"]'
212
- )
213
- const warningIcon = container.querySelector('svg[name="CircleAlert"]')
214
-
215
- expect(warningIconWrapper).toBeInTheDocument()
216
- expect(warningIcon).toBeInTheDocument()
217
- })
218
- })
219
-
220
- it(`should check contrast correctly when color does not have enough contrast [contrastStrength=${contrastStrength}, isStrict=true]`, async () => {
221
- //porcelain in canvas color palette, it should be failing even the min check
222
- const colorToCheck = 'F5F5F5'
223
- const { container } = render(
224
- <SimpleExample
225
- checkContrast={{
226
- isStrict: true,
227
- contrastStrength: contrastStrength
228
- }}
229
- />
230
- )
231
- const input = screen.getByRole('textbox')
232
-
233
- await userEvent.type(input, colorToCheck)
234
- fireEvent.blur(input)
235
-
236
- await waitFor(() => {
237
- expect(input).toHaveValue(colorToCheck)
238
-
239
- const errorIconWrapper = container.querySelector(
240
- 'div[class$="-colorPicker__errorIcons"]'
241
- )
242
- const errorIcon = container.querySelector('svg[name="CircleX"]')
243
-
244
- expect(errorIconWrapper).toBeInTheDocument()
245
- expect(errorIcon).toBeInTheDocument()
246
- })
247
- })
248
-
249
- it(`should display success message when contrast is met [contrastStrength=${contrastStrength}]`, async () => {
250
- const colorToCheck = '394B58'
251
- render(
252
- <SimpleExample
253
- checkContrast={{
254
- isStrict: false,
255
- contrastStrength: contrastStrength,
256
- renderContrastSuccessMessage: () => [
257
- { type: 'success', text: 'I am a contrast success message' }
258
- ]
259
- }}
260
- />
261
- )
262
- const input = screen.getByRole('textbox')
263
-
264
- await userEvent.type(input, colorToCheck)
265
- fireEvent.blur(input)
266
-
267
- await waitFor(() => {
268
- const successMessage = screen.getByText(
269
- 'I am a contrast success message'
270
- )
271
-
272
- expect(input).toHaveValue(colorToCheck)
273
- expect(successMessage).toBeInTheDocument()
274
- })
275
- })
276
-
277
- it(`should display error message when contrast is not met [contrastStrength=${contrastStrength}, isStrict=false]`, async () => {
278
- const colorToCheck = 'F5F5F5'
279
- render(
280
- <SimpleExample
281
- checkContrast={{
282
- isStrict: false,
283
- contrastStrength: contrastStrength,
284
- renderContrastErrorMessage: () => [
285
- { type: 'error', text: 'I am a contrast warning message' }
286
- ]
287
- }}
288
- />
289
- )
290
- const input = screen.getByRole('textbox')
291
-
292
- await userEvent.type(input, colorToCheck)
293
- fireEvent.blur(input)
294
-
295
- await waitFor(() => {
296
- const warningMessage = screen.getByText(
297
- 'I am a contrast warning message'
298
- )
299
-
300
- expect(input).toHaveValue(colorToCheck)
301
- expect(warningMessage).toBeInTheDocument()
302
- })
303
- })
304
-
305
- it(`should display error message when contrast is not met [contrastStrength=${contrastStrength}, isStrict=true]`, async () => {
306
- const colorToCheck = 'F5F5F5'
307
- render(
308
- <SimpleExample
309
- checkContrast={{
310
- isStrict: true,
311
- contrastStrength: contrastStrength,
312
- renderContrastErrorMessage: () => [
313
- { type: 'error', text: 'I am a contrast error message' }
314
- ]
315
- }}
316
- />
317
- )
318
- const input = screen.getByRole('textbox')
319
-
320
- await userEvent.type(input, colorToCheck)
321
- fireEvent.blur(input)
322
-
323
- await waitFor(() => {
324
- const errorMessage = screen.getByText('I am a contrast error message')
325
-
326
- expect(input).toHaveValue(colorToCheck)
327
- expect(errorMessage).toBeInTheDocument()
328
- })
329
- })
330
- }
331
-
332
- it('should call onChange', async () => {
333
- const onChange = vi.fn()
334
- render(<SimpleExample onChange={onChange} />)
335
-
336
- const input = screen.getByRole('textbox')
337
-
338
- fireEvent.change(input, { target: { value: 'FFF' } })
339
- fireEvent.blur(input)
340
-
341
- await waitFor(() => {
342
- expect(onChange).toHaveBeenLastCalledWith('#FFF')
343
- })
344
- })
345
-
346
- it('should display message when ColorPicker is a required field', async () => {
347
- render(
348
- <SimpleExample
349
- isRequired
350
- renderInvalidColorMessage={() => [
351
- { type: 'error', text: 'I am an invalid color message' }
352
- ]}
353
- renderIsRequiredMessage={() => [
354
- { type: 'error', text: 'I am a required message' }
355
- ]}
356
- />
357
- )
358
- const input = screen.getByRole('textbox')
359
-
360
- fireEvent.focus(input)
361
- fireEvent.blur(input)
362
-
363
- await waitFor(() => {
364
- const requiredMessage = screen.getByText('I am a required message')
365
-
366
- expect(requiredMessage).toBeInTheDocument()
367
- })
368
- })
369
-
370
- it('should display message when color is invalid', async () => {
371
- render(
372
- <SimpleExample
373
- renderInvalidColorMessage={() => [
374
- { type: 'error', text: 'I am an invalid color message' }
375
- ]}
376
- />
377
- )
378
- const input = screen.getByRole('textbox')
379
-
380
- await userEvent.type(input, 'F')
381
- fireEvent.blur(input)
382
-
383
- await waitFor(() => {
384
- const errorMessage = screen.getByText('I am an invalid color message')
385
-
386
- expect(errorMessage).toBeInTheDocument()
387
- })
388
- })
389
-
390
- it('should provide an inputRef prop', async () => {
391
- const inputRef = vi.fn()
392
- render(<SimpleExample inputRef={inputRef} />)
393
- const input = screen.getByRole('textbox')
394
-
395
- expect(inputRef).toHaveBeenCalledWith(input)
396
- })
397
- })
398
-
399
- describe('complex mode', () => {
400
- it('should display trigger button', async () => {
401
- const { container } = render(
402
- <SimpleExample
403
- colorMixerSettings={{
404
- popoverAddButtonLabel: 'add',
405
- popoverCloseButtonLabel: 'close'
406
- }}
407
- />
408
- )
409
- const buttonWrapper = container.querySelector(
410
- 'div[class$="-colorPicker__colorMixerButtonWrapper"]'
411
- )
412
- const button = screen.getByRole('button')
413
-
414
- expect(buttonWrapper).toBeInTheDocument()
415
- expect(button).toBeInTheDocument()
416
- })
417
-
418
- it('should open popover when trigger is clicked', async () => {
419
- render(
420
- <SimpleExample
421
- colorMixerSettings={{
422
- popoverAddButtonLabel: 'add',
423
- popoverCloseButtonLabel: 'close'
424
- }}
425
- />
426
- )
427
- const trigger = screen.getByRole('button')
428
-
429
- expect(trigger).toBeInTheDocument()
430
- expect(trigger).toHaveAttribute('aria-expanded', 'false')
431
-
432
- fireEvent.click(trigger)
433
-
434
- await waitFor(() => {
435
- const buttons = screen.getAllByRole('button')
436
- const popoverContent = document.querySelector(
437
- 'div[class$="-colorPicker__popoverContent"]'
438
- )
439
-
440
- expect(trigger).toHaveAttribute('aria-expanded', 'true')
441
- expect(popoverContent).toBeInTheDocument()
442
-
443
- expect(buttons.length).toBe(2)
444
- expect(buttons[0]).toHaveTextContent('close')
445
- expect(buttons[1]).toHaveTextContent('add')
446
- })
447
- })
448
-
449
- it('should display the color mixer', async () => {
450
- render(
451
- <SimpleExample
452
- colorMixerSettings={{
453
- popoverAddButtonLabel: 'add',
454
- popoverCloseButtonLabel: 'close',
455
- colorMixer: {
456
- withAlpha: false,
457
- rgbRedInputScreenReaderLabel: 'Red input',
458
- rgbBlueInputScreenReaderLabel: 'Blue input',
459
- rgbGreenInputScreenReaderLabel: 'Green input',
460
- rgbAlphaInputScreenReaderLabel: '',
461
- alphaSliderNavigationExplanationScreenReaderLabel: '',
462
- colorSliderNavigationExplanationScreenReaderLabel: '',
463
- colorPaletteNavigationExplanationScreenReaderLabel: ''
464
- }
465
- }}
466
- />
467
- )
468
- const trigger = screen.getByRole('button')
469
-
470
- fireEvent.click(trigger)
471
-
472
- await waitFor(() => {
473
- const redInput = screen.getByLabelText('Red input')
474
- const blueInput = screen.getByLabelText('Blue input')
475
- const greenInput = screen.getByLabelText('Green input')
476
-
477
- expect(redInput).toBeInTheDocument()
478
- expect(blueInput).toBeInTheDocument()
479
- expect(greenInput).toBeInTheDocument()
480
- })
481
- })
482
-
483
- it('should display the correct color in the colormixer when the input is prefilled', async () => {
484
- const color = '0374B5'
485
- render(
486
- <SimpleExample
487
- colorMixerSettings={{
488
- popoverAddButtonLabel: 'add',
489
- popoverCloseButtonLabel: 'close',
490
- colorMixer: {
491
- withAlpha: false,
492
- rgbRedInputScreenReaderLabel: 'Red input',
493
- rgbBlueInputScreenReaderLabel: 'Blue input',
494
- rgbGreenInputScreenReaderLabel: 'Green input',
495
- rgbAlphaInputScreenReaderLabel: '',
496
- alphaSliderNavigationExplanationScreenReaderLabel: '',
497
- colorSliderNavigationExplanationScreenReaderLabel: '',
498
- colorPaletteNavigationExplanationScreenReaderLabel: ''
499
- }
500
- }}
501
- />
502
- )
503
- const input = screen.getByRole('textbox')
504
- const trigger = screen.getByRole('button')
505
-
506
- await userEvent.type(input, color)
507
- fireEvent.blur(input)
508
- fireEvent.click(trigger)
509
-
510
- await waitFor(() => {
511
- const redInput = screen.getByLabelText('Red input') as HTMLInputElement
512
- const blueInput = screen.getByLabelText(
513
- 'Blue input'
514
- ) as HTMLInputElement
515
- const greenInput = screen.getByLabelText(
516
- 'Green input'
517
- ) as HTMLInputElement
518
- const convertedColor = conversions.colorToRGB(`#${color}`)
519
-
520
- const actualColor = {
521
- r: parseInt(redInput.value),
522
- g: parseInt(greenInput.value),
523
- b: parseInt(blueInput.value),
524
- a: 1
525
- }
526
-
527
- expect(convertedColor).toStrictEqual(actualColor)
528
- })
529
- })
530
-
531
- it('should trigger onChange when selected color is added from colorMixer', async () => {
532
- const onChange = vi.fn()
533
- const rgb = { r: 131, g: 6, b: 25, a: 1 }
534
- render(
535
- <SimpleExample
536
- onChange={onChange}
537
- colorMixerSettings={{
538
- popoverAddButtonLabel: 'add',
539
- popoverCloseButtonLabel: 'close',
540
- colorMixer: {
541
- withAlpha: false,
542
- rgbRedInputScreenReaderLabel: 'Red input',
543
- rgbBlueInputScreenReaderLabel: 'Blue input',
544
- rgbGreenInputScreenReaderLabel: 'Green input',
545
- colorSliderNavigationExplanationScreenReaderLabel: '',
546
- rgbAlphaInputScreenReaderLabel: '',
547
- alphaSliderNavigationExplanationScreenReaderLabel: '',
548
- colorPaletteNavigationExplanationScreenReaderLabel: ''
549
- }
550
- }}
551
- />
552
- )
553
-
554
- const trigger = screen.getByRole('button')
555
-
556
- fireEvent.click(trigger)
557
-
558
- await waitFor(() => {
559
- const addBtn = screen.getByRole('button', { name: 'add' })
560
- const redInput = screen.getByLabelText('Red input') as HTMLInputElement
561
- const greenInput = screen.getByLabelText(
562
- 'Green input'
563
- ) as HTMLInputElement
564
- const blueInput = screen.getByLabelText(
565
- 'Blue input'
566
- ) as HTMLInputElement
567
-
568
- fireEvent.change(redInput, { target: { value: `${rgb.r}` } })
569
- fireEvent.change(greenInput, { target: { value: `${rgb.g}` } })
570
- fireEvent.change(blueInput, { target: { value: `${rgb.b}` } })
571
-
572
- fireEvent.click(addBtn)
573
-
574
- expect(onChange).toHaveBeenCalledWith(conversions.color2hex(rgb))
575
- })
576
- })
577
- })
578
-
579
- describe('custom popover mode', () => {
580
- it('should throw warning if children and settings object are passed too', async () => {
581
- render(
582
- <SimpleExample
583
- colorMixerSettings={{
584
- popoverAddButtonLabel: 'add',
585
- popoverCloseButtonLabel: 'close'
586
- }}
587
- >
588
- {() => <div></div>}
589
- </SimpleExample>
590
- )
591
-
592
- await waitFor(() => {
593
- expect(consoleWarningMock.mock.calls[0][0]).toEqual(
594
- expect.stringContaining(
595
- 'Warning: You should either use children, colorMixerSettings or neither, not both. In this case, the colorMixerSettings will be ignored.'
596
- )
597
- )
598
- })
599
- })
600
-
601
- it('should display trigger button', async () => {
602
- render(<SimpleExample>{() => <div></div>}</SimpleExample>)
603
-
604
- const trigger = screen.getByRole('button')
605
- expect(trigger).toBeInTheDocument()
606
- expect(trigger).toHaveAttribute('data-popover-trigger', 'true')
607
- })
608
- })
609
-
610
- describe('should be accessible', () => {
611
- it('a11y', async () => {
612
- const { container } = render(<SimpleExample />)
613
- const axeCheck = await runAxeCheck(container)
614
-
615
- expect(axeCheck).toBe(true)
616
- })
617
- })
618
- })
@@ -1,2 +0,0 @@
1
- import '@testing-library/jest-dom';
2
- //# sourceMappingURL=ColorContrast.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ColorContrast.test.d.ts","sourceRoot":"","sources":["../../../src/ColorContrast/v2/ColorContrast.test.tsx"],"names":[],"mappings":"AA0BA,OAAO,2BAA2B,CAAA"}
@@ -1,2 +0,0 @@
1
- import '@testing-library/jest-dom';
2
- //# sourceMappingURL=ColorPicker.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ColorPicker.test.d.ts","sourceRoot":"","sources":["../../../src/ColorPicker/v2/ColorPicker.test.tsx"],"names":[],"mappings":"AA2BA,OAAO,2BAA2B,CAAA"}