Skip to content

Commit

Permalink
fix(module:carousel): fix nzAfterChange callback value not correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
chenc041 committed Mar 19, 2022
1 parent 2d77bc2 commit c50a5d5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
2 changes: 1 addition & 1 deletion components/carousel/carousel.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ export class NzCarouselComponent implements AfterContentInit, AfterViewInit, OnD
this.nzBeforeChange.emit({ from, to });
this.strategy!.switch(this.activeIndex, index).subscribe(() => {
this.scheduleNextTransition();
this.nzAfterChange.emit(index);
this.nzAfterChange.emit(to);
this.isTransiting = false;
});
this.markContentActive(to);
Expand Down
41 changes: 40 additions & 1 deletion components/carousel/carousel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('carousel', () => {
beforeEach(fakeAsync(() => {
TestBed.configureTestingModule({
imports: [BidiModule, NzCarouselModule],
declarations: [NzTestCarouselBasicComponent, NzTestCarouselRtlComponent]
declarations: [NzTestCarouselBasicComponent, NzTestCarouselRtlComponent, NzTestCarouselActiveIndexComponent]
});
TestBed.compileComponents();
}));
Expand Down Expand Up @@ -284,6 +284,26 @@ describe('carousel', () => {
// already covered in components specs.
// describe('opacity strategy', () => {});
});

describe('carousel nzAfterChange return value', () => {
let fixture: ComponentFixture<NzTestCarouselActiveIndexComponent>;
let testComponent: NzTestCarouselActiveIndexComponent;

beforeEach(() => {
fixture = TestBed.createComponent(NzTestCarouselActiveIndexComponent);
fixture.detectChanges();
testComponent = fixture.debugElement.componentInstance;
});

it('carousel activeIndex should be equal to nzAfterChange return value', fakeAsync(() => {
fixture.detectChanges();
[0, 1, 2, 3, 4].forEach(_ => {
testComponent.nzCarouselComponent.next();
tickMilliseconds(fixture, 700);
expect(testComponent.index).toBe(testComponent.nzCarouselComponent.activeIndex);
});
}));
});
});

describe('carousel custom strategies', () => {
Expand Down Expand Up @@ -421,3 +441,22 @@ export class NzTestCarouselRtlComponent {
@ViewChild(Dir) dir!: Dir;
direction = 'rtl';
}

@Component({
template: `
<nz-carousel (nzAfterChange)="afterChange($event)">
<div nz-carousel-content *ngFor="let index of array">
<h3>{{ index }}</h3>
</div>
</nz-carousel>
`
})
export class NzTestCarouselActiveIndexComponent {
@ViewChild(NzCarouselComponent, { static: true }) nzCarouselComponent!: NzCarouselComponent;
array = [0, 1, 2, 3, 4];
index = 0;

afterChange(index: number): void {
this.index = index;
}
}

0 comments on commit c50a5d5

Please sign in to comment.