44 beforeEach ,
55 inject ,
66 describe ,
7- async
7+ async ,
8+ fakeAsync ,
9+ tick
810} from '@angular/core/testing' ;
911import { TestComponentBuilder , ComponentFixture } from '@angular/compiler/testing' ;
1012import { MD_TABS_DIRECTIVES , MdTabGroup } from './tabs' ;
@@ -47,44 +49,80 @@ describe('MdTabGroup', () => {
4749 checkSelectedIndex ( 2 ) ;
4850 } ) ;
4951
50- it ( 'should cycle through tab focus with focusNextTab/focusPreviousTab functions' , ( ) => {
52+ it ( 'should cycle through tab focus with focusNextTab/focusPreviousTab functions' ,
53+ fakeAsync ( ( ) => {
54+ let testComponent = fixture . componentInstance ;
5155 let tabComponent = fixture . debugElement . query ( By . css ( 'md-tab-group' ) ) . componentInstance ;
56+
57+ spyOn ( testComponent , 'handleFocus' ) . and . callThrough ( ) ;
58+ fixture . detectChanges ( ) ;
59+
5260 tabComponent . focusIndex = 0 ;
5361 fixture . detectChanges ( ) ;
62+ tick ( ) ;
5463 expect ( tabComponent . focusIndex ) . toBe ( 0 ) ;
64+ expect ( testComponent . handleFocus ) . toHaveBeenCalledTimes ( 1 ) ;
65+ expect ( testComponent . focusEvent . index ) . toBe ( 0 ) ;
5566
5667 tabComponent . focusNextTab ( ) ;
5768 fixture . detectChanges ( ) ;
69+ tick ( ) ;
5870 expect ( tabComponent . focusIndex ) . toBe ( 1 ) ;
71+ expect ( testComponent . handleFocus ) . toHaveBeenCalledTimes ( 2 ) ;
72+ expect ( testComponent . focusEvent . index ) . toBe ( 1 ) ;
5973
6074 tabComponent . focusNextTab ( ) ;
6175 fixture . detectChanges ( ) ;
76+ tick ( ) ;
6277 expect ( tabComponent . focusIndex ) . toBe ( 2 ) ;
78+ expect ( testComponent . handleFocus ) . toHaveBeenCalledTimes ( 3 ) ;
79+ expect ( testComponent . focusEvent . index ) . toBe ( 2 ) ;
6380
6481 tabComponent . focusNextTab ( ) ;
6582 fixture . detectChanges ( ) ;
83+ tick ( ) ;
6684 expect ( tabComponent . focusIndex ) . toBe ( 2 ) ; // should stop at 2
85+ expect ( testComponent . handleFocus ) . toHaveBeenCalledTimes ( 3 ) ;
86+ expect ( testComponent . focusEvent . index ) . toBe ( 2 ) ;
6787
6888 tabComponent . focusPreviousTab ( ) ;
6989 fixture . detectChanges ( ) ;
90+ tick ( ) ;
7091 expect ( tabComponent . focusIndex ) . toBe ( 1 ) ;
92+ expect ( testComponent . handleFocus ) . toHaveBeenCalledTimes ( 4 ) ;
93+ expect ( testComponent . focusEvent . index ) . toBe ( 1 ) ;
7194
7295 tabComponent . focusPreviousTab ( ) ;
7396 fixture . detectChanges ( ) ;
97+ tick ( ) ;
7498 expect ( tabComponent . focusIndex ) . toBe ( 0 ) ;
99+ expect ( testComponent . handleFocus ) . toHaveBeenCalledTimes ( 5 ) ;
100+ expect ( testComponent . focusEvent . index ) . toBe ( 0 ) ;
75101
76102 tabComponent . focusPreviousTab ( ) ;
77103 fixture . detectChanges ( ) ;
104+ tick ( ) ;
78105 expect ( tabComponent . focusIndex ) . toBe ( 0 ) ; // should stop at 0
79- } ) ;
106+ expect ( testComponent . handleFocus ) . toHaveBeenCalledTimes ( 5 ) ;
107+ expect ( testComponent . focusEvent . index ) . toBe ( 0 ) ;
108+ } ) ) ;
109+
110+ it ( 'should change tabs based on selectedIndex' , fakeAsync ( ( ) => {
111+ let component = fixture . componentInstance ;
112+ let tabComponent = fixture . debugElement . query ( By . css ( 'md-tab-group' ) ) . componentInstance ;
113+
114+ spyOn ( component , 'handleSelection' ) . and . callThrough ( ) ;
80115
81- it ( 'should change tabs based on selectedIndex' , ( ) => {
82- let component = fixture . debugElement . componentInstance ;
83116 checkSelectedIndex ( 1 ) ;
84117
85- component . selectedIndex = 2 ;
118+ tabComponent . selectedIndex = 2 ;
119+
86120 checkSelectedIndex ( 2 ) ;
87- } ) ;
121+ tick ( ) ;
122+
123+ expect ( component . handleSelection ) . toHaveBeenCalledTimes ( 1 ) ;
124+ expect ( component . selectEvent . index ) . toBe ( 2 ) ;
125+ } ) ) ;
88126 } ) ;
89127
90128 describe ( 'async tabs' , ( ) => {
@@ -131,7 +169,10 @@ describe('MdTabGroup', () => {
131169@Component ( {
132170 selector : 'test-app' ,
133171 template : `
134- <md-tab-group class="tab-group" [selectedIndex]="selectedIndex">
172+ <md-tab-group class="tab-group"
173+ [selectedIndex]="selectedIndex"
174+ (focusChange)="handleFocus($event)"
175+ (selectChange)="handleSelection($event)">
135176 <md-tab>
136177 <template md-tab-label>Tab One</template>
137178 <template md-tab-content>Tab one content</template>
@@ -150,6 +191,14 @@ describe('MdTabGroup', () => {
150191} )
151192class SimpleTabsTestApp {
152193 selectedIndex : number = 1 ;
194+ focusEvent : any ;
195+ selectEvent : any ;
196+ handleFocus ( event : any ) {
197+ this . focusEvent = event ;
198+ }
199+ handleSelection ( event : any ) {
200+ this . selectEvent = event ;
201+ }
153202}
154203
155204@Component ( {
0 commit comments