1
- import { async , ComponentFixture , TestBed } from '@angular/core/testing' ;
2
- import { Component , DebugElement } from '@angular/core' ;
3
- import { By } from '@angular/platform-browser' ;
4
- import { MdChipList , MdChipsModule } from './index' ;
1
+ import { async , ComponentFixture , TestBed } from '@angular/core/testing' ;
2
+ import { Component , DebugElement , QueryList } from '@angular/core' ;
3
+ import { By } from '@angular/platform-browser' ;
4
+ import { MdChip , MdChipList , MdChipsModule } from './index' ;
5
+ import { ListKeyManager } from '../core/a11y/list-key-manager' ;
5
6
6
- describe ( 'MdChip ' , ( ) => {
7
+ describe ( 'MdChipList ' , ( ) => {
7
8
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 ;
8
15
9
16
beforeEach ( async ( ( ) => {
10
17
TestBed . configureTestingModule ( {
@@ -15,39 +22,87 @@ describe('MdChip', () => {
15
22
} ) ;
16
23
17
24
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 ;
18
33
} ) ) ;
19
34
20
35
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
+ } ) ;
25
40
41
+ describe ( 'focus behaviors' , ( ) => {
26
42
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 ( ) ;
28
53
fixture . detectChanges ( ) ;
29
54
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 ) ;
34
56
} ) ;
35
57
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
+ } ) ;
38
89
} ) ;
39
90
} ) ;
91
+
40
92
} ) ;
41
93
42
94
@Component ( {
43
95
template : `
44
96
<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>
48
102
</md-chip-list>
49
103
`
50
104
} )
51
105
class StaticChipList {
52
106
name : 'Test' ;
107
+ remove : Number ;
53
108
}
0 commit comments