elements.test.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. // Set up the getBoundingClientRect.
  12. splide.Components.Elements.getSlides( true ).forEach( Slide => {
  13. Slide.slide.getBoundingClientRect = jest.fn( () => ( {
  14. right: width * ( Slide.index + 1 + splide.Components.Clones.length / 2 ),
  15. } ) );
  16. } );
  17. } );
  18. test( 'should init index and slide attributes correctly.', () => {
  19. expect( splide.index ).toBe( 0 );
  20. const Slide = splide.Components.Elements.getSlide( splide.index );
  21. const classList = Slide.slide.classList;
  22. expect( classList.contains( STATUS_CLASSES.active ) ).toBe( true );
  23. expect( classList.contains( STATUS_CLASSES.visible ) ).toBe( true );
  24. } );
  25. test( 'should move slides and update attributes correctly.', done => {
  26. const { Track, Elements: { track, list } } = splide.Components;
  27. Object.defineProperty( track, 'clientWidth', { value: width } );
  28. expect( parseInt( Track.position ) ).toBe( 0 );
  29. splide.on( 'moved', ( newIndex, prevIndex ) => {
  30. expect( Track.position ).toBe( -width );
  31. const prevSlide = splide.Components.Elements.getSlide( prevIndex );
  32. const newSlide = splide.Components.Elements.getSlide( newIndex );
  33. const prevClasses = prevSlide.slide.classList;
  34. const newClasses = newSlide.slide.classList;
  35. expect( prevSlide.index ).toBe( 0 );
  36. expect( newSlide.index ).toBe( 1 );
  37. expect( prevClasses.contains( STATUS_CLASSES.active ) ).toBe( false );
  38. expect( prevClasses.contains( STATUS_CLASSES.visible ) ).toBe( false );
  39. expect( newClasses.contains( STATUS_CLASSES.active ) ).toBe( true );
  40. expect( newClasses.contains( STATUS_CLASSES.visible ) ).toBe( true );
  41. done();
  42. } );
  43. splide.go( '+' );
  44. list.dispatchEvent( new Event( 'transitionend' ) );
  45. } );
  46. test( 'should not rewind index.', () => {
  47. expect( splide.index ).toBe( 0 );
  48. splide.go( '-' );
  49. expect( splide.index ).toBe( 0 );
  50. } );
  51. } );