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