slide.test.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { minimum } from '../data/html';
  2. import Splide from '../../src/js/splide';
  3. import { STATUS_CLASSES } from "../../src/js/constants/classes";
  4. import { COMPLETE } from '../../src/js/components';
  5. describe( 'The "slide" type Splide', () => {
  6. let splide;
  7. const width = 800;
  8. beforeEach( () => {
  9. document.body.innerHTML = minimum;
  10. splide = new Splide( '#splide', {}, COMPLETE ).mount();
  11. const { track, list, slides } = splide.Components.Elements;
  12. // Set up the getBoundingClientRect.
  13. slides.forEach( ( slide, index ) => {
  14. slide.getBoundingClientRect = jest.fn( () => ( {
  15. right: width * index + 1,
  16. } ) );
  17. Object.defineProperty( slide, 'offsetWidth', { value: width } );
  18. Object.defineProperty( slide, 'clientWidth', { value: width } );
  19. } );
  20. track.getBoundingClientRect = jest.fn( () => ( { left: 0, right: width } ) );
  21. splide.on( 'move', newIndex => {
  22. const offset = slides.filter( ( slide, index ) => index < newIndex ).reduce( ( offset, slide ) => {
  23. offset += slide.clientWidth;
  24. return offset;
  25. }, 0 );
  26. list.getBoundingClientRect = jest.fn( () => ( { left: -offset } ) );
  27. } );
  28. } );
  29. test( 'should init index and slide attributes correctly.', () => {
  30. expect( splide.index ).toBe( 0 );
  31. const Slide = splide.Components.Elements.getSlide( splide.index );
  32. const classList = Slide.slide.classList;
  33. expect( classList.contains( STATUS_CLASSES.active ) ).toBe( true );
  34. expect( classList.contains( STATUS_CLASSES.visible ) ).toBe( true );
  35. } );
  36. test( 'should move slides and update attributes correctly.', done => {
  37. const { Track, Elements: { track, list } } = splide.Components;
  38. Object.defineProperty( track, 'clientWidth', { value: width } );
  39. expect( parseInt( Track.position ) ).toBe( 0 );
  40. splide.on( 'moved', ( newIndex, prevIndex ) => {
  41. expect( Track.position ).toBe( -width );
  42. const prevSlide = splide.Components.Elements.getSlide( prevIndex );
  43. const newSlide = splide.Components.Elements.getSlide( newIndex );
  44. const prevClasses = prevSlide.slide.classList;
  45. const newClasses = newSlide.slide.classList;
  46. expect( prevSlide.index ).toBe( 0 );
  47. expect( newSlide.index ).toBe( 1 );
  48. expect( prevClasses.contains( STATUS_CLASSES.active ) ).toBe( false );
  49. expect( prevClasses.contains( STATUS_CLASSES.visible ) ).toBe( false );
  50. expect( newClasses.contains( STATUS_CLASSES.active ) ).toBe( true );
  51. expect( newClasses.contains( STATUS_CLASSES.visible ) ).toBe( true );
  52. done();
  53. } );
  54. splide.go( '+' );
  55. list.dispatchEvent( new Event( 'transitionend' ) );
  56. } );
  57. test( 'should not rewind index.', () => {
  58. expect( splide.index ).toBe( 0 );
  59. splide.go( '-' );
  60. expect( splide.index ).toBe( 0 );
  61. } );
  62. } );