general.test.ts 3.5 KB

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