@spectrum-web-components/color-slider 1.0.0 → 1.0.1-color-testing

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.
@@ -13,37 +13,25 @@ import {
13
13
  testForLitDevWarnings
14
14
  } from "../../../test/testing-helpers.js";
15
15
  import "@spectrum-web-components/color-slider/sp-color-slider.js";
16
- import { TinyColor } from "@ctrl/tinycolor";
17
16
  import { sendKeys } from "@web/test-runner-commands";
18
17
  import { sendMouse } from "../../../test/plugins/browser.js";
19
18
  import { spy } from "sinon";
19
+ import { Default } from "../stories/color-slider.stories.js";
20
20
  describe("ColorSlider", () => {
21
- testForLitDevWarnings(
22
- async () => await fixture(
23
- html`
24
- <sp-color-slider></sp-color-slider>
25
- `
26
- )
27
- );
21
+ testForLitDevWarnings(async () => await fixture(Default()));
28
22
  it("loads default color-slider accessibly", async () => {
29
- const el = await fixture(
30
- html`
31
- <sp-color-slider></sp-color-slider>
32
- `
33
- );
23
+ const el = await fixture(Default());
34
24
  await elementUpdated(el);
35
25
  await expect(el).to.be.accessible();
36
26
  });
37
27
  it("manages a single tab stop", async () => {
38
- const test = await fixture(
39
- html`
40
- <div>
41
- <input type="text" id="test-input-1" />
42
- <sp-color-slider></sp-color-slider>
43
- <input type="text" id="test-input-2" />
44
- </div>
45
- `
46
- );
28
+ const test = await fixture(html`
29
+ <div>
30
+ <input type="text" id="test-input-1" />
31
+ <sp-color-slider></sp-color-slider>
32
+ <input type="text" id="test-input-2" />
33
+ </div>
34
+ `);
47
35
  const el = test.querySelector("sp-color-slider");
48
36
  const input1 = test.querySelector(
49
37
  "input:nth-of-type(1)"
@@ -83,11 +71,7 @@ describe("ColorSlider", () => {
83
71
  expect(document.activeElement).to.equal(input1);
84
72
  });
85
73
  it("manages [focused]", async () => {
86
- const el = await fixture(
87
- html`
88
- <sp-color-slider></sp-color-slider>
89
- `
90
- );
74
+ const el = await fixture(Default());
91
75
  await elementUpdated(el);
92
76
  expect(el.focused).to.be.false;
93
77
  await sendKeys({ press: "Tab" });
@@ -106,14 +90,12 @@ describe("ColorSlider", () => {
106
90
  it('dispatches input and change events in response to "Arrow*" keypresses', async () => {
107
91
  const inputSpy = spy();
108
92
  const changeSpy = spy();
109
- const el = await fixture(
110
- html`
111
- <sp-color-slider
112
- @change=${() => changeSpy()}
113
- @input=${() => inputSpy()}
114
- ></sp-color-slider>
115
- `
116
- );
93
+ const el = await fixture(html`
94
+ <sp-color-slider
95
+ @change=${() => changeSpy()}
96
+ @input=${() => inputSpy()}
97
+ ></sp-color-slider>
98
+ `);
117
99
  await elementUpdated(el);
118
100
  el.focus();
119
101
  await sendKeys({ press: "ArrowRight" });
@@ -132,14 +114,12 @@ describe("ColorSlider", () => {
132
114
  it("responds to events on the internal input element", async () => {
133
115
  const inputSpy = spy();
134
116
  const changeSpy = spy();
135
- const el = await fixture(
136
- html`
137
- <sp-color-slider
138
- @change=${() => changeSpy()}
139
- @input=${() => inputSpy()}
140
- ></sp-color-slider>
141
- `
142
- );
117
+ const el = await fixture(html`
118
+ <sp-color-slider
119
+ @change=${() => changeSpy()}
120
+ @input=${() => inputSpy()}
121
+ ></sp-color-slider>
122
+ `);
143
123
  await elementUpdated(el);
144
124
  el.focus();
145
125
  el.focusElement.dispatchEvent(
@@ -159,13 +139,11 @@ describe("ColorSlider", () => {
159
139
  expect(changeSpy.callCount).to.equal(1);
160
140
  });
161
141
  it('manages value on "Arrow*" keypresses', async () => {
162
- const el = await fixture(
163
- html`
164
- <sp-color-slider
165
- style="--spectrum-colorslider-default-length: 192px; --spectrum-colorslider-default-height: 24px; --spectrum-colorslider-default-height: 24px;"
166
- ></sp-color-slider>
167
- `
168
- );
142
+ const el = await fixture(html`
143
+ <sp-color-slider
144
+ style="--spectrum-colorslider-default-length: 192px; --spectrum-colorslider-default-height: 24px; --spectrum-colorslider-default-height: 24px;"
145
+ ></sp-color-slider>
146
+ `);
169
147
  await elementUpdated(el);
170
148
  expect(el.sliderHandlePosition).to.equal(0);
171
149
  expect(el.value).to.equal(0);
@@ -209,11 +187,9 @@ describe("ColorSlider", () => {
209
187
  expect(el.value).to.equal(0);
210
188
  });
211
189
  it('accepts "Arrow*" keypresses in dir="rtl"', async () => {
212
- const el = await fixture(
213
- html`
214
- <sp-color-slider dir="rtl"></sp-color-slider>
215
- `
216
- );
190
+ const el = await fixture(html`
191
+ <sp-color-slider dir="rtl"></sp-color-slider>
192
+ `);
217
193
  await elementUpdated(el);
218
194
  expect(el.sliderHandlePosition).to.equal(0);
219
195
  const input = el.focusElement;
@@ -253,11 +229,7 @@ describe("ColorSlider", () => {
253
229
  expect(el.value).to.equal(0);
254
230
  });
255
231
  it('accepts "Arrow*" keypresses with alteration', async () => {
256
- const el = await fixture(
257
- html`
258
- <sp-color-slider></sp-color-slider>
259
- `
260
- );
232
+ const el = await fixture(Default());
261
233
  await elementUpdated(el);
262
234
  el.focus();
263
235
  expect(el.sliderHandlePosition).to.equal(0);
@@ -300,7 +272,7 @@ describe("ColorSlider", () => {
300
272
  20 * 100 / 360,
301
273
  1e-6
302
274
  );
303
- expect(el.value).to.equal(20);
275
+ expect(el.value).to.be.approximately(20, 1e-6);
304
276
  await sendKeys({
305
277
  press: "ArrowLeft"
306
278
  });
@@ -315,15 +287,13 @@ describe("ColorSlider", () => {
315
287
  expect(el.value).to.equal(0);
316
288
  });
317
289
  it("accepts pointer events", async () => {
318
- const color = new TinyColor({ h: "0", s: "20%", l: "70%" });
319
- const el = await fixture(
320
- html`
321
- <sp-color-slider
322
- .color=${color}
323
- style="--spectrum-colorslider-default-length: 192px; --spectrum-colorslider-height: 24px;"
324
- ></sp-color-slider>
325
- `
326
- );
290
+ const color = { h: "0", s: "20%", l: "70%" };
291
+ const el = await fixture(html`
292
+ <sp-color-slider
293
+ .color=${color}
294
+ style="--spectrum-colorslider-default-length: 192px; --spectrum-colorslider-height: 24px;"
295
+ ></sp-color-slider>
296
+ `);
327
297
  await elementUpdated(el);
328
298
  const { handle } = el;
329
299
  handle.setPointerCapture = () => {
@@ -333,8 +303,12 @@ describe("ColorSlider", () => {
333
303
  return;
334
304
  };
335
305
  expect(el.sliderHandlePosition).to.equal(0);
336
- expect(el.color.s).to.be.within(0.19, 0.21);
337
- expect(el.color.l).to.be.within(0.69, 0.71);
306
+ expect(
307
+ el.color.s
308
+ ).to.be.within(0.19, 0.21);
309
+ expect(
310
+ el.color.l
311
+ ).to.be.within(0.69, 0.71);
338
312
  handle.dispatchEvent(
339
313
  new PointerEvent("pointerdown", {
340
314
  button: 1,
@@ -348,8 +322,12 @@ describe("ColorSlider", () => {
348
322
  );
349
323
  await elementUpdated(el);
350
324
  expect(el.sliderHandlePosition).to.equal(0);
351
- expect(el.color.s).to.be.within(0.19, 0.21);
352
- expect(el.color.l).to.be.within(0.69, 0.71);
325
+ expect(
326
+ el.color.s
327
+ ).to.be.within(0.19, 0.21);
328
+ expect(
329
+ el.color.l
330
+ ).to.be.within(0.69, 0.71);
353
331
  const root = el.shadowRoot ? el.shadowRoot : el;
354
332
  const gradient = root.querySelector(".gradient");
355
333
  gradient.dispatchEvent(
@@ -365,8 +343,12 @@ describe("ColorSlider", () => {
365
343
  );
366
344
  await elementUpdated(el);
367
345
  expect(el.sliderHandlePosition).to.equal(0);
368
- expect(el.color.s).to.be.within(0.19, 0.21);
369
- expect(el.color.l).to.be.within(0.69, 0.71);
346
+ expect(
347
+ el.color.s
348
+ ).to.be.within(0.19, 0.21);
349
+ expect(
350
+ el.color.l
351
+ ).to.be.within(0.69, 0.71);
370
352
  gradient.dispatchEvent(
371
353
  new PointerEvent("pointerdown", {
372
354
  pointerId: 1,
@@ -379,8 +361,12 @@ describe("ColorSlider", () => {
379
361
  );
380
362
  await elementUpdated(el);
381
363
  expect(el.sliderHandlePosition).to.equal(47.91666666666667);
382
- expect(el.color.s).to.be.within(0.19, 0.21);
383
- expect(el.color.l).to.be.within(0.69, 0.71);
364
+ expect(
365
+ el.color.s
366
+ ).to.be.within(0.19, 0.21);
367
+ expect(
368
+ el.color.l
369
+ ).to.be.within(0.69, 0.71);
384
370
  handle.dispatchEvent(
385
371
  new PointerEvent("pointermove", {
386
372
  pointerId: 1,
@@ -403,26 +389,28 @@ describe("ColorSlider", () => {
403
389
  );
404
390
  await elementUpdated(el);
405
391
  expect(el.sliderHandlePosition).to.equal(53.125);
406
- expect(el.color.s).to.be.within(0.19, 0.21);
407
- expect(el.color.l).to.be.within(0.69, 0.71);
392
+ expect(
393
+ el.color.s
394
+ ).to.be.within(0.19, 0.21);
395
+ expect(
396
+ el.color.l
397
+ ).to.be.within(0.69, 0.71);
408
398
  });
409
399
  it("can have `change` events prevented", async () => {
410
- const color = new TinyColor({ h: "0", s: "20%", l: "70%" });
411
- const el = await fixture(
412
- html`
413
- <sp-color-slider
414
- .color=${color}
415
- @change=${(event) => {
416
- event.preventDefault();
417
- }}
418
- style="
400
+ const color = { h: "0", s: "20%", l: "70%" };
401
+ const el = await fixture(html`
402
+ <sp-color-slider
403
+ .color=${color}
404
+ @change=${(event) => {
405
+ event.preventDefault();
406
+ }}
407
+ style="
419
408
  --spectrum-colorslider-default-length: 192px;
420
409
  --spectrum-colorslider-height: 24px;
421
410
  --spectrum-colorhandle-size: 5px;
422
411
  "
423
- ></sp-color-slider>
424
- `
425
- );
412
+ ></sp-color-slider>
413
+ `);
426
414
  await elementUpdated(el);
427
415
  expect(el.value).to.equal(0);
428
416
  const handle = el.shadowRoot.querySelector(
@@ -465,14 +453,12 @@ describe("ColorSlider", () => {
465
453
  expect(el.value).to.equal(0);
466
454
  });
467
455
  it("accepts pointer events while [vertical]", async () => {
468
- const el = await fixture(
469
- html`
470
- <sp-color-slider
471
- vertical
472
- style="--spectrum-colorslider-vertical-default-length: 192px; --spectrum-colorslider-vertical-width: 24px;"
473
- ></sp-color-slider>
474
- `
475
- );
456
+ const el = await fixture(html`
457
+ <sp-color-slider
458
+ vertical
459
+ style="--spectrum-colorslider-vertical-default-length: 192px; --spectrum-colorslider-vertical-width: 24px;"
460
+ ></sp-color-slider>
461
+ `);
476
462
  await elementUpdated(el);
477
463
  const { handle } = el;
478
464
  handle.setPointerCapture = () => {
@@ -521,14 +507,12 @@ describe("ColorSlider", () => {
521
507
  });
522
508
  it('accepts pointer events in dir="rtl"', async () => {
523
509
  document.documentElement.dir = "rtl";
524
- const el = await fixture(
525
- html`
526
- <sp-color-slider
527
- dir="rtl"
528
- style="--spectrum-colorslider-default-length: 192px; --spectrum-colorslider-default-height: 24px; --spectrum-colorslider-height: 24px;"
529
- ></sp-color-slider>
530
- `
531
- );
510
+ const el = await fixture(html`
511
+ <sp-color-slider
512
+ dir="rtl"
513
+ style="--spectrum-colorslider-default-length: 192px; --spectrum-colorslider-default-height: 24px; --spectrum-colorslider-height: 24px;"
514
+ ></sp-color-slider>
515
+ `);
532
516
  await elementUpdated(el);
533
517
  const { handle } = el;
534
518
  const clientWidth = document.documentElement.offsetWidth;
@@ -553,7 +537,10 @@ describe("ColorSlider", () => {
553
537
  })
554
538
  );
555
539
  await elementUpdated(el);
556
- expect(el.sliderHandlePosition).to.equal(100 - 52.083333333333336);
540
+ expect(el.sliderHandlePosition).to.be.approximately(
541
+ 100 - 52.083333333333336,
542
+ 1e-6
543
+ );
557
544
  handle.dispatchEvent(
558
545
  new PointerEvent("pointermove", {
559
546
  pointerId: 1,
@@ -579,19 +566,19 @@ describe("ColorSlider", () => {
579
566
  });
580
567
  const colorFormats = [
581
568
  //rgb
582
- { name: "RGB String", color: "rgb(204, 51, 204)" },
583
- { name: "RGB", color: { r: 204, g: 51, b: 204, a: 1 } },
569
+ { name: "RGB String", color: "rgba(204, 51, 204, 1)" },
570
+ { name: "RGBA", color: { r: 204, g: 51, b: 204, a: 1 } },
584
571
  //prgb
585
- { name: "PRGB String", color: "rgb(80%, 20%, 80%)" },
572
+ { name: "PRGB String", color: "rgba(80%, 20%, 80%,100%)" },
586
573
  { name: "PRGB", color: { r: "80%", g: "20%", b: "80%", a: 1 } },
587
574
  // hex
588
- { name: "Hex", color: "cc33cc" },
575
+ { name: "Hex", color: "#cc33cc" },
589
576
  { name: "Hex String", color: "#cc33cc" },
590
577
  // hex8
591
- { name: "Hex8", color: "cc33ccff" },
578
+ { name: "Hex8", color: "#cc33ccff" },
592
579
  { name: "Hex8 String", color: "#cc33ccff" },
593
580
  // name
594
- { name: "string", color: "red" },
581
+ { name: "string", color: "red", test: "ff0000" },
595
582
  // hsl
596
583
  { name: "HSL String", color: "hsl(300, 60%, 50%)" },
597
584
  { name: "HSL", color: { h: 300, s: 0.6000000000000001, l: 0.5, a: 1 } },
@@ -601,88 +588,59 @@ describe("ColorSlider", () => {
601
588
  ];
602
589
  colorFormats.map((format) => {
603
590
  it(`maintains \`color\` format as ${format.name}`, async () => {
604
- const el = await fixture(
605
- html`
606
- <sp-color-slider></sp-color-slider>
607
- `
608
- );
609
- el.color = format.color;
591
+ const el = await fixture(Default());
592
+ if (typeof format.color === "string") {
593
+ el.color = format.color;
594
+ } else {
595
+ el.color = { ...format.color };
596
+ }
610
597
  if (format.name.startsWith("Hex")) {
611
- expect(el.color).to.equal(format.color);
612
- } else expect(el.color).to.deep.equal(format.color);
598
+ expect(el.color, el.color.toString()).to.equal(
599
+ format.test || format.color
600
+ );
601
+ } else {
602
+ expect(
603
+ el.color,
604
+ `${JSON.stringify(el.color)} ${JSON.stringify(
605
+ format.color
606
+ )}`
607
+ ).to.deep.equal(format.test || format.color);
608
+ }
613
609
  });
614
610
  });
615
- it(`maintains \`color\` format as TinyColor`, async () => {
616
- const el = await fixture(
617
- html`
618
- <sp-color-slider></sp-color-slider>
619
- `
620
- );
621
- const color = new TinyColor("rgb(204, 51, 204)");
622
- el.color = color;
623
- expect(color.equals(el.color));
624
- });
625
611
  it(`resolves Hex3 format to Hex6 format`, async () => {
626
- const el = await fixture(
627
- html`
628
- <sp-color-slider></sp-color-slider>
629
- `
630
- );
612
+ const el = await fixture(Default());
631
613
  el.color = "0f0";
632
614
  expect(el.color).to.equal("00ff00");
633
615
  el.color = "#1e0";
634
616
  expect(el.color).to.equal("#11ee00");
635
617
  });
636
618
  it(`resolves Hex4 format to Hex8 format`, async () => {
637
- const el = await fixture(
638
- html`
639
- <sp-color-slider></sp-color-slider>
640
- `
641
- );
642
- el.color = "f3af";
643
- expect(el.color).to.equal("ff33aaff");
619
+ const el = await fixture(Default());
620
+ el.color = "#f3af";
621
+ expect(el.color).to.equal("#ff33aaff");
644
622
  el.color = "#f3af";
645
623
  expect(el.color).to.equal("#ff33aaff");
646
624
  });
647
625
  it(`maintains hue value`, async () => {
648
- const el = await fixture(
649
- html`
650
- <sp-color-slider></sp-color-slider>
651
- `
652
- );
626
+ const el = await fixture(Default());
653
627
  const hue = 300;
654
- const hsl = `hsl(${hue}, 60%, 100%)`;
628
+ const hsl = `hsl(${hue}, 60%, 99%)`;
655
629
  el.color = hsl;
656
630
  expect(el.value).to.equal(hue);
657
631
  expect(el.color).to.equal(hsl);
658
- const hsla = `hsla(${hue}, 60%, 100%, 0.9)`;
632
+ const hsla = `hsla(${hue}, 60%, 99%, 0.9)`;
659
633
  el.color = hsla;
660
634
  expect(el.value).to.equal(hue);
661
635
  expect(el.color).to.equal(hsla);
662
- const hsv = `hsv(${hue}, 60%, 100%)`;
636
+ const hsv = `hsv(${hue}, 60%, 99%)`;
663
637
  el.color = hsv;
664
638
  expect(el.value).to.equal(hue);
665
639
  expect(el.color).to.equal(hsv);
666
- const hsva = `hsva(${hue}, 60%, 100%, 0.9)`;
640
+ const hsva = `hsva(${hue}, 60%, 99%, 0.9)`;
667
641
  el.color = hsva;
668
642
  expect(el.value).to.equal(hue);
669
643
  expect(el.color).to.equal(hsva);
670
- const tinyHSV = new TinyColor({ h: hue, s: 60, v: 100 });
671
- el.color = tinyHSV;
672
- expect(el.value).to.equal(hue);
673
- expect(tinyHSV.equals(el.color)).to.be.true;
674
- const tinyHSVA = new TinyColor({ h: hue, s: 60, v: 100, a: 1 });
675
- el.color = tinyHSVA;
676
- expect(el.value).to.equal(hue);
677
- expect(tinyHSVA.equals(el.color)).to.be.true;
678
- const tinyHSL = new TinyColor({ h: hue, s: 60, l: 100 });
679
- el.color = tinyHSL;
680
- expect(el.value).to.equal(hue);
681
- expect(tinyHSL.equals(el.color)).to.be.true;
682
- const tinyHSLA = new TinyColor({ h: hue, s: 60, l: 100, a: 1 });
683
- el.color = tinyHSLA;
684
- expect(el.value).to.equal(hue);
685
- expect(tinyHSLA.equals(el.color)).to.be.true;
686
644
  });
687
645
  });
688
646
  //# sourceMappingURL=color-slider.test.js.map