Skip to content

Commit

Permalink
fix(kit): Tabs fix multiple emitting of activeItemIndexChange
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirpotekhin committed Dec 6, 2024
1 parent d3e025c commit b4ddbae
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
6 changes: 4 additions & 2 deletions projects/kit/components/tabs/tab.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {tuiTypedFromEvent} from '@taiga-ui/cdk/observables';
import {tuiInjectElement} from '@taiga-ui/cdk/utils/dom';
import {tuiIsNativeFocused} from '@taiga-ui/cdk/utils/focus';
import {TuiWithIcons} from '@taiga-ui/core/directives/icons';
import {EMPTY, filter, merge, switchMap} from 'rxjs';
import {EMPTY, filter, merge, switchMap, take} from 'rxjs';

export const TUI_TAB_ACTIVATE = 'tui-tab-activate';

Expand Down Expand Up @@ -35,7 +35,9 @@ export class TuiTab implements OnDestroy {
this.el.matches('button')
? tuiTypedFromEvent(this.el, 'click').pipe(
// Delaying execution until after all other click callbacks
switchMap(() => tuiTypedFromEvent(this.el.parentElement!, 'click')),
switchMap(() =>
tuiTypedFromEvent(this.el.parentElement!, 'click').pipe(take(1)),
),
)
: EMPTY,
)
Expand Down
33 changes: 31 additions & 2 deletions projects/kit/components/tabs/test/tabs.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@ import {ChangeDetectionStrategy, Component, Input, ViewChild} from '@angular/cor
import type {ComponentFixture} from '@angular/core/testing';
import {TestBed} from '@angular/core/testing';
import {By} from '@angular/platform-browser';
import {TuiTabs, TuiTabsDirective, TuiTabsHorizontal} from '@taiga-ui/kit';
import {
TUI_TAB_ACTIVATE,
TuiTabs,
TuiTabsDirective,
TuiTabsHorizontal,
} from '@taiga-ui/kit';

describe('Tabs', () => {
@Component({
standalone: true,
imports: [AsyncPipe, CommonModule, TuiTabs],
template: `
<tui-tabs [(activeItemIndex)]="activeItemIndex">
<tui-tabs
[(activeItemIndex)]="activeItemIndex"
(${TUI_TAB_ACTIVATE})="onTabActivate($event)"
>
<button
*ngFor="let tab of tabs"
tuiTab
Expand All @@ -34,6 +42,10 @@ describe('Tabs', () => {

@Input()
public tabs = [1, 2, 3];

public onTabActivate(_: number): void {

Check notice on line 46 in projects/kit/components/tabs/test/tabs.component.spec.ts

View check run for this annotation

codefactor.io / CodeFactor

projects/kit/components/tabs/test/tabs.component.spec.ts#L46

'_' is defined but never used. (@typescript-eslint/no-unused-vars)
//
}
}

let fixture: ComponentFixture<Test>;
Expand Down Expand Up @@ -78,6 +90,23 @@ describe('Tabs', () => {
expect(secondTab).toEqual(component.tabsDirective.activeElement);
});

it('when you click on a tab tui-tab-activate event emits once', () => {
const [firstTab, secondTab] = getTabs().map(
(tab) => tab.nativeElement as HTMLButtonElement,
);

secondTab?.click();
fixture.detectChanges();

jest.spyOn(component, 'onTabActivate');

firstTab?.click();
fixture.detectChanges();

expect(component.activeItemIndex).toBe(0);
expect(component.onTabActivate).toHaveBeenCalledTimes(1);
});

it('when a tab is active, it has the class _active', () => {
const [firstTab, secondTab] = getTabs().map(
(tab) => tab.nativeElement as HTMLButtonElement,
Expand Down

0 comments on commit b4ddbae

Please sign in to comment.