|
| 1 | +import { |
| 2 | + it, |
| 3 | + expect, |
| 4 | + beforeEach, |
| 5 | + inject, |
| 6 | + describe, |
| 7 | + async |
| 8 | +} from '@angular/core/testing'; |
| 9 | +import {TestComponentBuilder, ComponentFixture} from '@angular/compiler/testing'; |
| 10 | +import {MD_TAB_GROUP_DIRECTIVES, MdTabGroup} from './tab-group'; |
| 11 | +import {Component} from '@angular/core'; |
| 12 | +import {By} from '@angular/platform-browser'; |
| 13 | + |
| 14 | +describe('MdTabGroup', () => { |
| 15 | + let builder: TestComponentBuilder; |
| 16 | + let fixture: ComponentFixture<SimpleTabsTestApp>; |
| 17 | + |
| 18 | + beforeEach(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => { |
| 19 | + builder = tcb; |
| 20 | + })); |
| 21 | + |
| 22 | + describe('basic behavior', () => { |
| 23 | + beforeEach(async(() => { |
| 24 | + builder.createAsync(SimpleTabsTestApp).then(f => { |
| 25 | + fixture = f; |
| 26 | + }); |
| 27 | + })); |
| 28 | + |
| 29 | + it('should default to the first tab', () => { |
| 30 | + checkSelectedIndex(1); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should change selected index on click', () => { |
| 34 | + let component = fixture.debugElement.componentInstance; |
| 35 | + component.selectedIndex = 0; |
| 36 | + checkSelectedIndex(0); |
| 37 | + |
| 38 | + // select the second tab |
| 39 | + let tabLabel = fixture.debugElement.query(By.css('.md-tab-label:nth-of-type(2)')); |
| 40 | + tabLabel.nativeElement.click(); |
| 41 | + checkSelectedIndex(1); |
| 42 | + |
| 43 | + // select the third tab |
| 44 | + tabLabel = fixture.debugElement.query(By.css('.md-tab-label:nth-of-type(3)')); |
| 45 | + tabLabel.nativeElement.click(); |
| 46 | + checkSelectedIndex(2); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should cycle through tab focus with focusNextTab/focusPreviousTab functions', () => { |
| 50 | + let tabComponent = fixture.debugElement.query(By.css('md-tab-group')).componentInstance; |
| 51 | + tabComponent.focusIndex = 0; |
| 52 | + fixture.detectChanges(); |
| 53 | + expect(tabComponent.focusIndex).toBe(0); |
| 54 | + |
| 55 | + tabComponent.focusNextTab(); |
| 56 | + fixture.detectChanges(); |
| 57 | + expect(tabComponent.focusIndex).toBe(1); |
| 58 | + |
| 59 | + tabComponent.focusNextTab(); |
| 60 | + fixture.detectChanges(); |
| 61 | + expect(tabComponent.focusIndex).toBe(2); |
| 62 | + |
| 63 | + tabComponent.focusNextTab(); |
| 64 | + fixture.detectChanges(); |
| 65 | + expect(tabComponent.focusIndex).toBe(2); // should stop at 2 |
| 66 | + |
| 67 | + tabComponent.focusPreviousTab(); |
| 68 | + fixture.detectChanges(); |
| 69 | + expect(tabComponent.focusIndex).toBe(1); |
| 70 | + |
| 71 | + tabComponent.focusPreviousTab(); |
| 72 | + fixture.detectChanges(); |
| 73 | + expect(tabComponent.focusIndex).toBe(0); |
| 74 | + |
| 75 | + tabComponent.focusPreviousTab(); |
| 76 | + fixture.detectChanges(); |
| 77 | + expect(tabComponent.focusIndex).toBe(0); // should stop at 0 |
| 78 | + }); |
| 79 | + |
| 80 | + it('should change tabs based on selectedIndex', () => { |
| 81 | + let component = fixture.debugElement.componentInstance; |
| 82 | + checkSelectedIndex(1); |
| 83 | + |
| 84 | + component.selectedIndex = 2; |
| 85 | + checkSelectedIndex(2); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + /** |
| 90 | + * Checks that the `selectedIndex` has been updated; checks that the label and body have the |
| 91 | + * `md-active` class |
| 92 | + */ |
| 93 | + function checkSelectedIndex(index: number) { |
| 94 | + fixture.detectChanges(); |
| 95 | + |
| 96 | + let tabComponent: MdTabGroup = fixture.debugElement |
| 97 | + .query(By.css('md-tab-group')).componentInstance; |
| 98 | + expect(tabComponent.selectedIndex).toBe(index); |
| 99 | + |
| 100 | + let tabLabelElement = fixture.debugElement |
| 101 | + .query(By.css(`.md-tab-label:nth-of-type(${index + 1})`)).nativeElement; |
| 102 | + expect(tabLabelElement.classList.contains('md-active')).toBe(true); |
| 103 | + |
| 104 | + let tabContentElement = fixture.debugElement |
| 105 | + .query(By.css(`#${tabLabelElement.id}`)).nativeElement; |
| 106 | + expect(tabContentElement.classList.contains('md-active')).toBe(true); |
| 107 | + } |
| 108 | +}); |
| 109 | + |
| 110 | +@Component({ |
| 111 | + selector: 'test-app', |
| 112 | + template: ` |
| 113 | + <md-tab-group class="tab-group" [selectedIndex]="selectedIndex"> |
| 114 | + <md-tab> |
| 115 | + <template md-tab-label>Tab One</template> |
| 116 | + <template md-tab-content>Tab one content</template> |
| 117 | + </md-tab> |
| 118 | + <md-tab> |
| 119 | + <template md-tab-label>Tab Two</template> |
| 120 | + <template md-tab-content>Tab two content</template> |
| 121 | + </md-tab> |
| 122 | + <md-tab> |
| 123 | + <template md-tab-label>Tab Three</template> |
| 124 | + <template md-tab-content>Tab three content</template> |
| 125 | + </md-tab> |
| 126 | + </md-tab-group> |
| 127 | + `, |
| 128 | + directives: [MD_TAB_GROUP_DIRECTIVES] |
| 129 | +}) |
| 130 | +class SimpleTabsTestApp { |
| 131 | + selectedIndex: number = 1; |
| 132 | +} |
0 commit comments