-
Notifications
You must be signed in to change notification settings - Fork 399
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): active tab does not have class _active (#7355)
- Loading branch information
Showing
2 changed files
with
119 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import {AsyncPipe, CommonModule} from '@angular/common'; | ||
import type {DebugElement} from '@angular/core'; | ||
import {Component, Input, ViewChild} from '@angular/core'; | ||
import type {ComponentFixture} from '@angular/core/testing'; | ||
import {TestBed} from '@angular/core/testing'; | ||
import {By} from '@angular/platform-browser'; | ||
import { | ||
TuiTabDirective, | ||
TuiTabsDirective, | ||
TuiTabsHorizontalDirective, | ||
} from '@taiga-ui/kit'; | ||
|
||
describe('Tabs', () => { | ||
@Component({ | ||
standalone: true, | ||
imports: [AsyncPipe, CommonModule, TuiTabDirective, TuiTabsHorizontalDirective], | ||
template: ` | ||
<tui-tabs [(activeItemIndex)]="activeItemIndex"> | ||
<button | ||
*ngFor="let tab of tabs" | ||
tuiTab | ||
> | ||
{{ tab }} | ||
</button> | ||
</tui-tabs> | ||
`, | ||
}) | ||
class TestComponent { | ||
@ViewChild(TuiTabsHorizontalDirective, {static: true}) | ||
public tabsHorizontalDirective!: TuiTabsHorizontalDirective; | ||
|
||
@ViewChild(TuiTabsDirective, {static: true}) | ||
public tabsDirective!: TuiTabsDirective; | ||
|
||
@Input() | ||
public activeItemIndex = 0; | ||
|
||
@Input() | ||
public tabs = [1, 2, 3]; | ||
} | ||
|
||
let fixture: ComponentFixture<TestComponent>; | ||
let component: TestComponent; | ||
|
||
beforeEach(async () => { | ||
TestBed.configureTestingModule({ | ||
imports: [TestComponent], | ||
}); | ||
await TestBed.compileComponents(); | ||
|
||
fixture = TestBed.createComponent(TestComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
function getTabs(): DebugElement[] { | ||
return fixture.debugElement.queryAll(By.css('[tuiTab]')); | ||
} | ||
|
||
describe('Tabs component', () => { | ||
it('Should render all tabs', () => { | ||
const tabs = [1, 2, 3, 4, 5]; | ||
|
||
fixture.componentRef.setInput('tabs', tabs); | ||
fixture.detectChanges(); | ||
|
||
expect(getTabs().length).toBe(tabs.length); | ||
}); | ||
|
||
it('When you click on a tab it changes the active tab', () => { | ||
const [firstTab, secondTab] = getTabs().map( | ||
tab => tab.nativeElement as HTMLButtonElement, | ||
); | ||
|
||
expect(firstTab).toEqual(component.tabsDirective.activeElement); | ||
|
||
secondTab.click(); | ||
fixture.detectChanges(); | ||
|
||
expect(firstTab).not.toEqual(component.tabsDirective.activeElement); | ||
expect(secondTab).toEqual(component.tabsDirective.activeElement); | ||
}); | ||
|
||
it('When a tab is active, it has the class _active', () => { | ||
const [firstTab, secondTab] = getTabs().map( | ||
tab => tab.nativeElement as HTMLButtonElement, | ||
); | ||
|
||
expect(firstTab.classList.contains('_active')).toBeTruthy(); | ||
expect(secondTab.classList.contains('_active')).toBeFalsy(); | ||
|
||
secondTab.click(); | ||
fixture.detectChanges(); | ||
|
||
expect(firstTab.classList.contains('_active')).toBeFalsy(); | ||
expect(secondTab.classList.contains('_active')).toBeTruthy(); | ||
}); | ||
}); | ||
}); |