|
1 | 1 | import {Directionality} from '@angular/cdk/bidi'; |
2 | | -import {ENTER, COMMA} from '@angular/cdk/keycodes'; |
| 2 | +import {ENTER, COMMA, TAB} from '@angular/cdk/keycodes'; |
3 | 3 | import {PlatformModule} from '@angular/cdk/platform'; |
4 | | -import {createKeyboardEvent} from '@angular/cdk/testing'; |
| 4 | +import {createKeyboardEvent, dispatchKeyboardEvent, dispatchEvent} from '@angular/cdk/testing'; |
5 | 5 | import {Component, DebugElement, ViewChild} from '@angular/core'; |
6 | | -import {async, ComponentFixture, TestBed} from '@angular/core/testing'; |
| 6 | +import {async, ComponentFixture, TestBed, fakeAsync, tick} from '@angular/core/testing'; |
7 | 7 | import {By} from '@angular/platform-browser'; |
8 | 8 | import {NoopAnimationsModule} from '@angular/platform-browser/animations'; |
9 | 9 | import {MatFormFieldModule} from '@angular/material/form-field'; |
@@ -98,6 +98,42 @@ describe('MatChipInput', () => { |
98 | 98 | expect(chipInputDirective.disabled).toBe(true); |
99 | 99 | }); |
100 | 100 |
|
| 101 | + it('should allow focus to escape when tabbing forwards', fakeAsync(() => { |
| 102 | + const listElement: HTMLElement = fixture.nativeElement.querySelector('.mat-chip-list'); |
| 103 | + |
| 104 | + expect(listElement.getAttribute('tabindex')).toBe('0'); |
| 105 | + |
| 106 | + dispatchKeyboardEvent(inputNativeElement, 'keydown', TAB); |
| 107 | + fixture.detectChanges(); |
| 108 | + |
| 109 | + expect(listElement.getAttribute('tabindex')) |
| 110 | + .toBe('-1', 'Expected tabIndex to be set to -1 temporarily.'); |
| 111 | + |
| 112 | + tick(); |
| 113 | + fixture.detectChanges(); |
| 114 | + |
| 115 | + expect(listElement.getAttribute('tabindex')) |
| 116 | + .toBe('0', 'Expected tabIndex to be reset back to 0'); |
| 117 | + })); |
| 118 | + |
| 119 | + it('should not allow focus to escape when tabbing backwards', fakeAsync(() => { |
| 120 | + const listElement: HTMLElement = fixture.nativeElement.querySelector('.mat-chip-list'); |
| 121 | + const event = createKeyboardEvent('keydown', TAB); |
| 122 | + Object.defineProperty(event, 'shiftKey', {get: () => true}); |
| 123 | + |
| 124 | + expect(listElement.getAttribute('tabindex')).toBe('0'); |
| 125 | + |
| 126 | + dispatchEvent(inputNativeElement, event); |
| 127 | + fixture.detectChanges(); |
| 128 | + |
| 129 | + expect(listElement.getAttribute('tabindex')).toBe('0', 'Expected tabindex to remain 0'); |
| 130 | + |
| 131 | + tick(); |
| 132 | + fixture.detectChanges(); |
| 133 | + |
| 134 | + expect(listElement.getAttribute('tabindex')).toBe('0', 'Expected tabindex to remain 0'); |
| 135 | + })); |
| 136 | + |
101 | 137 | }); |
102 | 138 |
|
103 | 139 | describe('[addOnBlur]', () => { |
@@ -205,11 +241,12 @@ describe('MatChipInput', () => { |
205 | 241 | template: ` |
206 | 242 | <mat-form-field> |
207 | 243 | <mat-chip-list #chipList> |
| 244 | + <mat-chip>Hello</mat-chip> |
| 245 | + <input matInput [matChipInputFor]="chipList" |
| 246 | + [matChipInputAddOnBlur]="addOnBlur" |
| 247 | + (matChipInputTokenEnd)="add($event)" |
| 248 | + [placeholder]="placeholder" /> |
208 | 249 | </mat-chip-list> |
209 | | - <input matInput [matChipInputFor]="chipList" |
210 | | - [matChipInputAddOnBlur]="addOnBlur" |
211 | | - (matChipInputTokenEnd)="add($event)" |
212 | | - [placeholder]="placeholder" /> |
213 | 250 | </mat-form-field> |
214 | 251 | ` |
215 | 252 | }) |
|
0 commit comments