Files
TREK/client/src/mobile/components/MEdgeBack.test.tsx
T
Maurice faaa56eda6 fix(mobile): harden back gestures and repair CI regressions
Reflect the drag position in the sheet scrim and match back gestures to the reading direction. Keep Escape on the top overlay and update failing tests for the redesigned forms, navigation, and offline preparation. Remove dead exports and lower obsolete lint counts.
2026-12-03 23:30:26 +01:00

161 lines
5.9 KiB
TypeScript

import { render } from '@testing-library/react';
import { useRef } from 'react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { closeTopOverlay, hasOpenOverlay } from '../../hooks/overlayStack';
import { tickHaptic } from '../../native/haptics';
import MEdgeBack from './MEdgeBack';
vi.mock('../../hooks/overlayStack', () => ({ closeTopOverlay: vi.fn(), hasOpenOverlay: vi.fn(() => false) }));
vi.mock('../../native/haptics', () => ({ tickHaptic: vi.fn() }));
function Host({ enabled = true }: { enabled?: boolean }) {
const ref = useRef<HTMLDivElement>(null);
return (
<>
<div ref={ref} data-testid="page" />
<MEdgeBack target={ref} enabled={enabled} />
</>
);
}
function touch(type: string, x: number, y = 300, t = 0) {
const e = new Event(type, { bubbles: true, cancelable: true }) as Event & Record<string, unknown>;
Object.defineProperty(e, 'touches', { value: type === 'touchend' ? [] : [{ clientX: x, clientY: y }] });
Object.defineProperty(e, 'changedTouches', { value: [{ clientX: x, clientY: y }] });
Object.defineProperty(e, 'timeStamp', { value: t });
document.dispatchEvent(e);
}
beforeEach(() => {
vi.useFakeTimers();
vi.mocked(hasOpenOverlay).mockReturnValue(false);
window.history.replaceState({ idx: 2 }, '');
Object.defineProperty(window, 'innerWidth', { configurable: true, value: 400 });
});
afterEach(() => {
document.documentElement.removeAttribute('dir');
vi.useRealTimers();
vi.clearAllMocks();
});
describe('MEdgeBack', () => {
it('mirrors the swipe from the right edge in a right-to-left interface', () => {
document.documentElement.dir = 'rtl';
const back = vi.spyOn(window.history, 'back').mockImplementation(() => {});
const { getByTestId } = render(<Host />);
touch('touchstart', 395, 300, 0);
touch('touchmove', 220, 300, 100);
expect(getByTestId('page').style.transform).toBe('translateX(-175px)');
touch('touchend', 220, 300, 150);
vi.advanceTimersByTime(280);
expect(back).toHaveBeenCalledTimes(1);
});
it('drops a scheduled back navigation if the history changes first', () => {
const back = vi.spyOn(window.history, 'back').mockImplementation(() => {});
const { getByTestId } = render(<Host />);
touch('touchstart', 5, 300, 0);
touch('touchmove', 200, 300, 100);
touch('touchend', 200, 300, 150);
window.dispatchEvent(new PopStateEvent('popstate'));
vi.advanceTimersByTime(280);
expect(getByTestId('page').style.transform).toBe('');
expect(back).not.toHaveBeenCalled();
});
it('drags the page with a finger from the left edge, and goes back past a third', () => {
const back = vi.spyOn(window.history, 'back').mockImplementation(() => {});
const { getByTestId } = render(<Host />);
touch('touchstart', 5, 300, 0);
touch('touchmove', 40, 302, 50);
touch('touchmove', 200, 304, 400);
expect(getByTestId('page').style.transform).toBe('translateX(195px)');
touch('touchend', 200, 304, 900);
vi.advanceTimersByTime(250);
expect(back).toHaveBeenCalledTimes(1);
expect(tickHaptic).toHaveBeenCalled();
});
it('springs back on a short, slow drag', () => {
const back = vi.spyOn(window.history, 'back').mockImplementation(() => {});
const { getByTestId } = render(<Host />);
touch('touchstart', 5, 300, 0);
touch('touchmove', 30, 300, 200);
touch('touchmove', 60, 300, 800);
touch('touchend', 60, 300, 1600);
vi.advanceTimersByTime(300);
expect(back).not.toHaveBeenCalled();
expect(getByTestId('page').style.transform).toBe('');
});
it('cancels a swipe interrupted by the system even past the commit point', () => {
const back = vi.spyOn(window.history, 'back').mockImplementation(() => {});
const { getByTestId } = render(<Host />);
touch('touchstart', 5);
touch('touchmove', 200, 300, 100);
touch('touchcancel', 200, 300, 105);
vi.advanceTimersByTime(300);
expect(getByTestId('page').style.transform).toBe('');
expect(back).not.toHaveBeenCalled();
expect(tickHaptic).not.toHaveBeenCalled();
});
it('uses the release speed and stops treating a held swipe as a flick', () => {
const back = vi.spyOn(window.history, 'back').mockImplementation(() => {});
render(<Host />);
touch('touchstart', 5);
touch('touchmove', 65, 300, 40);
touch('touchend', 65, 300, 700);
vi.advanceTimersByTime(300);
expect(back).not.toHaveBeenCalled();
touch('touchstart', 5, 300, 800);
touch('touchmove', 65, 300, 840);
touch('touchend', 65, 300, 850);
vi.advanceTimersByTime(300);
expect(back).toHaveBeenCalledTimes(1);
});
it('closes an open sheet first and leaves the page where it is', () => {
vi.mocked(hasOpenOverlay).mockReturnValue(true);
const back = vi.spyOn(window.history, 'back').mockImplementation(() => {});
const { getByTestId } = render(<Host />);
touch('touchstart', 5, 300, 0);
touch('touchmove', 200, 300, 100);
expect(getByTestId('page').style.transform).toBe('');
touch('touchend', 200, 300, 200);
expect(closeTopOverlay).toHaveBeenCalledTimes(1);
expect(back).not.toHaveBeenCalled();
});
it('ignores a touch away from the edge, a vertical scroll, the first page and a binary without the feature', () => {
const back = vi.spyOn(window.history, 'back').mockImplementation(() => {});
const { unmount } = render(<Host />);
touch('touchstart', 120);
touch('touchmove', 300);
touch('touchend', 300, 300, 100);
touch('touchstart', 5, 300);
touch('touchmove', 8, 400);
touch('touchend', 8, 400, 100);
window.history.replaceState({ idx: 0 }, '');
touch('touchstart', 5);
touch('touchmove', 300);
touch('touchend', 300, 300, 100);
unmount();
window.history.replaceState({ idx: 2 }, '');
render(<Host enabled={false} />);
touch('touchstart', 5);
touch('touchmove', 300);
touch('touchend', 300, 300, 100);
vi.advanceTimersByTime(300);
expect(back).not.toHaveBeenCalled();
});
});