a11y.test.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { minimum } from '../data/html';
  2. import Splide from '../../src/js/splide';
  3. import { sprintf } from '../../src/js/utils/utils';
  4. import { COMPLETE } from '../../src/js/components';
  5. describe( 'The A11y', () => {
  6. beforeEach( () => {
  7. document.body.innerHTML = minimum;
  8. } );
  9. test( 'should update screen reader texts and aria labels of arrows properly.', done => {
  10. const splide = new Splide( '#splide', { arrows: true, rewind: true }, COMPLETE );
  11. splide.mount();
  12. const Arrows = splide.Components.Arrows;
  13. const srClass = splide.classes.sr;
  14. const i18n = splide.i18n;
  15. const prev = Arrows.arrows.prev;
  16. const next = Arrows.arrows.next;
  17. const prevSrt = prev.querySelector( `.${ srClass }` );
  18. const nextSrt = next.querySelector( `.${ srClass }` );
  19. expect( prevSrt ).not.toBeNull();
  20. expect( nextSrt ).not.toBeNull();
  21. expect( prev.getAttribute( 'aria-label' ) ).toBe( i18n.last );
  22. expect( prevSrt.textContent ).toBe( i18n.last );
  23. expect( next.getAttribute( 'aria-label' ) ).toBe( i18n.next );
  24. expect( nextSrt.textContent ).toBe( i18n.next );
  25. splide.on( 'moved', () => {
  26. expect( prev.getAttribute( 'aria-label' ) ).toBe( i18n.prev );
  27. expect( prevSrt.textContent ).toBe( i18n.prev );
  28. expect( next.getAttribute( 'aria-label' ) ).toBe( i18n.first );
  29. expect( nextSrt.textContent ).toBe( i18n.first );
  30. done();
  31. } );
  32. splide.go( splide.length - 1 );
  33. } );
  34. describe( 'should initialize screen reader texts and aria labels of pagination properly', () => {
  35. function confirm( splide, labelFormat ) {
  36. const items = splide.Components.Pagination.data.items;
  37. items.forEach( ( { button, page } ) => {
  38. const label = sprintf( labelFormat, page + 1 );
  39. const srt = button.querySelector( `.${ splide.classes.sr }` );
  40. expect( button.getAttribute( 'aria-label' ) ).toBe( label );
  41. expect( srt ).not.toBeNull();
  42. expect( srt.textContent ).toBe( label );
  43. } );
  44. }
  45. test( 'with "go to slide X" when perView is 1.', () => {
  46. const splide = new Splide( '#splide', { pagination: true }, COMPLETE );
  47. splide.mount();
  48. confirm( splide, splide.i18n.slideX );
  49. } );
  50. test( 'with "go to page X" when perView is not 1.', () => {
  51. const splide = new Splide( '#splide', { pagination: true, perView: 2 }, COMPLETE );
  52. splide.mount();
  53. confirm( splide, splide.i18n.pageX );
  54. } );
  55. } );
  56. } );