autowidth.test.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { autoWidth } from '../data/html';
  2. import Splide from '../../src/js/splide';
  3. import { COMPLETE } from '../../src/js/components';
  4. describe( 'When the autoWidth option is true, ', () => {
  5. let splide;
  6. const width = 800;
  7. beforeEach( () => {
  8. document.body.innerHTML = autoWidth;
  9. splide = new Splide( '#splide', { autoWidth: true }, COMPLETE );
  10. Object.defineProperty( splide.root.querySelector( '.splide__track' ), 'clientWidth', { value: width } );
  11. let accumulatedWidth = 0;
  12. splide.root.querySelectorAll( '.splide__slide' ).forEach( slide => {
  13. const slideWidth = parseInt( slide.style.width );
  14. Object.defineProperty( slide, 'offsetWidth', { value: slideWidth } );
  15. Object.defineProperty( slide, 'clientWidth', { value: slideWidth } );
  16. accumulatedWidth += slideWidth;
  17. const right = accumulatedWidth;
  18. slide.getBoundingClientRect = jest.fn( () => ( { right } ) );
  19. } );
  20. splide.mount();
  21. } );
  22. test( 'Splide should move slides according to their width.', done => {
  23. const { Elements, Track } = splide.Components;
  24. splide.on( 'moved', () => {
  25. const slide = Elements.getSlide( 0 ).slide;
  26. expect( Math.abs( Track.position ) ).toBe( slide.offsetWidth );
  27. done();
  28. } );
  29. splide.go( 1 );
  30. Elements.list.dispatchEvent( new Event( 'transitionend' ) );
  31. } );
  32. test( '"is-visible" class is properly toggled by the slide and viewport width.', done => {
  33. const Elements = splide.Components.Elements;
  34. Element.prototype.hasClass = function( className ) {
  35. return this.classList.contains( className );
  36. };
  37. splide.on( 'moved', () => {
  38. const slide1 = Elements.slides[1];
  39. const slide2 = Elements.slides[2]; // 300px, active
  40. const slide3 = Elements.slides[3]; // 400px
  41. const slide4 = Elements.slides[4]; // 500px
  42. expect( slide1.hasClass( 'is-visible' ) ).toBeFalsy();
  43. expect( slide2.hasClass( 'is-active' ) && slide2.classList.contains( 'is-visible' ) ).toBeTruthy();
  44. expect( slide3.hasClass( 'is-visible' ) ).toBeTruthy();
  45. expect( slide4.hasClass( 'is-visible' ) ).toBeFalsy(); // Out of the viewport.
  46. done();
  47. } );
  48. splide.go( 2 );
  49. splide.Components.Elements.list.dispatchEvent( new Event( 'transitionend' ) );
  50. } );
  51. } );