Files
TREK/client/src/mobile/components/MSwipeRow.test.tsx
T
Maurice 0b46b89b62 fix(mobile): swipe actions are rounded tiles beside the row, icon only; expenses keep their own buttons
The actions under a swiped row were square blocks to the row's edge, unlike
the rounded cards around them. They are now tiles of their own with the
card's radius and a gap, inset within a grouped card such as the packing
list, and show only their icon (the label names them for a screen reader).
An expense already shows edit and delete beside its card, so it no longer
swipes to a second, larger pair of the same buttons.
2026-11-10 00:11:50 +01:00

98 lines
3.3 KiB
TypeScript

import { Check, Trash2 } from 'lucide-react';
import { describe, expect, it, vi } from 'vitest';
import { act, fireEvent, render, screen } from '../../../tests/helpers/render';
import MSwipeRow from './MSwipeRow';
vi.mock('../../native/haptics', () => ({ tickHaptic: vi.fn() }));
/** jsdom has no Touch to build a TouchEvent from: an Event with the touches set on it, as MEdgeBack's test does. */
function touch(el: Element, type: 'touchstart' | 'touchmove' | 'touchend', x = 0, y = 100) {
const e = new Event(type, { bubbles: true, cancelable: true });
Object.defineProperty(e, 'touches', { value: type === 'touchend' ? [] : [{ clientX: x, clientY: y }] });
act(() => {
el.dispatchEvent(e);
});
}
function setup() {
const onDelete = vi.fn();
const onDone = vi.fn();
const onRowClick = vi.fn();
render(
<MSwipeRow
leading={{ label: 'Done', icon: Check, tone: 'accent', onAction: onDone }}
trailing={[{ label: 'Delete', icon: Trash2, tone: 'danger', onAction: onDelete }]}
>
<button type="button" onClick={onRowClick}>
Sunscreen
</button>
</MSwipeRow>
);
const row = screen.getByText('Sunscreen').closest('div')?.parentElement as HTMLElement;
return { row, onDelete, onDone, onRowClick };
}
describe('MSwipeRow', () => {
it('FE-MOB-SWIPE-001: shows no action buttons until the row is swiped', () => {
setup();
expect(screen.queryByLabelText('Delete')).toBeNull();
expect(screen.queryByLabelText('Done')).toBeNull();
});
it('FE-MOB-SWIPE-002: a swipe towards the start opens the trailing action, a tap on it runs it', () => {
const { row, onDelete } = setup();
touch(row, 'touchstart', 200);
touch(row, 'touchmove', 160);
touch(row, 'touchmove', 120);
touch(row, 'touchend');
fireEvent.click(screen.getByLabelText('Delete'));
expect(onDelete).toHaveBeenCalledOnce();
});
it('FE-MOB-SWIPE-003: a mostly vertical drag is a scroll and opens nothing', () => {
const { row } = setup();
touch(row, 'touchstart', 200, 100);
touch(row, 'touchmove', 190, 160);
touch(row, 'touchend');
expect(screen.queryByLabelText('Delete')).toBeNull();
});
it('FE-MOB-SWIPE-004: a tap on an open row closes it instead of opening the row', () => {
const { row, onRowClick } = setup();
touch(row, 'touchstart', 200);
touch(row, 'touchmove', 160);
touch(row, 'touchmove', 120);
touch(row, 'touchend');
fireEvent.click(screen.getByText('Sunscreen'));
expect(onRowClick).not.toHaveBeenCalled();
});
it('FE-MOB-SWIPE-005: a swipe that starts at the screen edge is left to the back gesture', () => {
const { row } = setup();
touch(row, 'touchstart', 10);
touch(row, 'touchmove', 120);
touch(row, 'touchend');
expect(screen.queryByLabelText('Done')).toBeNull();
});
it('FE-MOB-SWIPE-006: swiped through past most of the row, the first action runs at once', () => {
const { row, onDelete } = setup();
row.getBoundingClientRect = () => ({
width: 360,
height: 48,
top: 0,
left: 0,
right: 360,
bottom: 48,
x: 0,
y: 0,
toJSON: () => ({}),
});
touch(row, 'touchstart', 330);
touch(row, 'touchmove', 300);
touch(row, 'touchmove', 60);
touch(row, 'touchend');
expect(onDelete).toHaveBeenCalledOnce();
});
});