|
1 | 1 | import * as React from "react";
|
2 | 2 | import { render, fireEvent, act } from "@testing-library/react";
|
3 | 3 | import { useSwipeable } from "../src/index";
|
4 |
| -import { LEFT, RIGHT, UP, DOWN } from "../src/types"; |
| 4 | +import { LEFT, RIGHT, UP, DOWN, SwipeableProps } from "../src/types"; |
5 | 5 | import { expectSwipeFuncsDir } from "./helpers";
|
6 | 6 |
|
7 | 7 | const DIRECTIONS: [typeof LEFT, typeof RIGHT, typeof UP, typeof DOWN] = [
|
@@ -34,7 +34,10 @@ const TESTING_TEXT = "touch here";
|
34 | 34 | /*
|
35 | 35 | * Wrapping component for the hook testing
|
36 | 36 | */
|
37 |
| -function SwipeableUsingHook({ nodeName = "div", ...rest }) { |
| 37 | +function SwipeableUsingHook({ |
| 38 | + nodeName = "div", |
| 39 | + ...rest |
| 40 | +}: SwipeableProps & { nodeName?: string }) { |
38 | 41 | const eventHandlers = useSwipeable(rest);
|
39 | 42 | const Elem = nodeName as React.ElementType;
|
40 | 43 | return (
|
@@ -222,6 +225,33 @@ describe("useSwipeable", () => {
|
222 | 225 | );
|
223 | 226 | });
|
224 | 227 |
|
| 228 | + it("correctly calls onSwipeStart for first swipe event", () => { |
| 229 | + const onSwipeStart = jest.fn(); |
| 230 | + const { getByText } = render( |
| 231 | + <SwipeableUsingHook onSwipeStart={onSwipeStart} /> |
| 232 | + ); |
| 233 | + |
| 234 | + const touchArea = getByText(TESTING_TEXT); |
| 235 | + |
| 236 | + // first swipe |
| 237 | + fireEvent[TS](touchArea, cte({ x: 100, y: 100 })); |
| 238 | + fireEvent[TM](touchArea, cte({ x: 100, y: 125 })); |
| 239 | + fireEvent[TM](touchArea, cte({ x: 100, y: 150 })); |
| 240 | + fireEvent[TM](touchArea, cte({ x: 100, y: 175 })); |
| 241 | + fireEvent[TE](touchArea, cte({})); |
| 242 | + |
| 243 | + expect(onSwipeStart).toHaveBeenCalledTimes(1); |
| 244 | + |
| 245 | + // second swipe |
| 246 | + fireEvent[TS](touchArea, cte({ x: 100, y: 100 })); |
| 247 | + fireEvent[TM](touchArea, cte({ x: 125, y: 125 })); |
| 248 | + fireEvent[TM](touchArea, cte({ x: 150, y: 150 })); |
| 249 | + fireEvent[TM](touchArea, cte({ x: 175, y: 175 })); |
| 250 | + fireEvent[TE](touchArea, cte({})); |
| 251 | + |
| 252 | + expect(onSwipeStart).toHaveBeenCalledTimes(2); |
| 253 | + }); |
| 254 | + |
225 | 255 | it("calls preventDefault when swiping in direction that has a callback", () => {
|
226 | 256 | const onSwipedDown = jest.fn();
|
227 | 257 |
|
@@ -346,22 +376,22 @@ describe("useSwipeable", () => {
|
346 | 376 | expectSwipeFuncsDir(swipeFuncsLeft, LEFT);
|
347 | 377 |
|
348 | 378 | // check up
|
349 |
| - const swipeFunsUp = getMockedSwipeFunctions(); |
350 |
| - rerender(<SwipeableUsingHook {...swipeFunsUp} rotationAngle={90} />); |
| 379 | + const swipeFuncsUp = getMockedSwipeFunctions(); |
| 380 | + rerender(<SwipeableUsingHook {...swipeFuncsUp} rotationAngle={90} />); |
351 | 381 | fireEvent[TS](touchArea, cte({ x: 100, y: 100 }));
|
352 | 382 | fireEvent[TM](touchArea, cte({ x: 125, y: 100 }));
|
353 | 383 | fireEvent[TM](touchArea, cte({ x: 150, y: 100 }));
|
354 | 384 | fireEvent[TE](touchArea, cte({}));
|
355 |
| - expectSwipeFuncsDir(swipeFunsUp, UP); |
| 385 | + expectSwipeFuncsDir(swipeFuncsUp, UP); |
356 | 386 |
|
357 | 387 | // check down
|
358 |
| - const swipeFunsDown = getMockedSwipeFunctions(); |
359 |
| - rerender(<SwipeableUsingHook {...swipeFunsDown} rotationAngle={90} />); |
| 388 | + const swipeFuncsDown = getMockedSwipeFunctions(); |
| 389 | + rerender(<SwipeableUsingHook {...swipeFuncsDown} rotationAngle={90} />); |
360 | 390 | fireEvent[TS](touchArea, cte({ x: 100, y: 100 }));
|
361 | 391 | fireEvent[TM](touchArea, cte({ x: 75, y: 100 }));
|
362 | 392 | fireEvent[TM](touchArea, cte({ x: 50, y: 100 }));
|
363 | 393 | fireEvent[TE](touchArea, cte({}));
|
364 |
| - expectSwipeFuncsDir(swipeFunsDown, DOWN); |
| 394 | + expectSwipeFuncsDir(swipeFuncsDown, DOWN); |
365 | 395 | });
|
366 | 396 |
|
367 | 397 | it('Handle "odd" rotations', () => {
|
|
0 commit comments