-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcolor-picker.spec.ts
More file actions
1352 lines (1069 loc) · 42.9 KB
/
Copy pathcolor-picker.spec.ts
File metadata and controls
1352 lines (1069 loc) · 42.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {
elementUpdated,
expect,
fixture,
html,
nextFrame,
} from '@open-wc/testing';
import { spy, stub } from 'sinon';
import {
altKey,
arrowDown,
arrowRight,
arrowUp,
endKey,
escapeKey,
} from '#internals/controllers/key-bindings.js';
import { defineComponents } from '#internals/definitions/defineComponents.js';
import {
createFormAssociatedTestBed,
runAriaProjectionTests,
runExternalLabelAssociationTests,
} from '#internals/testing/form-testbed.spec.js';
import { isFocused } from '#internals/testing/helpers.spec.js';
import {
simulateClick,
simulateInput,
simulateKeyboard,
simulateScroll,
} from '#internals/testing/simulate.spec.js';
import {
runValidationContainerTests,
type ValidationContainerTestsParams,
ValidityHelpers,
} from '#internals/testing/validity-helpers.spec.js';
import { configureTheme } from '#theming/config.js';
import type IgcInputComponent from '../input/input.js';
import type IgcSelectComponent from '../select/select.js';
import IgcColorPickerComponent from './color-picker.js';
import { ColorModel } from './model.js';
import type IgcPickerCanvasComponent from './picker-canvas.js';
async function createDefaultColorPicker() {
return await fixture<IgcColorPickerComponent>(
html`<igc-color-picker label="Choose a color"></igc-color-picker>`
);
}
function getAnchor(picker: IgcColorPickerComponent): HTMLElement {
return picker.renderRoot.querySelector('[part~="anchor"]')!;
}
/**
* The native input inside the input mode anchor - the element assistive
* technology lands on and reports, and the target of the ARIA projection.
*/
function getAnchorNativeInput(host: HTMLElement): HTMLInputElement {
return (host as IgcColorPickerComponent).renderRoot
.querySelector<IgcInputComponent>('igc-input[slot="anchor"]')!
.renderRoot.querySelector('input')!;
}
function isAnchorEmpty(picker: IgcColorPickerComponent): boolean {
return getAnchor(picker).part.contains('empty');
}
function getColorInput(picker: IgcColorPickerComponent): IgcInputComponent {
return picker.renderRoot.querySelector<IgcInputComponent>('#color-input')!;
}
function commitColorInput(input: IgcInputComponent, value: string): void {
input.value = value;
input.dispatchEvent(
new CustomEvent('igcChange', {
detail: value,
bubbles: true,
composed: true,
})
);
}
function getHueSlider(picker: IgcColorPickerComponent): HTMLInputElement {
return picker.renderRoot.querySelector('[part="hue"]')!;
}
function getAlphaSlider(picker: IgcColorPickerComponent): HTMLInputElement {
return picker.renderRoot.querySelector('[part="alpha"]')!;
}
function getAlphaInput(picker: IgcColorPickerComponent): IgcInputComponent {
return picker.renderRoot.querySelector('#alpha')!;
}
function getAlphaEditor(picker: IgcColorPickerComponent): HTMLInputElement {
return getAlphaInput(picker).renderRoot.querySelector('input')!;
}
/** Replaces the alpha field's current selection with `text`, as the browser would. */
function typeIntoAlpha(picker: IgcColorPickerComponent, text: string): void {
const editor = getAlphaEditor(picker);
const start = editor.selectionStart ?? editor.value.length;
const end = editor.selectionEnd ?? start;
// The caret has to be in place before the event, so the value is spliced here
// rather than handed to the helper.
editor.value = `${editor.value.slice(0, start)}${text}${editor.value.slice(end)}`;
editor.setSelectionRange(start + text.length, start + text.length);
simulateInput(editor, {
skipValueProperty: true,
inputType: 'insertText',
data: text,
bubbles: true,
composed: true,
});
}
/** Selects the whole alpha field and deletes it, as the browser would. */
function clearAlpha(picker: IgcColorPickerComponent): void {
simulateInput(getAlphaEditor(picker), { bubbles: true, composed: true });
}
/**
* Dispatches a cancelable `key` press on `node` and hands the event back, which
* `simulateKeyboard` does neither of.
*/
function press(node: Element, key: string): KeyboardEvent {
const event = new KeyboardEvent('keydown', {
key,
bubbles: true,
composed: true,
cancelable: true,
});
node.dispatchEvent(event);
return event;
}
function getCanvas(picker: IgcColorPickerComponent): IgcPickerCanvasComponent {
return picker.renderRoot.querySelector('igc-picker-canvas')!;
}
function getFormatSelect(picker: IgcColorPickerComponent): IgcSelectComponent {
return picker.renderRoot.querySelector('#format-select')!;
}
/**
* The two halves of the anchor swatch preview - the opaque color on the left
* and the color with its real alpha across the whole surface.
*/
function getAnchorPreview(picker: IgcColorPickerComponent): {
opaque: string;
alpha: string;
} {
const { style } = getAnchor(picker);
return {
opaque: style.getPropertyValue('--_color-preview'),
alpha: style.getPropertyValue('--_alpha-preview'),
};
}
describe('Color picker', () => {
before(() => defineComponents(IgcColorPickerComponent));
let picker: IgcColorPickerComponent;
describe('Default', () => {
beforeEach(async () => {
picker = await createDefaultColorPicker();
});
it('is initialized', () => {
expect(picker).to.exist;
});
it('is accessible (close state)', async () => {
await expect(picker).shadowDom.to.be.accessible();
await expect(picker).lightDom.to.be.accessible();
});
it('is accessible (open state)', async () => {
picker.open = true;
await elementUpdated(picker);
await expect(picker).shadowDom.to.be.accessible();
await expect(picker).lightDom.to.be.accessible();
});
it('is accessible (alpha row)', async () => {
picker.open = true;
picker.showAlpha = true;
await elementUpdated(picker);
await expect(picker).shadowDom.to.be.accessible();
await expect(picker).lightDom.to.be.accessible();
});
});
describe('ARIA', () => {
beforeEach(async () => {
picker = await createDefaultColorPicker();
});
it('exposes the popover as a named dialog', async () => {
picker.open = true;
await elementUpdated(picker);
const dialog = picker.renderRoot.querySelector('[part="picker"]')!;
expect(dialog.getAttribute('role')).to.equal('dialog');
expect(dialog.getAttribute('aria-label')).to.equal('Color picker');
expect(dialog.id).to.equal('picker');
});
it('points the anchor at the dialog it controls', () => {
const anchor = getAnchor(picker);
expect(anchor.getAttribute('aria-haspopup')).to.equal('dialog');
expect(anchor.getAttribute('aria-controls')).to.equal('picker');
});
it('reflects the open state through `aria-expanded`', async () => {
expect(getAnchor(picker).getAttribute('aria-expanded')).to.equal('false');
picker.open = true;
await elementUpdated(picker);
expect(getAnchor(picker).getAttribute('aria-expanded')).to.equal('true');
picker.open = false;
await elementUpdated(picker);
expect(getAnchor(picker).getAttribute('aria-expanded')).to.equal('false');
});
it('feeds the canvas marker the current saturation and value', async () => {
picker.open = true;
picker.value = 'hsl(120 100% 25%)';
await elementUpdated(picker);
const canvas = getCanvas(picker);
expect(canvas.saturation).to.be.closeTo(100, 0.5);
expect(canvas.brightness).to.be.closeTo(50, 0.5);
});
it('paints the canvas from the hue the slider points at when there is no value', async () => {
picker.open = true;
await elementUpdated(picker);
// Both are unset until the color changes if they are only written from
// the color handlers, leaving the plane on the stylesheet fallback.
expect(picker.style.getPropertyValue('--_current-color')).to.equal(
'hsl(0 100% 50%)'
);
expect(getCanvas(picker).currentColor).to.equal('hsl(0 100% 50%)');
expect(getHueSlider(picker).value).to.equal('0');
});
it('starts the alpha ramp and the canvas marker at white when there is no value', async () => {
picker.open = true;
await elementUpdated(picker);
// An empty color is white, so both start there rather than falling back
// to the pure hue the plane is drawn from.
expect(picker.style.getPropertyValue('--_selected-color')).to.equal(
'rgb(255 255 255)'
);
expect(getCanvas(picker).markerColor).to.equal('rgb(255 255 255)');
});
it('starts the canvas marker at the white corner when there is no value', async () => {
picker.open = true;
await elementUpdated(picker);
// The marker is positioned from a `requestAnimationFrame` after paint.
await nextFrame();
const canvas = getCanvas(picker);
const { width, height } = canvas.getMarkerDimensions();
expect(canvas.saturation).to.equal(0);
expect(canvas.brightness).to.equal(100);
// Top left of the saturation/value plane - the marker straddles the
// corner, so it sits at minus half its own size on both axes.
expect(canvas.x).to.equal(-width);
expect(canvas.y).to.equal(-height);
});
it('keeps the alpha field a labelled text field', async () => {
picker.open = true;
picker.showAlpha = true;
picker.value = 'rgb(255 0 0 / 0.5)';
await elementUpdated(picker);
await nextFrame();
// Deliberately not a `spinbutton`, despite the bounded value and the
// arrow-key stepping: overriding the native textbox role breaks how
// screen readers announce typing, which is the field's primary use. The
// alpha slider beside it carries the natively announced range semantics.
const editor = getAlphaEditor(picker);
const label = picker.renderRoot.querySelector('label[for="alpha"]')!;
expect(editor.role).to.be.null;
expect(editor.getAttribute('aria-valuenow')).to.be.null;
// `<label for="alpha">` labels the `igc-input`; the native editor picks
// it up by element reflection, since an IDREF cannot cross into its
// shadow root.
expect(editor.ariaLabelledByElements).to.eql([label]);
});
});
describe('Scroll strategy', () => {
let container: HTMLDivElement;
async function openColorPicker() {
picker.open = true;
await elementUpdated(picker);
await nextFrame();
}
beforeEach(async () => {
container = await fixture(html`
<div style="height: 1200px">
<igc-color-picker></igc-color-picker>
</div>
`);
picker = container.querySelector(IgcColorPickerComponent.tagName)!;
});
it('`scroll` behavior', async () => {
picker.scrollStrategy = 'scroll';
await openColorPicker();
await simulateScroll(container, { top: 200 });
expect(picker.open).to.be.true;
});
it('`close` behavior', async () => {
const eventSpy = spy(picker, 'emitEvent');
picker.scrollStrategy = 'close';
await openColorPicker();
await simulateScroll(container, { top: 200 });
expect(picker.open).to.be.false;
expect(eventSpy.firstCall).calledWith('igcClosing');
expect(eventSpy.lastCall).calledWith('igcClosed');
});
});
describe('API', () => {
beforeEach(async () => {
picker = await createDefaultColorPicker();
});
it('`toggle()`', async () => {
await picker.toggle();
expect(picker.open).to.be.true;
await picker.toggle();
expect(picker.open).to.be.false;
});
});
describe('Empty value', () => {
beforeEach(async () => {
picker = await createDefaultColorPicker();
});
it('has an empty value by default', () => {
expect(picker.value).to.equal('');
});
it('renders a checkered anchor when empty', async () => {
expect(isAnchorEmpty(picker)).to.be.true;
picker.value = '#ff0000';
await elementUpdated(picker);
expect(isAnchorEmpty(picker)).to.be.false;
});
it('reverts to an empty value for null/undefined/empty', async () => {
for (const value of ['', null, undefined]) {
picker.value = '#ff0000';
await elementUpdated(picker);
picker.value = value as unknown as string;
await elementUpdated(picker);
expect(picker.value).to.equal('');
expect(isAnchorEmpty(picker)).to.be.true;
}
});
});
describe('Color value input', () => {
beforeEach(async () => {
picker = await createDefaultColorPicker();
picker.value = '#ff0000';
picker.open = true;
await elementUpdated(picker);
});
it('commits a valid color', async () => {
commitColorInput(getColorInput(picker), '#00ff00');
await elementUpdated(picker);
expect(picker.value).to.equal('#00ff00');
});
it('reverts the input on an invalid color', async () => {
const input = getColorInput(picker);
commitColorInput(input, 'not-a-color');
await elementUpdated(picker);
expect(picker.value).to.equal('#ff0000');
expect(input.value).to.equal('#ff0000');
});
it('clears the value on an empty input', async () => {
const input = getColorInput(picker);
commitColorInput(input, '');
await elementUpdated(picker);
expect(picker.value).to.equal('');
expect(isAnchorEmpty(picker)).to.be.true;
});
it('hints the notation of the active format while empty', async () => {
const input = getColorInput(picker);
expect(input.placeholder).to.equal('#rrggbb');
picker.format = 'rgb';
await elementUpdated(picker);
expect(input.placeholder).to.equal('rgb(r g b)');
picker.format = 'hsl';
await elementUpdated(picker);
expect(input.placeholder).to.equal('hsl(h s% l%)');
});
});
describe('igcChange', () => {
beforeEach(async () => {
picker = await createDefaultColorPicker();
picker.value = '#ff0000';
await elementUpdated(picker);
});
it('emits when the value changed while focus was inside the component', async () => {
const eventSpy = spy(picker, 'emitEvent');
picker.dispatchEvent(new FocusEvent('focusin', { relatedTarget: null }));
picker.value = '#00ff00';
await elementUpdated(picker);
picker.dispatchEvent(new FocusEvent('focusout', { relatedTarget: null }));
expect(eventSpy).calledWith('igcChange', { detail: '#00ff00' });
});
it('does not emit when the value did not change', async () => {
const eventSpy = spy(picker, 'emitEvent');
picker.dispatchEvent(new FocusEvent('focusin', { relatedTarget: null }));
picker.dispatchEvent(new FocusEvent('focusout', { relatedTarget: null }));
expect(eventSpy).not.calledWith('igcChange');
});
it('does not emit when focus moves within the component', async () => {
const eventSpy = spy(picker, 'emitEvent');
const anchor = getAnchor(picker);
picker.dispatchEvent(new FocusEvent('focusin', { relatedTarget: null }));
picker.value = '#00ff00';
await elementUpdated(picker);
picker.dispatchEvent(
new FocusEvent('focusout', { relatedTarget: anchor })
);
expect(eventSpy).not.calledWith('igcChange');
});
it('does not emit when only the format changed', async () => {
const eventSpy = spy(picker, 'emitEvent');
picker.dispatchEvent(new FocusEvent('focusin', { relatedTarget: null }));
// The rendered value moves from `#ff0000` to `rgb(255 0 0)`, but the
// color behind it is untouched.
picker.format = 'rgb';
await elementUpdated(picker);
expect(picker.value).to.equal('rgb(255 0 0)');
picker.dispatchEvent(new FocusEvent('focusout', { relatedTarget: null }));
expect(eventSpy).not.calledWith('igcChange');
});
it('still emits when the color changed alongside the format', async () => {
const eventSpy = spy(picker, 'emitEvent');
picker.dispatchEvent(new FocusEvent('focusin', { relatedTarget: null }));
picker.format = 'rgb';
picker.value = '#00ff00';
await elementUpdated(picker);
picker.dispatchEvent(new FocusEvent('focusout', { relatedTarget: null }));
expect(eventSpy).calledWith('igcChange', { detail: 'rgb(0 255 0)' });
});
});
describe('Color channels', () => {
beforeEach(async () => {
picker = await createDefaultColorPicker();
picker.value = '#ff0000';
picker.open = true;
await elementUpdated(picker);
});
it('updates the hue via the hue slider', async () => {
const inputSpy = spy(picker, 'emitEvent');
const hue = getHueSlider(picker);
hue.value = '120';
hue.dispatchEvent(new Event('input', { bubbles: true }));
await elementUpdated(picker);
expect(picker.value).to.equal('#00ff00');
expect(inputSpy).calledWith('igcInput', { detail: '#00ff00' });
});
it('updates the alpha via the alpha slider', async () => {
picker.showAlpha = true;
picker.format = 'rgb';
await elementUpdated(picker);
const alpha = getAlphaSlider(picker);
alpha.value = '50';
alpha.dispatchEvent(new Event('input', { bubbles: true }));
await elementUpdated(picker);
expect(picker.value).to.equal('rgb(255 0 0 / 0.5)');
});
describe('Alpha input', () => {
beforeEach(async () => {
picker.showAlpha = true;
picker.format = 'rgb';
picker.value = 'rgb(255 0 0 / 0.5)';
await elementUpdated(picker);
});
it('updates the alpha via the alpha input', async () => {
clearAlpha(picker);
typeIntoAlpha(picker, '25');
await elementUpdated(picker);
expect(getAlphaEditor(picker).value).to.equal('25%');
expect(picker.value).to.equal('rgb(255 0 0 / 0.25)');
});
it('renders the alpha as a percentage', async () => {
picker.value = 'rgb(255 0 0 / 0.4)';
await elementUpdated(picker);
expect(getAlphaInput(picker).value).to.equal('40%');
expect(getAlphaSlider(picker).value).to.equal('40');
});
it('keeps the caret in front of the % suffix', async () => {
// Clicking past the text is what puts the caret behind the suffix.
const editor = getAlphaEditor(picker);
editor.setSelectionRange(3, 3);
simulateClick(editor);
expect([editor.selectionStart, editor.selectionEnd]).to.eql([2, 2]);
typeIntoAlpha(picker, '7');
await elementUpdated(picker);
// Appending a digit to 50 exceeds the range and clamps - what matters
// is that the keystroke landed in the number, not after the suffix.
expect(editor.value).to.equal('100%');
});
it('strips non-digits regardless of how the text arrived', async () => {
// A paste carries no `data`, so a `beforeinput` keystroke filter misses it.
clearAlpha(picker);
typeIntoAlpha(picker, '3a5%x');
await elementUpdated(picker);
expect(getAlphaEditor(picker).value).to.equal('35%');
expect(picker.value).to.equal('rgb(255 0 0 / 0.35)');
});
it('clamps the alpha input to 100', async () => {
clearAlpha(picker);
typeIntoAlpha(picker, '150');
await elementUpdated(picker);
expect(getAlphaEditor(picker).value).to.equal('100%');
});
it('reverts an emptied alpha input on commit', async () => {
// Emptying the field is a valid intermediate state - it has to leave
// somewhere to type into - so nothing is committed yet.
clearAlpha(picker);
await elementUpdated(picker);
expect(getAlphaEditor(picker).value).to.equal('');
expect(picker.value).to.equal('rgb(255 0 0 / 0.5)');
getAlphaInput(picker).dispatchEvent(
new CustomEvent('igcChange', {
detail: '',
bubbles: true,
composed: true,
})
);
await elementUpdated(picker);
expect(getAlphaInput(picker).value).to.equal('50%');
expect(picker.value).to.equal('rgb(255 0 0 / 0.5)');
});
it('steps the alpha with the arrow keys', async () => {
simulateKeyboard(getAlphaInput(picker), arrowUp);
await elementUpdated(picker);
expect(picker.value).to.equal('rgb(255 0 0 / 0.51)');
simulateKeyboard(getAlphaInput(picker), arrowDown, 2);
await elementUpdated(picker);
expect(picker.value).to.equal('rgb(255 0 0 / 0.49)');
});
it('leaves the caret in place while stepping', async () => {
const editor = getAlphaEditor(picker);
editor.setSelectionRange(2, 2);
simulateKeyboard(getAlphaInput(picker), arrowUp);
// Synchronously, before any re-render: a value written a frame later
// would drop the caret behind the `%` and be seen to skip.
expect(editor.value).to.equal('51%');
expect([editor.selectionStart, editor.selectionEnd]).to.eql([2, 2]);
await elementUpdated(picker);
expect([editor.selectionStart, editor.selectionEnd]).to.eql([2, 2]);
});
it('stops the caret at the % instead of moving past it', () => {
const editor = getAlphaEditor(picker);
// A synthetic key never moves a real caret, so what is asserted here is
// that the key is cancelled before the browser would have moved it -
// correcting afterwards is what makes the caret visibly skip.
editor.setSelectionRange(2, 2);
expect(press(editor, arrowRight).defaultPrevented).to.be.true;
// End is taken over wherever it starts, and lands on the limit.
editor.setSelectionRange(0, 0);
expect(press(editor, endKey).defaultPrevented).to.be.true;
expect(editor.selectionStart).to.equal(2);
// Below the limit the key is left to the browser.
editor.setSelectionRange(0, 0);
expect(press(editor, arrowRight).defaultPrevented).to.be.false;
});
it('holds at the bounds without re-emitting', async () => {
const eventSpy = spy(picker, 'emitEvent');
picker.value = 'rgb(255 0 0 / 1)';
await elementUpdated(picker);
simulateKeyboard(getAlphaInput(picker), arrowUp, 3);
await elementUpdated(picker);
expect(picker.value).to.equal('rgb(255 0 0)');
expect(eventSpy).not.calledWith('igcInput');
});
});
it('leaves arrow keys alone elsewhere while showAlpha is off', async () => {
// The alpha bindings are scoped to the alpha input, which does not exist
// here - they must not fall back to observing the whole component.
expect(press(getHueSlider(picker), arrowUp).defaultPrevented).to.be.false;
});
it('moves the hue slider when the color changes from elsewhere', async () => {
const hue = getHueSlider(picker);
// Interacting with the slider marks its value dirty, after which the
// browser stops taking the value from the content attribute.
hue.value = '120';
hue.dispatchEvent(new Event('input', { bubbles: true }));
await elementUpdated(picker);
picker.value = '#0000ff';
await elementUpdated(picker);
expect(hue.value).to.equal(String(ColorModel.parse('#0000ff').h));
});
it('moves the alpha slider when the color changes from elsewhere', async () => {
picker.showAlpha = true;
await elementUpdated(picker);
const alpha = getAlphaSlider(picker);
alpha.value = '50';
alpha.dispatchEvent(new Event('input', { bubbles: true }));
await elementUpdated(picker);
picker.value = 'rgb(255 0 0 / 0.25)';
await elementUpdated(picker);
expect(alpha.value).to.equal('25');
});
it('picks a color from the canvas using HSV saturation/value', async () => {
const canvas = getCanvas(picker);
canvas.dispatchEvent(
new CustomEvent('igcColorPicked', {
detail: { x: 50, y: 25 },
})
);
await elementUpdated(picker);
const expected = ColorModel.parse('#ff0000');
expected.setSaturationAndValue(50, 75);
expect(picker.value).to.equal(expected.asString('hex'));
// Hue is preserved by the HSV saturation/value update.
expect(expected.h).to.equal(ColorModel.parse('#ff0000').h);
});
it('fills the canvas marker with the selected color', async () => {
expect(getCanvas(picker).markerColor).to.equal('rgb(255 0 0)');
const hue = getHueSlider(picker);
hue.value = '120';
hue.dispatchEvent(new Event('input', { bubbles: true }));
await elementUpdated(picker);
expect(getCanvas(picker).markerColor).to.equal('rgb(0 255 0)');
});
it('keeps the canvas marker opaque regardless of alpha', async () => {
picker.showAlpha = true;
picker.format = 'rgb';
await elementUpdated(picker);
const alpha = getAlphaSlider(picker);
alpha.value = '50';
alpha.dispatchEvent(new Event('input', { bubbles: true }));
await elementUpdated(picker);
// The marker sits on the saturation/value plane, which has no alpha - a
// translucent fill would just read as the gradient underneath it.
expect(getCanvas(picker).markerColor).to.equal('rgb(255 0 0)');
});
it('updates the format via the format select', async () => {
const select = getFormatSelect(picker);
select.dispatchEvent(
new CustomEvent('igcChange', {
detail: { value: 'rgb' } as unknown as IgcSelectComponent,
bubbles: true,
composed: true,
})
);
await elementUpdated(picker);
expect(picker.format).to.equal('rgb');
expect(getColorInput(picker).value).to.equal('rgb(255 0 0)');
});
});
describe('Swatches', () => {
beforeEach(async () => {
picker = await createDefaultColorPicker();
picker.open = true;
await elementUpdated(picker);
});
it('does not render swatches by default', () => {
expect(picker.renderRoot.querySelector('[part="swatches"]')).to.be.null;
});
it('renders and selects a swatch', async () => {
picker.swatches = ['#ff0000', '#00ff00'];
await elementUpdated(picker);
const buttons = picker.renderRoot.querySelectorAll<HTMLButtonElement>(
'button[part="swatch"]'
);
expect(buttons.length).to.equal(2);
expect(buttons[0].ariaLabel).to.equal('#ff0000');
expect(buttons[0].dataset.color).to.equal('#ff0000');
const inputSpy = spy(picker, 'emitEvent');
buttons[1].click();
await elementUpdated(picker);
expect(picker.value).to.equal('#00ff00');
expect(inputSpy).calledWith('igcInput', { detail: '#00ff00' });
});
it('selects a swatch independently of its accessible name', async () => {
picker.swatches = ['#ff0000', '#00ff00'];
await elementUpdated(picker);
const buttons = picker.renderRoot.querySelectorAll<HTMLButtonElement>(
'button[part="swatch"]'
);
// The color is read from `data-color`, so a localized or otherwise
// overridden label must not affect which color the swatch commits.
buttons[1].ariaLabel = 'Vert';
buttons[1].click();
await elementUpdated(picker);
expect(picker.value).to.equal('#00ff00');
});
});
describe('Copy and EyeDropper', () => {
beforeEach(async () => {
picker = await createDefaultColorPicker();
picker.value = '#ff0000';
picker.open = true;
await elementUpdated(picker);
});
it('copies the value to the clipboard', async () => {
const writeText = stub(navigator.clipboard, 'writeText').resolves();
picker.renderRoot.querySelector<HTMLElement>('[part="copy"]')!.click();
expect(writeText).calledWith('#ff0000');
writeText.restore();
});
describe('unsupported', () => {
let originalEyeDropper: unknown;
beforeEach(() => {
originalEyeDropper = (globalThis as any).EyeDropper;
delete (globalThis as any).EyeDropper;
});
afterEach(() => {
(globalThis as any).EyeDropper = originalEyeDropper;
});
it('disables the eye dropper button', async () => {
picker = await createDefaultColorPicker();
const button = picker.renderRoot.querySelector('[part="eye-dropper"]')!;
expect(button.hasAttribute('disabled')).to.be.true;
});
});
describe('supported', () => {
let originalEyeDropper: unknown;
beforeEach(() => {
originalEyeDropper = (globalThis as any).EyeDropper;
(globalThis as any).EyeDropper = class {
public open() {
return Promise.resolve({ sRGBHex: '#112233' });
}
};
});
afterEach(() => {
(globalThis as any).EyeDropper = originalEyeDropper;
});
it('picks a color via the EyeDropper API', async () => {
picker = await createDefaultColorPicker();
const button = picker.renderRoot.querySelector<HTMLElement>(
'[part="eye-dropper"]'
)!;
expect(button.hasAttribute('disabled')).to.be.false;
button.click();
// Let the mocked EyeDropper's promise resolve.
await new Promise((resolve) => setTimeout(resolve));
await elementUpdated(picker);
expect(picker.value).to.equal('#112233');
});
});
});
describe('Open and close', () => {
beforeEach(async () => {
picker = await createDefaultColorPicker();
});
it('opens and closes via the anchor click', async () => {
const eventSpy = spy(picker, 'emitEvent');
const anchor = getAnchor(picker);
simulateClick(anchor);
await elementUpdated(picker);
expect(picker.open).to.be.true;
expect(eventSpy).calledWith('igcOpening');
expect(eventSpy).calledWith('igcOpened');
eventSpy.resetHistory();
simulateClick(anchor);
await elementUpdated(picker);
expect(picker.open).to.be.false;
expect(eventSpy).calledWith('igcClosing');
expect(eventSpy).calledWith('igcClosed');
});
it('closes and refocuses the anchor on Escape', async () => {
const anchor = getAnchor(picker);
picker.open = true;
await elementUpdated(picker);
simulateKeyboard(picker, escapeKey);
await elementUpdated(picker);
expect(picker.open).to.be.false;
expect(isFocused(anchor)).to.be.true;
});
it('opens with Alt+ArrowDown and closes with Alt+ArrowUp', async () => {
simulateKeyboard(picker, [altKey, arrowDown]);
await elementUpdated(picker);
expect(picker.open).to.be.true;
simulateKeyboard(picker, [altKey, arrowUp]);
await elementUpdated(picker);
expect(picker.open).to.be.false;
});
it('skips keybindings when disabled', async () => {
picker.disabled = true;
await elementUpdated(picker);
simulateKeyboard(picker, [altKey, arrowDown]);
await elementUpdated(picker);
expect(picker.open).to.be.false;
});
});
describe('Input mode', () => {
function getInputAnchor(): IgcInputComponent {
return picker.renderRoot.querySelector<IgcInputComponent>(
'igc-input[slot="anchor"]'
)!;
}
beforeEach(async () => {
picker = await fixture<IgcColorPickerComponent>(
html`<igc-color-picker
label="Choose a color"
mode="input"
></igc-color-picker>`
);
});
it('renders an input anchor instead of a button', () => {
expect(picker.renderRoot.querySelector('igc-button')).to.be.null;
expect(getInputAnchor()).to.exist;
});
it('orients the swatch preview the same way as the button anchor', async () => {
picker.showAlpha = true;
picker.value = 'rgb(255 0 0 / 0.5)';
await elementUpdated(picker);
const inputMode = getAnchorPreview(picker);
const defaultModePicker = await fixture<IgcColorPickerComponent>(
html`<igc-color-picker value="rgb(255 0 0 / 0.5)"></igc-color-picker>`
);
const defaultMode = getAnchorPreview(defaultModePicker);
// `--_color-preview` paints the opaque half and `--_alpha-preview` the
// translucent whole - transposing them inverts the swatch.
expect(inputMode.opaque).to.equal('rgb(255 0 0)');