autowidth.test.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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, track, slides;
  6. const width = 800;
  7. beforeEach( () => {
  8. document.body.innerHTML = autoWidth;
  9. splide = new Splide( '#splide', { autoWidth: true }, COMPLETE );
  10. track = splide.root.querySelector( '.splide__track' );
  11. slides = Object.values( splide.root.querySelectorAll( '.splide__slide' ) );
  12. Object.defineProperty( track, 'clientWidth', { value: width } );
  13. track.getBoundingClientRect = jest.fn( () => ( { left: 0, right: width } ) );
  14. slides.forEach( slide => {
  15. const slideWidth = parseInt( slide.style.width );
  16. Object.defineProperty( slide, 'offsetWidth', { value: slideWidth } );
  17. Object.defineProperty( slide, 'clientWidth', { value: slideWidth } );
  18. } );
  19. // Move slides and a list manually because jest does not render HTML and getBoundingClientRect props are always 0.
  20. splide.on( 'move', newIndex => {
  21. const offset = slides.filter( ( slide, index ) => index < newIndex ).reduce( ( offset, slide ) => {
  22. offset += slide.clientWidth;
  23. return offset;
  24. }, 0 );
  25. let accumulatedWidth = -offset;
  26. slides.forEach( slide => {
  27. const left = accumulatedWidth;
  28. accumulatedWidth += slide.clientWidth;
  29. const right = accumulatedWidth;
  30. slide.getBoundingClientRect = jest.fn( () => ( { left, right } ) );
  31. } );
  32. splide.Components.Elements.list.getBoundingClientRect = jest.fn( () => ( { left: -offset } ) );
  33. } );
  34. splide.mount();
  35. } );
  36. test( 'Splide should move slides according to their width.', done => {
  37. const { Elements, Track } = splide.Components;
  38. splide.on( 'moved', () => {
  39. const slide = Elements.getSlide( 0 ).slide;
  40. expect( Math.abs( Track.position ) ).toBe( slide.offsetWidth );
  41. done();
  42. } );
  43. splide.go( 1 );
  44. Elements.list.dispatchEvent( new Event( 'transitionend' ) );
  45. } );
  46. test( '"is-visible" class is properly toggled by the slide and viewport width.', done => {
  47. const Elements = splide.Components.Elements;
  48. Element.prototype.hasClass = function( className ) {
  49. return this.classList.contains( className );
  50. };
  51. splide.on( 'moved', () => {
  52. const slide1 = Elements.slides[1];
  53. const slide2 = Elements.slides[2]; // 300px, active
  54. const slide3 = Elements.slides[3]; // 400px
  55. const slide4 = Elements.slides[4]; // 500px
  56. expect( slide1.hasClass( 'is-visible' ) ).toBeFalsy();
  57. expect( slide2.hasClass( 'is-active' ) && slide2.classList.contains( 'is-visible' ) ).toBeTruthy();
  58. expect( slide3.hasClass( 'is-visible' ) ).toBeTruthy();
  59. expect( slide4.hasClass( 'is-visible' ) ).toBeFalsy(); // Out of the viewport.
  60. done();
  61. } );
  62. splide.go( 2 );
  63. splide.Components.Elements.list.dispatchEvent( new Event( 'transitionend' ) );
  64. } );
  65. } );