mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 09:38:54 +00:00
b7951baf23
- RTL: the measured pills and centred controls use logical forms or a marked line - i18n: the two native widget numbers are positions, not counts (NOT_PLURAL) - Types: styleOverride on the lazy GL map, the location and shared-file payloads, the capabilities fallback, the transport sheet's stay metadata - ESLint: latest-value refs move to layout effects, usePresence keeps the last open state in state, the booking import opens clean with a shared document kept in the list (the reset effect used to clear it again) - Layers: the phone import sheet lives under mobile/ and reads through tripRepo; the offline host check moves to utils/tileUrl - Sizes: plugin layer geometry, hardNavigate and the exit animations get modules of their own - Tests follow the status radios, the stay chip overview, the dock + button for the first list and the field token; store registry lists the new stores
185 lines
6.1 KiB
TypeScript
185 lines
6.1 KiB
TypeScript
import { act, fireEvent, render, screen } from '@testing-library/react';
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import { tickHaptic } from '../../native/haptics';
|
|
import type { Day } from '../../types';
|
|
import MDayPager from './MDayPager';
|
|
import { pagesFor } from './dayPagerPages';
|
|
|
|
vi.mock('../../native/haptics', () => ({ tickHaptic: vi.fn() }));
|
|
|
|
const DAYS = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }] as unknown as Day[];
|
|
|
|
function setup(selectedDayId: number | null, over: Partial<Parameters<typeof MDayPager>[0]> = {}) {
|
|
const onSelectDay = vi.fn();
|
|
const utils = render(
|
|
<MDayPager
|
|
days={DAYS}
|
|
selectedDayId={selectedDayId}
|
|
onSelectDay={onSelectDay}
|
|
describeDay={(i, n) => `day ${i + 1} of ${n}`}
|
|
renderDay={(dayId, active) => <p>{`page ${dayId ?? 'none'}${active ? ' live' : ''}`}</p>}
|
|
{...over}
|
|
/>
|
|
);
|
|
const strip = utils.container.querySelector('[data-day-pager]') as HTMLElement;
|
|
return { ...utils, strip, onSelectDay };
|
|
}
|
|
|
|
/** jsdom has no layout: say where each page sits, with `restingId` filling the strip. */
|
|
function place(strip: HTMLElement, restingId: number | null, between = false) {
|
|
strip.getBoundingClientRect = () => ({ left: 0 }) as DOMRect;
|
|
for (const page of Array.from(strip.children) as HTMLElement[]) {
|
|
const here = page.dataset.dayId === String(restingId);
|
|
page.getBoundingClientRect = () => ({ left: here ? (between ? 37 : 0) : 400 }) as DOMRect;
|
|
}
|
|
}
|
|
|
|
beforeEach(() => {
|
|
vi.mocked(tickHaptic).mockReset();
|
|
});
|
|
|
|
describe('pagesFor', () => {
|
|
it('windows the selection with its neighbours', () => {
|
|
expect(pagesFor(DAYS, 2).map((p) => [p.dayId, p.active])).toEqual([
|
|
[1, false],
|
|
[2, true],
|
|
[3, false],
|
|
]);
|
|
});
|
|
|
|
it('has no page beyond the first and last day', () => {
|
|
expect(pagesFor(DAYS, 1).map((p) => p.dayId)).toEqual([1, 2]);
|
|
expect(pagesFor(DAYS, 4).map((p) => p.dayId)).toEqual([3, 4]);
|
|
});
|
|
|
|
it('keeps a page for a selection that points at no day, with the first day as the way back', () => {
|
|
expect(pagesFor(DAYS, null).map((p) => [p.key, p.dayId, p.active])).toEqual([
|
|
['none', null, true],
|
|
['d1', 1, false],
|
|
]);
|
|
expect(pagesFor([], null).map((p) => p.key)).toEqual(['none']);
|
|
});
|
|
});
|
|
|
|
describe('MDayPager', () => {
|
|
it('draws the current day live and its neighbours inert', () => {
|
|
const { strip } = setup(2);
|
|
|
|
expect(screen.getByText('page 2 live')).toBeInTheDocument();
|
|
const pages = Array.from(strip.children) as HTMLElement[];
|
|
expect(pages.map((p) => p.hasAttribute('inert'))).toEqual([true, false, true]);
|
|
expect(pages[0]).toHaveAttribute('aria-hidden', 'true');
|
|
expect(pages[1]).not.toHaveAttribute('aria-hidden');
|
|
});
|
|
|
|
it('selects the neighbour the strip comes to rest on, with a detent and an announcement', () => {
|
|
const { strip, onSelectDay } = setup(2);
|
|
|
|
place(strip, 3);
|
|
fireEvent(strip, new Event('scrollend'));
|
|
|
|
expect(onSelectDay).toHaveBeenCalledWith(3);
|
|
expect(tickHaptic).toHaveBeenCalledTimes(1);
|
|
expect(screen.getByRole('status')).toHaveTextContent('day 3 of 4');
|
|
});
|
|
|
|
it('previews the day a swipe heads for while the strip still moves, and takes it back', () => {
|
|
const onPreview = vi.fn();
|
|
const { strip, onSelectDay } = setup(2, { onPreview });
|
|
|
|
place(strip, 3, true);
|
|
fireEvent.scroll(strip);
|
|
fireEvent.scroll(strip);
|
|
expect(onPreview).toHaveBeenCalledTimes(1);
|
|
expect(onPreview).toHaveBeenLastCalledWith(3);
|
|
expect(onSelectDay).not.toHaveBeenCalled();
|
|
|
|
place(strip, 2, true);
|
|
fireEvent.scroll(strip);
|
|
expect(onPreview).toHaveBeenLastCalledWith(null);
|
|
});
|
|
|
|
it('asks once, however many events report the same landing', () => {
|
|
const { strip, onSelectDay } = setup(2);
|
|
|
|
place(strip, 1);
|
|
fireEvent(strip, new Event('scrollend'));
|
|
fireEvent(strip, new Event('scrollend'));
|
|
|
|
expect(onSelectDay).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('does nothing when it comes back to the current day or is still between pages', () => {
|
|
const { strip, onSelectDay } = setup(2);
|
|
|
|
place(strip, 2);
|
|
fireEvent(strip, new Event('scrollend'));
|
|
place(strip, 3, true);
|
|
fireEvent(strip, new Event('scrollend'));
|
|
|
|
expect(onSelectDay).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('waits for the finger to lift where scrollend is missing', () => {
|
|
vi.useFakeTimers();
|
|
try {
|
|
const { strip, onSelectDay } = setup(2);
|
|
place(strip, 3);
|
|
|
|
fireEvent.touchStart(strip);
|
|
fireEvent.scroll(strip);
|
|
act(() => {
|
|
vi.advanceTimersByTime(200);
|
|
});
|
|
expect(onSelectDay).not.toHaveBeenCalled();
|
|
|
|
fireEvent.touchEnd(strip);
|
|
act(() => {
|
|
vi.advanceTimersByTime(200);
|
|
});
|
|
expect(onSelectDay).toHaveBeenCalledWith(3);
|
|
} finally {
|
|
vi.useRealTimers();
|
|
}
|
|
});
|
|
|
|
it('puts the strip back on the current page when the window moves', () => {
|
|
const { strip, rerender, onSelectDay } = setup(2);
|
|
// jsdom keeps no scroll offset, so give the strip one to move.
|
|
let scrollLeft = 0;
|
|
Object.defineProperty(strip, 'scrollLeft', {
|
|
configurable: true,
|
|
get: () => scrollLeft,
|
|
set: (v) => {
|
|
scrollLeft = v;
|
|
},
|
|
});
|
|
// After the selection moves to day 3, its page sits one page width to the right.
|
|
const realQuery = strip.querySelector.bind(strip);
|
|
vi.spyOn(strip, 'querySelector').mockImplementation((sel: string) => {
|
|
const el = realQuery(sel) as HTMLElement | null;
|
|
if (el && sel === '[data-page-active]') el.getBoundingClientRect = () => ({ left: 390 }) as DOMRect;
|
|
return el;
|
|
});
|
|
strip.getBoundingClientRect = () => ({ left: 0 }) as DOMRect;
|
|
|
|
rerender(
|
|
<MDayPager
|
|
days={DAYS}
|
|
selectedDayId={3}
|
|
onSelectDay={onSelectDay}
|
|
describeDay={() => ''}
|
|
renderDay={(dayId) => <p>{`page ${dayId}`}</p>}
|
|
/>
|
|
);
|
|
|
|
expect(strip.scrollLeft).toBe(390);
|
|
});
|
|
|
|
it('holds still while locked', () => {
|
|
const { strip } = setup(2, { locked: true });
|
|
expect(strip).toHaveClass('overflow-x-hidden');
|
|
expect(strip).not.toHaveClass('overflow-x-auto');
|
|
});
|
|
});
|