general.test.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { EVENT_DRAG, EVENT_DRAGGING } from '../../../constants/events';
  2. import { fire, init } from '../../../test';
  3. import { assign } from '../../../utils';
  4. describe( 'Drag', () => {
  5. test( 'can move the slider by drag.', () => {
  6. const splide = init();
  7. const track = splide.Components.Elements.track;
  8. fireWithCoord( track, 'mousedown', { x: 0, timeStamp: 1 } );
  9. fireWithCoord( window, 'mousemove', { x: 1, timeStamp: 1 } );
  10. fireWithCoord( window, 'mousemove', { x: -100, timeStamp: 2 } );
  11. expect( splide.Components.Move.getPosition() ).toBe( -100 );
  12. fireWithCoord( window, 'mousemove', { x: -200, timeStamp: 3 } );
  13. expect( splide.Components.Move.getPosition() ).toBe( -200 );
  14. } );
  15. test( 'should not move the slider after dragging.', () => {
  16. const splide = init();
  17. const track = splide.Components.Elements.track;
  18. fireWithCoord( track, 'mousedown', { x: 0 } );
  19. fireWithCoord( window, 'mousemove', { x: 1 } );
  20. fireWithCoord( window, 'mouseup' );
  21. expect( splide.Components.Move.getPosition() ).toBe( 0 );
  22. fireWithCoord( window, 'mousemove', { x: -200 } );
  23. fireWithCoord( window, 'mousemove', { x: -400 } );
  24. expect( splide.Components.Move.getPosition() ).toBe( 0 );
  25. } );
  26. test( 'can change the slide index if the drag velocity is enough.', () => {
  27. const splide = init( { speed: 0, width: 600 } );
  28. const track = splide.Components.Elements.track;
  29. fireWithCoord( track, 'mousedown', { x: 0, timeStamp: 1 } );
  30. fireWithCoord( window, 'mousemove', { x: 1, timeStamp: 1 } );
  31. fireWithCoord( window, 'mousemove', { x: -20, timeStamp: 21 } ); // v = -1
  32. fireWithCoord( window, 'mouseup', { x: -20, timeStamp: 21 } );
  33. // The destination will be flickPower * v + (-20) = -620
  34. expect( splide.index ).toBe( 1 );
  35. } );
  36. test( 'should not change the slide index if the drag velocity is not enough.', () => {
  37. const splide = init( { speed: 0, width: 600 } );
  38. const track = splide.Components.Elements.track;
  39. fireWithCoord( track, 'mousedown', { x: 0, timeStamp: 1 } );
  40. fireWithCoord( window, 'mousemove', { x: 1, timeStamp: 1 } );
  41. fireWithCoord( window, 'mousemove', { x: -20, timeStamp: 100 } );
  42. fireWithCoord( window, 'mouseup', { x: -20, timeStamp: 100 } );
  43. expect( splide.index ).toBe( 0 );
  44. } );
  45. test( 'should start moving the slider immediately if the pointing device is a mouse.', () => {
  46. const splide = init();
  47. const onDrag = jest.fn();
  48. const track = splide.Components.Elements.track;
  49. splide.on( EVENT_DRAG, onDrag );
  50. fire( track, 'mousedown' );
  51. fireWithCoord( window, 'mousemove' );
  52. expect( onDrag ).toHaveBeenCalledTimes( 1 );
  53. } );
  54. test( 'should start moving the slider only when the drag distance becomes greater than the threshold.', () => {
  55. const splide = init( { dragMinThreshold: 20 } );
  56. const onDragging = jest.fn();
  57. const track = splide.Components.Elements.track;
  58. splide.on( EVENT_DRAGGING, onDragging );
  59. fireWithCoord( track, 'touchstart', { x: 0 } );
  60. fireWithCoord( track, 'touchmove', { x: -10 } );
  61. expect( onDragging ).not.toHaveBeenCalled();
  62. fireWithCoord( track, 'touchmove', { x: -30 } ); // isDragging becomes true
  63. fireWithCoord( track, 'touchmove', { x: -31 } );
  64. expect( onDragging ).toHaveBeenCalled();
  65. } );
  66. } );
  67. function fireCancelable( elm: Element | Window, event: string, data: any = {} ): void {
  68. fire( elm, event, data, { cancelable: true } );
  69. }
  70. function fireWithCoord( elm: Element | Window, event: string, data: any = {} ): void {
  71. const { x: pageX = 0, y: pageY = 0 } = data;
  72. fireCancelable( elm, event, assign( data, {
  73. pageX,
  74. pageY,
  75. touches: [
  76. { pageX, pageY },
  77. ],
  78. } ) );
  79. }