| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- /**
- * Tests for the useLongPress hook.
- */
- import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
- import { renderHook, act } from '@testing-library/react';
- import { useLongPress } from '../../hooks/useLongPress';
- describe('useLongPress', () => {
- beforeEach(() => {
- vi.useFakeTimers();
- });
- afterEach(() => {
- vi.useRealTimers();
- });
- it('calls onLongPress after delay', () => {
- const onLongPress = vi.fn();
- const onClick = vi.fn();
- const { result } = renderHook(() =>
- useLongPress({ onLongPress, onClick, delay: 500 })
- );
- // Simulate mouse down
- act(() => {
- result.current.onMouseDown({} as React.MouseEvent);
- });
- // Fast forward past the delay
- act(() => {
- vi.advanceTimersByTime(600);
- });
- // Should trigger long press
- expect(onLongPress).toHaveBeenCalled();
- expect(onClick).not.toHaveBeenCalled();
- });
- it('calls onClick for short press', () => {
- const onLongPress = vi.fn();
- const onClick = vi.fn();
- const { result } = renderHook(() =>
- useLongPress({ onLongPress, onClick, delay: 500 })
- );
- // Simulate mouse down
- act(() => {
- result.current.onMouseDown({} as React.MouseEvent);
- });
- // Release before delay
- act(() => {
- vi.advanceTimersByTime(200);
- result.current.onMouseUp({} as React.MouseEvent);
- });
- // Should trigger click, not long press
- expect(onClick).toHaveBeenCalled();
- expect(onLongPress).not.toHaveBeenCalled();
- });
- it('cancels on mouse leave', () => {
- const onLongPress = vi.fn();
- const onClick = vi.fn();
- const { result } = renderHook(() =>
- useLongPress({ onLongPress, onClick, delay: 500 })
- );
- // Simulate mouse down
- act(() => {
- result.current.onMouseDown({} as React.MouseEvent);
- });
- // Mouse leaves before delay
- act(() => {
- vi.advanceTimersByTime(200);
- result.current.onMouseLeave({} as React.MouseEvent);
- });
- // Continue past delay
- act(() => {
- vi.advanceTimersByTime(400);
- });
- // Neither should be called
- expect(onLongPress).not.toHaveBeenCalled();
- expect(onClick).not.toHaveBeenCalled();
- });
- it('uses default delay of 500ms', () => {
- const onLongPress = vi.fn();
- const { result } = renderHook(() =>
- useLongPress({ onLongPress })
- );
- // Simulate mouse down
- act(() => {
- result.current.onMouseDown({} as React.MouseEvent);
- });
- // Just before default delay
- act(() => {
- vi.advanceTimersByTime(450);
- });
- expect(onLongPress).not.toHaveBeenCalled();
- // After default delay
- act(() => {
- vi.advanceTimersByTime(100);
- });
- expect(onLongPress).toHaveBeenCalled();
- });
- it('handles touch events', () => {
- const onLongPress = vi.fn();
- const { result } = renderHook(() =>
- useLongPress({ onLongPress, delay: 500 })
- );
- // Simulate touch start
- act(() => {
- result.current.onTouchStart({} as React.TouchEvent);
- });
- // Fast forward past the delay
- act(() => {
- vi.advanceTimersByTime(600);
- });
- expect(onLongPress).toHaveBeenCalled();
- });
- it('cancels on touch end', () => {
- const onLongPress = vi.fn();
- const onClick = vi.fn();
- const { result } = renderHook(() =>
- useLongPress({ onLongPress, onClick, delay: 500 })
- );
- // Simulate touch start
- act(() => {
- result.current.onTouchStart({} as React.TouchEvent);
- });
- // End touch before delay
- act(() => {
- vi.advanceTimersByTime(200);
- result.current.onTouchEnd({} as React.TouchEvent);
- });
- expect(onClick).toHaveBeenCalled();
- expect(onLongPress).not.toHaveBeenCalled();
- });
- });
|