diff --git a/starsky/starsky/clientapp/src/hooks/use-gestures/use-gestures.spec.tsx b/starsky/starsky/clientapp/src/hooks/use-gestures/use-gestures.spec.tsx index addbef495..5080375b0 100644 --- a/starsky/starsky/clientapp/src/hooks/use-gestures/use-gestures.spec.tsx +++ b/starsky/starsky/clientapp/src/hooks/use-gestures/use-gestures.spec.tsx @@ -1,4 +1,4 @@ -import { createEvent, fireEvent, render, renderHook } from "@testing-library/react"; +import { createEvent, fireEvent, render } from "@testing-library/react"; import React, { act, useRef, useState } from "react"; import { mountReactHook } from "../___tests___/test-hook"; import * as callHandler from "./call-handler"; @@ -571,50 +571,88 @@ describe("useGestures", () => { }); }); - it("should call callHandler and setGesture with the correct arguments", () => { - executeTouchMove( - {} as globalThis.TouchEvent, - {} as ICurrentTouches, + it("executeTouchMove returns emthy string", () => { + const result = executeTouchMove( + { touches: [] } as unknown as globalThis.TouchEvent, + { + x: 1, + y: 1, + deltaX: 1, + deltaY: 1 + } as ICurrentTouches, {} as IHandlers, - { minDelta: 1 }, + { minDelta: 0 }, {} as ICurrentTouches, jest.fn() ); - }); - - it("should correctly destructure args array", () => { - // const mockEventName = "touchstart"; - // // const mockTouches: ICurrentTouches = { - // // x: 100, - // // y: 100 - // // }; - // const mockGesture = "swipe"; - - // const args: [string, ICurrentTouches, string] = [mockEventName, mockTouches, mockGesture]; - - jest - .spyOn(React, "useState") - .mockReset() - .mockImplementationOnce(() => [{ pointers: ["a"] }, jest.fn()]) - .mockImplementationOnce(() => ["gesture1", jest.fn()]); - - const result = renderHook(() => { - const inputFormControlReference = useRef(null); - return [
, inputFormControlReference]; - }); - - const reference = result.result.current[1] as React.RefObject; - const result1 = renderHook(() => useGestures(reference, { onPanStart: jest.fn() })); - - console.log(result1.result.current); + expect(result).toBe(""); + }); - // Call the function that contains the destructuring - // const [eventNameScoped, , theGestureScoped] = args; + describe("executeTouchMove", () => { + const testCases = [ + { + description: "swipeRight", + currentTouches: { x: 150, y: 150, deltaX: 1000, deltaY: 5 }, + options: { minDelta: 10 }, + expectedGesture: "swipeRight" + }, + { + description: "swipeUp", + currentTouches: { x: 150, y: 150, deltaX: 5, deltaY: -1000 }, + options: { minDelta: 10 }, + expectedGesture: "swipeUp" + }, + { + description: "swipeDown", + currentTouches: { x: 150, y: 150, deltaX: 5, deltaY: 1000 }, + options: { minDelta: 10 }, + expectedGesture: "swipeDown" + }, + { + description: "swipeLeft", + currentTouches: { x: 150, y: 150, deltaX: -1000, deltaY: 5 }, + options: { minDelta: 10 }, + expectedGesture: "swipeLeft" + } + ]; + + test.each(testCases)( + "should detect $description gesture", + ({ currentTouches, options, expectedGesture }) => { + jest.spyOn(debounce, "debounce").mockImplementationOnce((arg) => { + arg("onSwipe", {}, expectedGesture); + return jest.fn(); + }); - // Verify that the variables are correctly assigned - // expect(eventNameScoped).toBe(mockEventName); - // expect(touchesScoped).toBe(mockTouches); - // expect(theGestureScoped).toBe(mockGesture); + const callHandlerSpy = jest + .spyOn(callHandler, "callHandler") + .mockImplementationOnce(() => {}); + + const mockTouchEvent = { touches: [] } as unknown as globalThis.TouchEvent; + const handlers: IHandlers = {}; + const previousTouches: ICurrentTouches = {} as ICurrentTouches; + const callback = jest.fn(); + + const result = executeTouchMove( + mockTouchEvent, + currentTouches as ICurrentTouches, + handlers, + options, + previousTouches, + callback + ); + + // Verify that the condition is hit + expect( + Math.abs(currentTouches.deltaX) >= options.minDelta || + Math.abs(currentTouches.deltaY) >= options.minDelta + ).toBe(true); + + // Verify that the callback is called with the expected gesture + expect(callHandlerSpy).toHaveBeenCalled(); + expect(result).toBe(expectedGesture); + } + ); }); });