splide.test.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { minimum } from '../data/html';
  2. import Splide from '../../src/js/splide';
  3. import { DEFAULTS } from '../../src/js/constants/defaults';
  4. import { COMPLETE } from '../../src/js/components';
  5. describe( 'Splide ', () => {
  6. beforeEach( () => {
  7. document.body.innerHTML = minimum;
  8. } );
  9. test( 'should find an element with the given selector.', () => {
  10. const splide = new Splide( '#splide', {}, COMPLETE );
  11. expect( splide.root.id ).toBe( 'splide' );
  12. } );
  13. test( 'should accept an element as a root on construction.', () => {
  14. const root = document.getElementById( 'splide' );
  15. const splide = new Splide( root, {}, COMPLETE );
  16. expect( splide.root.id ).toBe( 'splide' );
  17. } );
  18. test( 'should overwrite default options with a given ones on construction.', () => {
  19. const splide = new Splide( '#splide', { perPage: 3 }, COMPLETE );
  20. expect( splide.options ).toEqual( { ...DEFAULTS, perPage: 3 } );
  21. } );
  22. test( '"is" should verify if a given type is a current one.', () => {
  23. const splide = new Splide( '#splide', { type: 'loop' }, COMPLETE ).mount();
  24. expect( splide.is( 'slide' ) ).toBe( false );
  25. expect( splide.is( 'loop' ) ).toBe( true );
  26. } );
  27. test( 'should make a root element visible after mount.', () => {
  28. const root = document.getElementById( 'splide' );
  29. root.style.visibility = 'hidden';
  30. const splide = new Splide( root, {}, COMPLETE );
  31. expect( splide.root.style.visibility ).toBe( 'hidden' );
  32. splide.mount();
  33. expect( splide.root.style.visibility ).toBe( 'visible' );
  34. } );
  35. test( 'should add a slide dynamically.', () => {
  36. const splide = new Splide( '#splide', { perMove: 1 }, COMPLETE ).mount();
  37. const slide = document.createElement( 'li' );
  38. const length = splide.length;
  39. slide.classList.add( 'splide__slide' );
  40. slide.textContent = `${ length }`;
  41. splide.add( slide );
  42. expect( splide.length ).toBe( length + 1 );
  43. expect( parseInt( splide.Components.Elements.slides[ splide.length - 1 ].textContent ) ).toBe( length );
  44. } );
  45. test( 'should remove a slide dynamically.', () => {
  46. const splide = new Splide( '#splide', {}, COMPLETE ).mount();
  47. const length = splide.length;
  48. splide.remove( 0 );
  49. expect( splide.length ).toBe( length - 1 );
  50. expect( splide.Components.Elements.slides[0].textContent ).toBe( '2' );
  51. } );
  52. } );