@spectrum-web-components/textfield 0.10.0 → 0.11.0

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,668 +0,0 @@
1
- import { __asyncValues } from "tslib";
2
- /*
3
- Copyright 2020 Adobe. All rights reserved.
4
- This file is licensed to you under the Apache License, Version 2.0 (the "License");
5
- you may not use this file except in compliance with the License. You may obtain a copy
6
- of the License at http://www.apache.org/licenses/LICENSE-2.0
7
-
8
- Unless required by applicable law or agreed to in writing, software distributed under
9
- the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
10
- OF ANY KIND, either express or implied. See the License for the specific language
11
- governing permissions and limitations under the License.
12
- */
13
- import '../sp-textfield.js';
14
- import { elementUpdated, expect, html, litFixture } from '@open-wc/testing';
15
- import { executeServerCommand, sendKeys } from '@web/test-runner-commands';
16
- describe('Textfield', () => {
17
- it('loads default textfield accessibly', async () => {
18
- const el = await litFixture(html `
19
- <sp-textfield label="Enter Your Name"></sp-textfield>
20
- `);
21
- await elementUpdated(el);
22
- await expect(el).to.be.accessible();
23
- });
24
- it('loads multiline textfield accessibly', async () => {
25
- const el = await litFixture(html `
26
- <sp-textfield label="Enter your name" multiline></sp-textfield>
27
- `);
28
- await elementUpdated(el);
29
- await expect(el).to.be.accessible();
30
- });
31
- it('manages tabIndex while disabled', async () => {
32
- const el = await litFixture(html `
33
- <sp-textfield placeholder="Enter Your Name"></sp-textfield>
34
- `);
35
- await elementUpdated(el);
36
- expect(el.tabIndex).to.equal(0);
37
- el.disabled = true;
38
- await elementUpdated(el);
39
- expect(el.tabIndex).to.equal(-1);
40
- el.tabIndex = 2;
41
- await elementUpdated(el);
42
- expect(el.tabIndex).to.equal(-1);
43
- el.disabled = false;
44
- await elementUpdated(el);
45
- expect(el.tabIndex).to.equal(2);
46
- });
47
- it('manages tabIndex before first render', async () => {
48
- const el = document.createElement('sp-textfield');
49
- expect(el.focusElement).to.be.null;
50
- expect(el.tabIndex).to.equal(0);
51
- el.remove();
52
- });
53
- it('loads', async () => {
54
- const testPlaceholder = 'Enter your name';
55
- const el = await litFixture(html `
56
- <sp-textfield placeholder=${testPlaceholder}></sp-textfield>
57
- `);
58
- expect(el).to.not.equal(undefined);
59
- const input = el.shadowRoot
60
- ? el.shadowRoot.querySelector('input')
61
- : null;
62
- expect(input).to.not.be.null;
63
- const placeholder = input ? input.placeholder : null;
64
- expect(placeholder).to.equal(testPlaceholder);
65
- });
66
- it('multiline', async () => {
67
- const el = await litFixture(html `
68
- <sp-textfield
69
- placeholder="Enter your name"
70
- multiline
71
- ></sp-textfield>
72
- `);
73
- expect(el).to.not.equal(undefined);
74
- const input = el.shadowRoot
75
- ? el.shadowRoot.querySelector('textarea')
76
- : null;
77
- expect(input).to.not.be.null;
78
- });
79
- it('resizes by default', async () => {
80
- const el = await litFixture(html `
81
- <sp-textfield
82
- multiline
83
- label="No resize control"
84
- placeholder="No resize control"
85
- ></sp-textfield>
86
- `);
87
- const startBounds = el.getBoundingClientRect();
88
- await executeServerCommand('send-mouse', {
89
- steps: [
90
- {
91
- type: 'move',
92
- position: [startBounds.right - 2, startBounds.bottom - 2],
93
- },
94
- {
95
- type: 'down',
96
- },
97
- {
98
- type: 'move',
99
- position: [startBounds.right + 50, startBounds.bottom + 50],
100
- },
101
- {
102
- type: 'up',
103
- },
104
- ],
105
- });
106
- const endBounds = el.getBoundingClientRect();
107
- expect(endBounds.width).to.be.greaterThan(startBounds.width);
108
- expect(endBounds.height).to.be.greaterThan(startBounds.height);
109
- });
110
- it('accepts resize styling', async () => {
111
- const el = await litFixture(html `
112
- <sp-textfield
113
- multiline
114
- style="resize: none;"
115
- label="No resize control"
116
- placeholder="No resize control"
117
- ></sp-textfield>
118
- `);
119
- const startBounds = el.getBoundingClientRect();
120
- await executeServerCommand('send-mouse', {
121
- steps: [
122
- {
123
- type: 'move',
124
- position: [startBounds.right - 2, startBounds.bottom - 2],
125
- },
126
- {
127
- type: 'down',
128
- },
129
- {
130
- type: 'move',
131
- position: [startBounds.right + 50, startBounds.bottom + 50],
132
- },
133
- {
134
- type: 'up',
135
- },
136
- ],
137
- });
138
- const endBounds = el.getBoundingClientRect();
139
- expect(endBounds.width).equals(startBounds.width);
140
- expect(endBounds.height).equals(startBounds.height);
141
- });
142
- it('grows', async () => {
143
- const el = await litFixture(html `
144
- <sp-textfield
145
- placeholder="Enter your name"
146
- multiline
147
- grows
148
- ></sp-textfield>
149
- `);
150
- expect(el).to.not.equal(undefined);
151
- const sizer = el.shadowRoot
152
- ? el.shadowRoot.querySelector('#sizer')
153
- : null;
154
- expect(sizer).to.not.be.null;
155
- });
156
- it('valid', async () => {
157
- const el = await litFixture(html `
158
- <sp-textfield
159
- placeholder="Enter your number"
160
- pattern="[\\d]+"
161
- value="123"
162
- required
163
- ></sp-textfield>
164
- `);
165
- await elementUpdated(el);
166
- expect(el).to.not.equal(undefined);
167
- const input = el.shadowRoot
168
- ? el.shadowRoot.querySelector('#valid')
169
- : null;
170
- expect(input).to.not.be.null;
171
- });
172
- it('valid - multiline', async () => {
173
- const el = await litFixture(html `
174
- <sp-textfield
175
- placeholder="Enter your number"
176
- pattern="[\\d]+"
177
- value="123"
178
- required
179
- multiline
180
- ></sp-textfield>
181
- `);
182
- await elementUpdated(el);
183
- expect(el).to.not.equal(undefined);
184
- const input = el.shadowRoot
185
- ? el.shadowRoot.querySelector('#valid')
186
- : null;
187
- expect(input).to.not.be.null;
188
- });
189
- it('valid - required', async () => {
190
- const el = await litFixture(html `
191
- <sp-textfield
192
- placeholder="Enter your number"
193
- value="123"
194
- required
195
- ></sp-textfield>
196
- `);
197
- await elementUpdated(el);
198
- expect(el).to.not.equal(undefined);
199
- const input = el.shadowRoot
200
- ? el.shadowRoot.querySelector('#valid')
201
- : null;
202
- expect(input).to.not.be.null;
203
- });
204
- it('valid - multiline - required', async () => {
205
- const el = await litFixture(html `
206
- <sp-textfield
207
- placeholder="Enter your number"
208
- value="123"
209
- required
210
- multiline
211
- ></sp-textfield>
212
- `);
213
- await elementUpdated(el);
214
- expect(el).to.not.equal(undefined);
215
- const input = el.shadowRoot
216
- ? el.shadowRoot.querySelector('#valid')
217
- : null;
218
- expect(input).to.not.be.null;
219
- });
220
- it('valid - boundary-type assertions', async () => {
221
- const el = await litFixture(html `
222
- <sp-textfield
223
- placeholder="Enter your number"
224
- pattern="^[\\d]+$"
225
- value="123"
226
- required
227
- ></sp-textfield>
228
- `);
229
- await elementUpdated(el);
230
- expect(el).to.not.equal(undefined);
231
- const input = el.shadowRoot
232
- ? el.shadowRoot.querySelector('#valid')
233
- : null;
234
- expect(input).to.not.be.null;
235
- });
236
- it('valid - multiline - boundary-type assertions', async () => {
237
- const el = await litFixture(html `
238
- <sp-textfield
239
- placeholder="Enter your number"
240
- pattern="^[\\d]+$"
241
- value="123"
242
- required
243
- multiline
244
- ></sp-textfield>
245
- `);
246
- await elementUpdated(el);
247
- expect(el).to.not.equal(undefined);
248
- const input = el.shadowRoot
249
- ? el.shadowRoot.querySelector('#valid')
250
- : null;
251
- expect(input).to.not.be.null;
252
- });
253
- it('valid - unicode', async () => {
254
- const el = await litFixture(html `
255
- <sp-textfield
256
- placeholder="Enter your name"
257
- pattern="\\p{L}{4,8}"
258
- value="你的名字"
259
- required
260
- ></sp-textfield>
261
- `);
262
- await elementUpdated(el);
263
- expect(el).to.not.equal(undefined);
264
- const input = el.shadowRoot
265
- ? el.shadowRoot.querySelector('#valid')
266
- : null;
267
- expect(input).to.not.be.null;
268
- });
269
- it('valid - multiline - unicode', async () => {
270
- const el = await litFixture(html `
271
- <sp-textfield
272
- placeholder="Enter your name"
273
- pattern="\\p{L}{4,8}"
274
- value="你的名字"
275
- required
276
- multiline
277
- ></sp-textfield>
278
- `);
279
- await elementUpdated(el);
280
- expect(el).to.not.equal(undefined);
281
- const input = el.shadowRoot
282
- ? el.shadowRoot.querySelector('#valid')
283
- : null;
284
- expect(input).to.not.be.null;
285
- });
286
- it('invalid', async () => {
287
- const el = await litFixture(html `
288
- <sp-textfield
289
- placeholder="Enter your number"
290
- pattern="[\\d]+"
291
- value="123 not valid"
292
- required
293
- ></sp-textfield>
294
- `);
295
- await elementUpdated(el);
296
- expect(el).to.not.equal(undefined);
297
- const input = el.shadowRoot
298
- ? el.shadowRoot.querySelector('#invalid')
299
- : null;
300
- expect(input).to.not.be.null;
301
- });
302
- it('invalid - multiline', async () => {
303
- const el = await litFixture(html `
304
- <sp-textfield
305
- placeholder="Enter your number"
306
- pattern="[\\d]+"
307
- value="123 not valid"
308
- required
309
- multiline
310
- ></sp-textfield>
311
- `);
312
- await elementUpdated(el);
313
- expect(el).to.not.equal(undefined);
314
- const input = el.shadowRoot
315
- ? el.shadowRoot.querySelector('#invalid')
316
- : null;
317
- expect(input).to.not.be.null;
318
- });
319
- it('invalid - required', async () => {
320
- const el = await litFixture(html `
321
- <sp-textfield
322
- placeholder="Enter your number"
323
- value=""
324
- required
325
- ></sp-textfield>
326
- `);
327
- await elementUpdated(el);
328
- expect(el).to.not.equal(undefined);
329
- const input = el.shadowRoot
330
- ? el.shadowRoot.querySelector('#invalid')
331
- : null;
332
- expect(input).to.not.be.null;
333
- });
334
- it('invalid - multiline - required', async () => {
335
- const el = await litFixture(html `
336
- <sp-textfield
337
- placeholder="Enter your number"
338
- value=""
339
- required
340
- multiline
341
- ></sp-textfield>
342
- `);
343
- await elementUpdated(el);
344
- expect(el).to.not.equal(undefined);
345
- const input = el.shadowRoot
346
- ? el.shadowRoot.querySelector('#invalid')
347
- : null;
348
- expect(input).to.not.be.null;
349
- });
350
- it('invalid - unicode', async () => {
351
- const el = await litFixture(html `
352
- <sp-textfield
353
- placeholder="Enter your number"
354
- pattern="\\p{N}+"
355
- value="123 not valid"
356
- required
357
- ></sp-textfield>
358
- `);
359
- await elementUpdated(el);
360
- expect(el).to.not.equal(undefined);
361
- const input = el.shadowRoot
362
- ? el.shadowRoot.querySelector('#invalid')
363
- : null;
364
- expect(input).to.not.be.null;
365
- });
366
- it('invalid - multiline - unicode', async () => {
367
- const el = await litFixture(html `
368
- <sp-textfield
369
- placeholder="Enter your number"
370
- pattern="\\p{N}+"
371
- value="123 not valid"
372
- required
373
- multiline
374
- ></sp-textfield>
375
- `);
376
- await elementUpdated(el);
377
- expect(el).to.not.equal(undefined);
378
- const input = el.shadowRoot
379
- ? el.shadowRoot.querySelector('#invalid')
380
- : null;
381
- expect(input).to.not.be.null;
382
- });
383
- it('invalid - boundary-type assertions', async () => {
384
- const el = await litFixture(html `
385
- <sp-textfield
386
- placeholder="Enter your number"
387
- pattern="^\\p{N}+$"
388
- value="123 not valid"
389
- required
390
- ></sp-textfield>
391
- `);
392
- await elementUpdated(el);
393
- expect(el).to.not.equal(undefined);
394
- const input = el.shadowRoot
395
- ? el.shadowRoot.querySelector('#invalid')
396
- : null;
397
- expect(input).to.not.be.null;
398
- });
399
- it('invalid - multiline - boundary-type assertions', async () => {
400
- const el = await litFixture(html `
401
- <sp-textfield
402
- placeholder="Enter your number"
403
- pattern="^\\p{N}+$"
404
- value="123 not valid"
405
- required
406
- multiline
407
- ></sp-textfield>
408
- `);
409
- await elementUpdated(el);
410
- expect(el).to.not.equal(undefined);
411
- const input = el.shadowRoot
412
- ? el.shadowRoot.querySelector('#invalid')
413
- : null;
414
- expect(input).to.not.be.null;
415
- });
416
- it('receives focus', async () => {
417
- let activeElement = null;
418
- const onFocusIn = (event) => {
419
- const path = event.composedPath();
420
- activeElement = path[0];
421
- };
422
- document.addEventListener('focusin', onFocusIn);
423
- const el = await litFixture(html `
424
- <sp-textfield placeholder="Enter your name"></sp-textfield>
425
- `);
426
- await elementUpdated(el);
427
- el.focus();
428
- await elementUpdated(el);
429
- expect(activeElement === el.focusElement).to.be.true;
430
- document.removeEventListener('focusin', onFocusIn);
431
- });
432
- it('does not receive focus when disabled', async () => {
433
- let activeElement = null;
434
- const onFocusIn = (event) => {
435
- const path = event.composedPath();
436
- activeElement = path[0];
437
- };
438
- document.addEventListener('focusin', onFocusIn);
439
- const el = await litFixture(html `
440
- <sp-textfield
441
- disabled
442
- placeholder="Enter your name"
443
- ></sp-textfield>
444
- `);
445
- await elementUpdated(el);
446
- el.focus();
447
- await elementUpdated(el);
448
- expect(activeElement === el.focusElement).to.be.false;
449
- expect(document.activeElement === el).to.be.false;
450
- document.removeEventListener('focusin', onFocusIn);
451
- el.click();
452
- await elementUpdated(el);
453
- expect(activeElement === el.focusElement).to.be.false;
454
- expect(document.activeElement === el).to.be.false;
455
- document.removeEventListener('focusin', onFocusIn);
456
- });
457
- it('accepts input', async () => {
458
- const testValue = 'Test Name';
459
- const el = await litFixture(html `
460
- <sp-textfield placeholder="Enter your name"></sp-textfield>
461
- `);
462
- await elementUpdated(el);
463
- el.focusElement.value = testValue;
464
- el.focusElement.dispatchEvent(new Event('input'));
465
- expect(el.value).to.equal(testValue);
466
- });
467
- it('selects', async () => {
468
- const testValue = 'Test Name';
469
- const el = await litFixture(html `
470
- <sp-textfield value=${testValue}></sp-textfield>
471
- `);
472
- await elementUpdated(el);
473
- expect(el.value).to.equal(testValue);
474
- el.focus();
475
- el.select();
476
- await sendKeys({ press: 'Backspace' });
477
- expect(el.value).to.equal('');
478
- });
479
- it('accepts maxlength', async () => {
480
- const el = await litFixture(html `
481
- <sp-textfield
482
- placeholder="Enter your name"
483
- maxlength="3"
484
- minlength="2"
485
- required
486
- ></sp-textfield>
487
- `);
488
- await elementUpdated(el);
489
- el.focus();
490
- await sendKeys({
491
- type: 'a',
492
- });
493
- await elementUpdated(el);
494
- expect(el.value).to.equal('a');
495
- expect(el.checkValidity()).to.be.false;
496
- await sendKeys({
497
- type: 'b',
498
- });
499
- await elementUpdated(el);
500
- expect(el.value).to.equal('ab');
501
- expect(el.checkValidity());
502
- await sendKeys({
503
- type: 'c',
504
- });
505
- await elementUpdated(el);
506
- expect(el.value).to.equal('abc');
507
- expect(el.checkValidity());
508
- await sendKeys({
509
- type: 'd',
510
- });
511
- await elementUpdated(el);
512
- expect(el.value).to.equal('abc');
513
- expect(el.checkValidity());
514
- await sendKeys({
515
- press: 'Backspace',
516
- });
517
- await elementUpdated(el);
518
- expect(el.value).to.equal('ab');
519
- expect(el.checkValidity());
520
- await sendKeys({
521
- press: 'Backspace',
522
- });
523
- await elementUpdated(el);
524
- expect(el.value).to.equal('a');
525
- expect(el.checkValidity()).to.be.false;
526
- });
527
- it('dispatches a `change` event', async () => {
528
- const testValue = 'Test Name';
529
- let eventSource = null;
530
- const onChange = (event) => {
531
- eventSource = event.composedPath()[0];
532
- };
533
- const el = await litFixture(html `
534
- <sp-textfield
535
- placeholder="Enter your name"
536
- @change=${onChange}
537
- ></sp-textfield>
538
- `);
539
- await elementUpdated(el);
540
- el.focusElement.value = testValue;
541
- el.focusElement.dispatchEvent(new Event('input'));
542
- el.focusElement.dispatchEvent(new Event('change'));
543
- expect(el.value).to.equal(testValue);
544
- const testSource = eventSource;
545
- expect(testSource).to.equal(el);
546
- });
547
- it('passes through `autocomplete` attribute', async () => {
548
- let el = await litFixture(html `
549
- <sp-textfield autocomplete="off"></sp-textfield>
550
- `);
551
- await elementUpdated(el);
552
- let input = el.shadowRoot ? el.shadowRoot.querySelector('input') : null;
553
- expect(input).to.exist;
554
- if (input) {
555
- expect(input.getAttribute('autocomplete')).to.equal('off');
556
- }
557
- el = await litFixture(html `
558
- <sp-textfield></sp-textfield>
559
- `);
560
- await elementUpdated(el);
561
- input = el.shadowRoot ? el.shadowRoot.querySelector('input') : null;
562
- expect(input).to.exist;
563
- if (input) {
564
- expect(input.getAttribute('autocomplete')).to.not.exist;
565
- }
566
- });
567
- it('tests on `required` changes', async () => {
568
- const el = await litFixture(html `
569
- <sp-textfield value=""></sp-textfield>
570
- `);
571
- await elementUpdated(el);
572
- expect(el.invalid).to.be.false;
573
- el.required = true;
574
- await elementUpdated(el);
575
- expect(el.invalid).to.be.true;
576
- });
577
- it('manages `allowed-keys`', async () => {
578
- const el = await litFixture(html `
579
- <sp-textfield allowed-keys="asdf"></sp-textfield>
580
- `);
581
- await elementUpdated(el);
582
- expect(el.value).to.equal('');
583
- const inputElement = el.focusElement;
584
- el.focusElement.value = 'asdf';
585
- inputElement.dispatchEvent(new InputEvent('input'));
586
- await elementUpdated(el);
587
- expect(el.value).to.equal('asdf');
588
- inputElement.value = `asdff`;
589
- inputElement.setSelectionRange(1, 1);
590
- inputElement.dispatchEvent(new InputEvent('input'));
591
- await elementUpdated(el);
592
- expect(el.value).to.equal('asdff');
593
- expect(inputElement.selectionStart).to.equal(1);
594
- inputElement.value = `asdoff`;
595
- inputElement.setSelectionRange(4, 4);
596
- inputElement.dispatchEvent(new InputEvent('input'));
597
- await elementUpdated(el);
598
- expect(el.value).to.equal('asdff');
599
- expect(inputElement.selectionStart).to.equal(3);
600
- });
601
- describe('type attribute', () => {
602
- // references:
603
- // https://developer.mozilla.org/en-US/docs/Glossary/IDL#content_versus_idl_attributes
604
- // https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#reflecting-content-attributes-in-idl-attributes
605
- // https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#keywords-and-enumerated-attributes
606
- it('assigns valid attributes to the property', async () => {
607
- var e_1, _a;
608
- const types = [
609
- 'text',
610
- 'url',
611
- 'tel',
612
- 'email',
613
- 'password',
614
- ];
615
- try {
616
- for (var types_1 = __asyncValues(types), types_1_1; types_1_1 = await types_1.next(), !types_1_1.done;) {
617
- const t = types_1_1.value;
618
- const el = await litFixture(html `
619
- <sp-textfield type=${t}></sp-textfield>
620
- `);
621
- expect(el.type).equals(t);
622
- el.setAttribute('type', 'url');
623
- expect(el.type).equals('url');
624
- }
625
- }
626
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
627
- finally {
628
- try {
629
- if (types_1_1 && !types_1_1.done && (_a = types_1.return)) await _a.call(types_1);
630
- }
631
- finally { if (e_1) throw e_1.error; }
632
- }
633
- });
634
- it('represents invalid and missing attributes as "text"', async () => {
635
- const el1 = await litFixture(html `
636
- <sp-textfield></sp-textfield>
637
- `);
638
- const el2 = await litFixture(html `
639
- <sp-textfield type="time"></sp-textfield>
640
- `);
641
- expect(el1.type).equals('text');
642
- expect(el2.type).equals('text');
643
- el1.setAttribute('type', 'submit');
644
- expect(el1.type).equals('text');
645
- });
646
- it('reflects valid property assignments', async () => {
647
- const el = await litFixture(html `
648
- <sp-textfield type="url"></sp-textfield>
649
- `);
650
- el.type = 'email';
651
- await elementUpdated(el);
652
- expect(el.getAttribute('type')).equals('email');
653
- expect(el.type).equals('email');
654
- });
655
- it('reflects invalid assignments but sets state to "text"', async () => {
656
- const el = await litFixture(html `
657
- <sp-textfield type="url"></sp-textfield>
658
- `);
659
- // eslint-disable-next-line
660
- // @ts-ignore
661
- el.type = 'range';
662
- await elementUpdated(el);
663
- expect(el.getAttribute('type')).equals('range');
664
- expect(el.type).equals('text');
665
- });
666
- });
667
- });
668
- //# sourceMappingURL=textfield.test.js.map