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