11import { async , ComponentFixture , TestBed } from '@angular/core/testing' ;
2- import { Component , DebugElement } from '@angular/core' ;
2+ import { Component , DebugElement , QueryList } from '@angular/core' ;
33import { By } from '@angular/platform-browser' ;
4- import { MdChipList , MdChipsModule } from './index' ;
4+ import { MdChip , MdChipList , MdChipsModule } from './index' ;
5+ import { ListKeyManager } from '../core/a11y/list-key-manager' ;
56
6- describe ( 'MdChip ' , ( ) => {
7+ describe ( 'MdChipList ' , ( ) => {
78 let fixture : ComponentFixture < any > ;
9+ let chipListDebugElement : DebugElement ;
10+ let chipListNativeElement : HTMLElement ;
11+ let chipListInstance : MdChipList ;
12+ let testComponent : StaticChipList ;
13+ let items : QueryList < MdChip > ;
14+ let manager : ListKeyManager ;
815
916 beforeEach ( async ( ( ) => {
1017 TestBed . configureTestingModule ( {
@@ -15,39 +22,87 @@ describe('MdChip', () => {
1522 } ) ;
1623
1724 TestBed . compileComponents ( ) ;
25+
26+ fixture = TestBed . createComponent ( StaticChipList ) ;
27+ fixture . detectChanges ( ) ;
28+
29+ chipListDebugElement = fixture . debugElement . query ( By . directive ( MdChipList ) ) ;
30+ chipListNativeElement = chipListDebugElement . nativeElement ;
31+ chipListInstance = chipListDebugElement . componentInstance ;
32+ testComponent = fixture . debugElement . componentInstance ;
1833 } ) ) ;
1934
2035 describe ( 'basic behaviors' , ( ) => {
21- let chipListDebugElement : DebugElement ;
22- let chipListNativeElement : HTMLElement ;
23- let chipListInstance : MdChipList ;
24- let testComponent : StaticChipList ;
36+ it ( 'adds the `md-chip-list` class' , ( ) => {
37+ expect ( chipListNativeElement . classList ) . toContain ( 'md-chip-list' ) ;
38+ } ) ;
39+ } ) ;
2540
41+ describe ( 'focus behaviors' , ( ) => {
2642 beforeEach ( ( ) => {
27- fixture = TestBed . createComponent ( StaticChipList ) ;
43+ items = chipListInstance . chips ;
44+ manager = chipListInstance . _keyManager ;
45+ } ) ;
46+
47+ it ( 'watches for chip focus' , ( ) => {
48+ let array = items . toArray ( ) ;
49+ let lastIndex = array . length - 1 ;
50+ let lastItem = array [ lastIndex ] ;
51+
52+ lastItem . focus ( ) ;
2853 fixture . detectChanges ( ) ;
2954
30- chipListDebugElement = fixture . debugElement . query ( By . directive ( MdChipList ) ) ;
31- chipListNativeElement = chipListDebugElement . nativeElement ;
32- chipListInstance = chipListDebugElement . componentInstance ;
33- testComponent = fixture . debugElement . componentInstance ;
55+ expect ( manager . focusedItemIndex ) . toBe ( lastIndex ) ;
3456 } ) ;
3557
36- it ( 'adds the `md-chip-list` class' , ( ) => {
37- expect ( chipListNativeElement . classList ) . toContain ( 'md-chip-list' ) ;
58+ describe ( 'on chip destroy' , ( ) => {
59+ it ( 'focuses the next item' , ( ) => {
60+ let array = items . toArray ( ) ;
61+ let midItem = array [ 2 ] ;
62+
63+ // Focus the middle item
64+ midItem . focus ( ) ;
65+
66+ // Destroy the middle item
67+ testComponent . remove = 2 ;
68+ fixture . detectChanges ( ) ;
69+
70+ // It focuses the 4th item (now at index 2)
71+ expect ( manager . focusedItemIndex ) . toEqual ( 2 ) ;
72+ } ) ;
73+
74+ it ( 'focuses the previous item' , ( ) => {
75+ let array = items . toArray ( ) ;
76+ let lastIndex = array . length - 1 ;
77+ let lastItem = array [ lastIndex ] ;
78+
79+ // Focus the last item
80+ lastItem . focus ( ) ;
81+
82+ // Destroy the last item
83+ testComponent . remove = lastIndex ;
84+ fixture . detectChanges ( ) ;
85+
86+ // It focuses the next-to-last item
87+ expect ( manager . focusedItemIndex ) . toEqual ( lastIndex - 1 ) ;
88+ } ) ;
3889 } ) ;
3990 } ) ;
91+
4092} ) ;
4193
4294@Component ( {
4395 template : `
4496 <md-chip-list>
45- <md-chip>{{name}} 1</md-chip>
46- <md-chip>{{name}} 2</md-chip>
47- <md-chip>{{name}} 3</md-chip>
97+ <div *ngIf="remove != 0"><md-chip>{{name}} 1</md-chip></div>
98+ <div *ngIf="remove != 1"><md-chip>{{name}} 2</md-chip></div>
99+ <div *ngIf="remove != 2"><md-chip>{{name}} 3</md-chip></div>
100+ <div *ngIf="remove != 3"><md-chip>{{name}} 4</md-chip></div>
101+ <div *ngIf="remove != 4"><md-chip>{{name}} 5</md-chip></div>
48102 </md-chip-list>
49103 `
50104} )
51105class StaticChipList {
52106 name : 'Test' ;
107+ remove : Number ;
53108}
0 commit comments