|
| 1 | +import React from "react"; |
| 2 | +import { render, fireEvent } from "@testing-library/react"; |
| 3 | +import MobileSwiper, { SwipeInput } from "@/components/mobile-swiper"; |
| 4 | + |
| 5 | +describe("MobileSwiper", () => { |
| 6 | + it("should trigger onSwipe with correct input on touch end", () => { |
| 7 | + const onSwipeMock = jest.fn(); |
| 8 | + const { container } = render( |
| 9 | + <MobileSwiper onSwipe={onSwipeMock}> |
| 10 | + <div data-testid="swipe-target">Swipe me!</div> |
| 11 | + </MobileSwiper>, |
| 12 | + ); |
| 13 | + |
| 14 | + const swipeTarget = container.querySelector( |
| 15 | + "[data-testid='swipe-target']", |
| 16 | + ) as HTMLElement; |
| 17 | + |
| 18 | + fireEvent.touchStart(swipeTarget, { |
| 19 | + touches: [{ clientX: 0, clientY: 0 }], |
| 20 | + }); |
| 21 | + fireEvent.touchEnd(swipeTarget, { |
| 22 | + changedTouches: [{ clientX: 50, clientY: 0 }], |
| 23 | + }); |
| 24 | + |
| 25 | + expect(onSwipeMock).toHaveBeenCalledWith({ deltaX: 50, deltaY: 0 }); |
| 26 | + }); |
| 27 | + |
| 28 | + it("should not trigger onSwipe if touch is outside the component", () => { |
| 29 | + const onSwipeMock = jest.fn(); |
| 30 | + const { container } = render( |
| 31 | + <MobileSwiper onSwipe={onSwipeMock}> |
| 32 | + <div data-testid="swipe-target">Swipe me!</div> |
| 33 | + </MobileSwiper>, |
| 34 | + ); |
| 35 | + |
| 36 | + const swipeTarget = container.querySelector( |
| 37 | + "[data-testid='swipe-target']", |
| 38 | + ) as HTMLElement; |
| 39 | + |
| 40 | + fireEvent.touchStart(swipeTarget, { |
| 41 | + touches: [{ clientX: 0, clientY: 0 }], |
| 42 | + }); |
| 43 | + fireEvent.touchEnd(document.body, { |
| 44 | + changedTouches: [{ clientX: 50, clientY: 0 }], |
| 45 | + }); |
| 46 | + |
| 47 | + expect(onSwipeMock).not.toHaveBeenCalled(); |
| 48 | + }); |
| 49 | +}); |
0 commit comments