Files
TREK/client/tests/unit/mobile/useIsPhone.test.ts
T
Maurice c204a25a9c feat(mobile): the touch shell laid out for iPad and unfolded foldables
The app shows the touch shell at every size now, an iPad's and an unfolded
foldable's included, instead of switching to the desktop pages from 768px.
One route tree survives rotation, Split View, Stage Manager and folding, and
the app's own features (Wallet, sharing into a trip, the edge swipe back)
are there on the big screen too.

Two size classes drive the layout (hooks/sizeClass.ts, Tailwind variants
wide: and split:):

- wide (600pt and taller than a phone on its side): a glass navigation rail
  on the leading edge replaces the dock and holds every destination with its
  name; sheets become centred form sheets; the trip's More menu is a popover
  beside the rail; lists keep a readable measure; dashboard, journey and
  vacay get columns; full-bleed maps run under the floating rail.
- split (900pt): the trip's plan, places or road trip chain sit in a pane
  beside the map, which follows the plan's day; the list/map switch hides
  and shows the pane like a sidebar; on a foldable that reports its fold the
  pane ends at the fold. Settings show their sections beside the page and the
  dashboard keeps its widgets in a column of their own.

A hardware keyboard gets the shortcuts an iPad app has (⌘1 to ⌘9 for the
sections, ⌘N, ⌘, , ⌥←/⌥→ through the days, ⌃⌘S for the pane, ⌘[ back),
answered by the page or, with a binary that has the keyCommands feature,
handed to it as native key commands.

The day strip fades at an edge that runs on, journey cards grow on a wide
window and a card without a photo takes the day's colour, and the Leaflet
map measures itself again when its box changes size.
2026-11-07 23:25:12 +01:00

108 lines
4.2 KiB
TypeScript

import { describe, it, expect, vi, afterEach } from 'vitest';
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { renderHook, act } from '@testing-library/react';
import { useIsPhone, isPhoneViewport, PHONE_QUERY } from '../../../src/mobile/useIsPhone';
const native = vi.hoisted(() => ({ app: false }));
vi.mock('../../../src/native/platform', () => ({
isNativeApp: () => native.app,
isNativeIos: () => native.app,
nativeServerHost: () => null,
}));
// FE-MOB-PHONE-001 onwards
type ChangeListener = (e: { matches: boolean }) => void;
function mockMatchMedia(matches: boolean) {
let listener: ChangeListener | null = null;
window.matchMedia = vi.fn().mockImplementation((query: string) => ({
matches,
media: query,
addEventListener: (_event: string, cb: ChangeListener) => { listener = cb; },
removeEventListener: vi.fn(),
})) as unknown as typeof window.matchMedia;
return { fire: (m: boolean) => act(() => listener?.({ matches: m })) };
}
const originalMatchMedia = window.matchMedia;
describe('useIsPhone', () => {
afterEach(() => {
window.matchMedia = originalMatchMedia;
native.app = false;
});
it('FE-MOB-PHONE-007: the app shows the touch shell at every size, an iPad and an unfolded foldable included', () => {
native.app = true;
mockMatchMedia(false);
expect(isPhoneViewport()).toBe(true);
const { result } = renderHook(() => useIsPhone());
expect(result.current).toBe(true);
// Nothing to follow: a rotation or a fold must never swap the route tree.
expect(window.matchMedia).not.toHaveBeenCalled();
});
it('FE-MOB-PHONE-001: returns true when the viewport matches the phone query', () => {
mockMatchMedia(true);
const { result } = renderHook(() => useIsPhone());
expect(result.current).toBe(true);
expect(window.matchMedia).toHaveBeenCalledWith(PHONE_QUERY);
});
it('FE-MOB-PHONE-004: a phone on its side stays a phone, a tablet and a desktop window do not', () => {
// Real media query semantics for the three viewports that matter, evaluated
// by hand: jsdom does not implement pointer or height features.
const viewports = {
phoneLandscape: { width: 932, height: 430, coarse: true },
tabletLandscape: { width: 1180, height: 820, coarse: true },
shortDesktopWindow: { width: 900, height: 450, coarse: false },
};
const evaluate = (v: { width: number; height: number; coarse: boolean }) =>
PHONE_QUERY.split(',').some(part => {
const p = part.trim();
if (p === '(max-width: 767px)') return v.width <= 767;
if (p === '(pointer: coarse) and (max-height: 500px)') return v.coarse && v.height <= 500;
throw new Error(`unexpected query part ${p}`);
});
expect(evaluate(viewports.phoneLandscape)).toBe(true);
expect(evaluate(viewports.tabletLandscape)).toBe(false);
expect(evaluate(viewports.shortDesktopWindow)).toBe(false);
});
it('FE-MOB-PHONE-006: the phone rules in the stylesheets use the same query as the shell', () => {
for (const file of ['src/index.css', 'src/components/Files/FileManager.tsx']) {
const source = readFileSync(resolve(__dirname, '../../..', file), 'utf8');
const queries = [...source.matchAll(/@media ([^{]*max-width: 767px[^{]*)\{/g)].map(m => m[1].trim());
expect(queries.length).toBeGreaterThan(0);
for (const q of queries) expect([PHONE_QUERY, '(max-width: 767px), (pointer: coarse)']).toContain(q);
}
});
it('FE-MOB-PHONE-005: isPhoneViewport answers outside React, and false without matchMedia', () => {
mockMatchMedia(true);
expect(isPhoneViewport()).toBe(true);
window.matchMedia = undefined as unknown as typeof window.matchMedia;
expect(isPhoneViewport()).toBe(false);
});
it('FE-MOB-PHONE-002: returns false on wider viewports', () => {
mockMatchMedia(false);
const { result } = renderHook(() => useIsPhone());
expect(result.current).toBe(false);
});
it('FE-MOB-PHONE-003: reacts to media query changes', () => {
const mq = mockMatchMedia(false);
const { result } = renderHook(() => useIsPhone());
expect(result.current).toBe(false);
mq.fire(true);
expect(result.current).toBe(true);
mq.fire(false);
expect(result.current).toBe(false);
});
});