general.test.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { CLASS_ACTIVE, CLASS_PAGINATION, CLASS_PAGINATION_PAGE } from '../../../constants/classes';
  2. import { fire, init } from '../../../test';
  3. describe( 'Pagination', () => {
  4. test( 'can create pagination.', () => {
  5. const splide = init();
  6. const pagination = document.querySelector( `.${ CLASS_PAGINATION }` );
  7. const items = document.getElementsByClassName( CLASS_PAGINATION_PAGE );
  8. expect( pagination.children.length ).toBe( splide.length );
  9. expect( items.length ).toBe( splide.length );
  10. } );
  11. test( 'can create pagination according to the perPage option.', () => {
  12. const splide = init( { perPage: 3 } );
  13. const items = document.getElementsByClassName( CLASS_PAGINATION_PAGE );
  14. expect( items.length ).toBe( Math.ceil( splide.length / 3 ) );
  15. } );
  16. test( 'should not paginate if the `focus` option is available.', () => {
  17. const splide = init( { perPage: 3, focus: 'center' } );
  18. const items = document.getElementsByClassName( CLASS_PAGINATION_PAGE );
  19. expect( items.length ).toBe( splide.length );
  20. } );
  21. test( 'should omit unnecessary items when `focus` and `omitEnd` options are enabled.', () => {
  22. const splide = init( { perPage: 3, focus: 0, omitEnd: true } );
  23. const items = document.getElementsByClassName( CLASS_PAGINATION_PAGE );
  24. expect( items.length ).not.toBe( splide.length );
  25. expect( items.length ).toBe( splide.Components.Controller.getEnd() + 1 );
  26. } );
  27. test( 'can move the slider when the item is clicked.', () => {
  28. const splide = init( { speed: 0 } );
  29. const items = document.getElementsByClassName( CLASS_PAGINATION_PAGE );
  30. fire( items[ 1 ], 'click' );
  31. expect( splide.index ).toBe( 1 );
  32. fire( items[ 5 ], 'click' );
  33. expect( splide.index ).toBe( 5 );
  34. } );
  35. test( 'can move the slider to the end index when the last item is clicked.', () => {
  36. const splide = init( { perPage: 3 } );
  37. const items = document.getElementsByClassName( CLASS_PAGINATION_PAGE );
  38. fire( items[ items.length - 1 ], 'click' );
  39. expect( splide.index ).toBe( splide.Components.Controller.getEnd() );
  40. } );
  41. test( 'can update status classes by the index.', () => {
  42. const splide = init();
  43. const items = document.getElementsByClassName( CLASS_PAGINATION_PAGE );
  44. expect( items[ 0 ].classList.contains( CLASS_ACTIVE ) ).toBe( true );
  45. expect( items[ 0 ].getAttribute( 'aria-selected' ) ).toBe( 'true' );
  46. splide.go( 2 );
  47. expect( items[ 0 ].classList.contains( CLASS_ACTIVE ) ).toBe( false );
  48. expect( items[ 0 ].getAttribute( 'aria-selected' ) ).toBeNull();
  49. expect( items[ 2 ].classList.contains( CLASS_ACTIVE ) ).toBe( true );
  50. expect( items[ 2 ].getAttribute( 'aria-selected' ) ).toBe( 'true' );
  51. } );
  52. test( 'can update status classes by the page.', () => {
  53. const splide = init( { perPage: 3, speed: 0 } );
  54. const items = document.getElementsByClassName( CLASS_PAGINATION_PAGE );
  55. splide.go( 4 ); // page: 1
  56. expect( items[ 1 ].classList.contains( CLASS_ACTIVE ) ).toBe( true );
  57. expect( items[ 1 ].getAttribute( 'aria-selected' ) ).toBe( 'true' );
  58. splide.go( 7 ); // end page
  59. expect( items[ 3 ].classList.contains( CLASS_ACTIVE ) ).toBe( true );
  60. expect( items[ 3 ].getAttribute( 'aria-selected' ) ).toBe( 'true' );
  61. } );
  62. test( 'should remove the pagination on destroy.', () => {
  63. const splide = init();
  64. splide.destroy();
  65. const pagination = document.querySelector( `.${ CLASS_PAGINATION }` );
  66. expect( pagination ).toBeNull();
  67. } );
  68. } );