Skip to content

Commit

Permalink
fix(carousel): fix value of aria-activedescendant attribute (#4015)
Browse files Browse the repository at this point in the history
fix #4014
  • Loading branch information
jnizet committed Mar 2, 2021
1 parent 59a1cf8 commit 35bb72a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
15 changes: 10 additions & 5 deletions src/carousel/carousel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import {NgbConfig} from '../ngb-config';
import {NgbConfigAnimation} from '../test/ngb-config-animation';
import {NgbSlideEventDirection} from './carousel-transition';

const createTestComponent = (html: string) =>
createGenericTestComponent(html, TestComponent) as ComponentFixture<TestComponent>;
const createTestComponent = (html: string, detectChanges = true) =>
createGenericTestComponent(html, TestComponent, detectChanges) as ComponentFixture<TestComponent>;

function expectActiveSlides(nativeEl: HTMLDivElement, active: boolean[]) {
const slideElms = nativeEl.querySelectorAll('.carousel-item');
const indicatorElms = nativeEl.querySelectorAll('ol.carousel-indicators > li');
const carouselElm = nativeEl.querySelector('ngb-carousel');

expect(slideElms.length).toBe(active.length);
expect(indicatorElms.length).toBe(active.length);
Expand All @@ -25,9 +26,12 @@ function expectActiveSlides(nativeEl: HTMLDivElement, active: boolean[]) {
if (active[i]) {
expect(slideElms[i]).toHaveCssClass('active');
expect(indicatorElms[i]).toHaveCssClass('active');
expect(indicatorElms[i].getAttribute('aria-selected')).toBe('true');
expect(carouselElm !.getAttribute('aria-activedescendant')).toBe(slideElms[i].id);
} else {
expect(slideElms[i]).not.toHaveCssClass('active');
expect(indicatorElms[i]).not.toHaveCssClass('active');
expect(indicatorElms[i].getAttribute('aria-selected')).toBe('false');
}
}
}
Expand Down Expand Up @@ -109,11 +113,12 @@ describe('ngb-carousel', () => {
</ngb-carousel>
`;

const fixture = createTestComponent(html);
// set the second slide active (instead of the first one by default), before the first change detection
const fixture = createTestComponent(html, false);

fixture.componentInstance.activeSlideId = '1';
fixture.componentInstance.activeSlideId = '2';
fixture.detectChanges();
expectActiveSlides(fixture.nativeElement, [true, false]);
expectActiveSlides(fixture.nativeElement, [false, true]);

discardPeriodicTasks();
}));
Expand Down
2 changes: 1 addition & 1 deletion src/carousel/carousel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class NgbSlide {
'(mouseleave)': 'mouseHover = false',
'(focusin)': 'focused = true',
'(focusout)': 'focused = false',
'[attr.aria-activedescendant]': 'activeId'
'[attr.aria-activedescendant]': `'slide-' + activeId`
},
template: `
<ol class="carousel-indicators" [class.sr-only]="!showNavigationIndicators" role="tablist">
Expand Down

0 comments on commit 35bb72a

Please sign in to comment.