From 1be79c1b61e35c39b3d62ba7498fac84fecc77fe Mon Sep 17 00:00:00 2001 From: therealsama <263038098+therealsama@users.noreply.github.com> Date: Sat, 5 Sep 2026 19:57:53 +0200 Subject: [PATCH] fix: split where mobile ends from who can swipe MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `useIsMobile` answered two questions with one media query, and each answer was wrong for someone. Shape — how much chrome fits in the header — asked 767px in px while every style around it switched on 48rem, so any reader whose default font size is not 16px got one layout's chrome under the other's styling: at a 24px default the pills grew icons at 900px that the packer had reserved nothing for. It now asks the desktop side, `(min-width: 48rem)`, and inverts it, so mobile is the absence of `md:` rather than a second edge that can drift past it. `rem` in a media query is the browser's default font size and ignores `html { font-size }` — the same rule `md:` follows. Which tree mounts was the same query, and that is not a width question. The swipe viewer binds touch and nothing else — no keys, no wheel, no buttons — so a reader without a finger who lands in it cannot advance the feed at all. Width put them there: by narrowing a window under 768px, and, once the query moved to rem, at a 1100px window on a 24px default font, which is a low-vision desktop reader sent to a gesture they have no way to perform. `useCanSwipe` asks for a coarse primary pointer as well, in the form that leaves a touch laptop on the list, and re-answers when a trackpad appears mid-session. Two more places inferred the tree from width and now ask what they mean. index.html bets on a layout before React exists, so the first paint does not carry the other one's scrollbar; it asked `innerWidth < 768`, a third formulation, and now asks both of the hook's questions verbatim. The padding keeping the network bar off the document scroller is the list's alone, since swipe panels reserve that strip themselves — it moves onto `
`, which already pads for the fixed header above it, and asks React which tree it is in. The pre-paint class was the other candidate, and it cannot answer: a route filter that matches every hash sets it on pages no viewer will ever mount, and only a viewer takes it off, so an unrouted URL reserved nothing at all. Asking React means React has to know, and one place had not been told. The resolver that decides story-or-comment paints the viewer's own panel while it waits, and that panel reserves the fixed chrome itself; it now declares swipe mode before paint and releases it on the way out, the way the viewer's own hook does. That also stops the skeleton starting a whole header below the story that replaces it, which it did on every phone deep-link into an item. Both of `
`'s reservations now measure the token they stand for. The header's was `pt-14`, 3px more than `--header-height`, and a full-page state sizes itself as the viewport minus both tokens — so the slack was 3px of scroll on a page written to fit exactly. The shared `matchMedia` stub answered `false` to everything, backwards for a `min-width` question in a 1024px jsdom window. It answers width queries from `window.innerWidth` now — through a getter, since the hook caches one list per query for the module's life and a test that moves the viewport has to be able to change the answer — and still answers `pointer` false, which is what makes an unmocked `useCanSwipe` read as the mouse jsdom is. Guards, each mutation-tested: the gate keeps the list for a mouse however narrow the window and follows a pointer that changes; the layout question is asked in rem, checked at the two viewports where px and rem disagree; the first paint agrees with the hook in both directions, tested with the app's own scripts cut so only the inline one can have set the class; and the bar's strip and the header are each reserved exactly once, at the size of the token that stands for them, in the viewer, in the list, and under a panel that has no viewer yet — the list's case put to a full-page state, which fits the viewport only if both of its reservations are those tokens. Specs that simulated a phone by narrowing a desktop window now declare a finger, which also runs the headline-selection guard on all four projects instead of two. The navigation specs ask which tree mounted the way the app asks, and now say which one they found: a headline is a visible link in either, so a branch that only picked its waits could not fail. --- e2e/domain-filter.spec.ts | 6 +- e2e/fixtures/swipe-helpers.ts | 14 ++ e2e/header-overflow.spec.ts | 10 +- e2e/layout-tokens.spec.ts | 78 +++++++++ e2e/mobile-breakpoint.spec.ts | 244 ++++++++++++++++++++++++++++ e2e/navigation.spec.ts | 33 ++-- e2e/network-status-bar.spec.ts | 6 +- e2e/touch-feel.spec.ts | 55 ++++--- e2e/user-submissions.spec.ts | 4 +- index.html | 10 +- src/App.test.tsx | 2 +- src/App.tsx | 42 +++-- src/components/Header.test.tsx | 4 +- src/components/Header.tsx | 13 +- src/hooks/useCanSwipe.test.ts | 97 +++++++++++ src/hooks/useCanSwipe.ts | 27 +++ src/hooks/useIsMobile.test.ts | 67 -------- src/hooks/useIsMobile.ts | 43 ----- src/hooks/useIsMobileLayout.test.ts | 70 ++++++++ src/hooks/useIsMobileLayout.ts | 23 +++ src/hooks/useMediaQuery.test.ts | 84 ++++++++++ src/hooks/useMediaQuery.ts | 46 ++++++ src/index.css | 12 +- src/test/matchMedia.test.ts | 37 +++++ src/test/setup.ts | 21 ++- 25 files changed, 853 insertions(+), 195 deletions(-) create mode 100644 e2e/mobile-breakpoint.spec.ts create mode 100644 src/hooks/useCanSwipe.test.ts create mode 100644 src/hooks/useCanSwipe.ts delete mode 100644 src/hooks/useIsMobile.test.ts delete mode 100644 src/hooks/useIsMobile.ts create mode 100644 src/hooks/useIsMobileLayout.test.ts create mode 100644 src/hooks/useIsMobileLayout.ts create mode 100644 src/hooks/useMediaQuery.test.ts create mode 100644 src/hooks/useMediaQuery.ts create mode 100644 src/test/matchMedia.test.ts diff --git a/e2e/domain-filter.spec.ts b/e2e/domain-filter.spec.ts index 7e81ca9..52e318a 100644 --- a/e2e/domain-filter.spec.ts +++ b/e2e/domain-filter.spec.ts @@ -398,9 +398,9 @@ test.describe('Domain Filter - Mobile Swipe', () => { await expect(page.getByRole('button', { name: /try again/i })).toBeVisible(); // Confirm the error UI is rendered inside the swipe viewer's container, - // not the desktop list's wrapper. Catches a regression that flips the - // viewport branch (e.g. an `isMobile` change leaking the desktop list - // into mobile viewports). + // not the desktop list's wrapper. Catches a regression that flips + // `useCanSwipe` — its width half or its pointer half — and leaks the + // desktop list into a phone. await expect(page.getByTestId('swipe-container')).toBeVisible(); }); diff --git a/e2e/fixtures/swipe-helpers.ts b/e2e/fixtures/swipe-helpers.ts index cedaf3f..5f94610 100644 --- a/e2e/fixtures/swipe-helpers.ts +++ b/e2e/fixtures/swipe-helpers.ts @@ -6,6 +6,20 @@ interface ActiveSwipeTextOptions { timeout?: number; } +/** + * Whether this page gets the swipe viewer, asked the way `useCanSwipe` asks it. + * + * A narrow viewport on its own stopped answering it: the viewer wants a finger + * too, so a desktop project only reaches it with `hasTouch`. + */ +export function mountsSwipeViewer(page: Page): Promise { + return page.evaluate( + () => + !matchMedia('(min-width: 48rem)').matches && + matchMedia('(pointer: coarse) and (hover: none)').matches, + ); +} + /** * Wait for swipe container to be ready with multiple panels. * Verifies container visibility, minimum panel count, rendered dimensions, diff --git a/e2e/header-overflow.spec.ts b/e2e/header-overflow.spec.ts index ab6a06d..8772c3c 100644 --- a/e2e/header-overflow.spec.ts +++ b/e2e/header-overflow.spec.ts @@ -123,11 +123,11 @@ test.describe('Header overflow — More dropdown', () => { const showItem = page.getByRole('menuitem', { name: 'show' }); await expect(showItem).toBeVisible(); await showItem.click(); - // On this viewport SwipeStoryViewer mounts for /show and immediately - // replaces the URL with /item/:firstShowId (objectID 99999 in the - // mock) — racing Playwright's first poll. Same pattern as the - // domain-filter URL assertion. Either form is a pass: both prove - // navigation reached the show feed. + // Either form is a pass: both prove navigation reached the show feed. + // Which one shows up is the project's pointer, not this test's business — + // a touch project mounts SwipeStoryViewer, which replaces the URL with + // /item/:firstShowId (objectID 99999 in the mock) faster than Playwright's + // first poll; a mouse gets the list at 320px and stays on /#/show. await expect(page).toHaveURL(/#\/(show|item\/99999)/); }); diff --git a/e2e/layout-tokens.spec.ts b/e2e/layout-tokens.spec.ts index 790b04b..e4684fc 100644 --- a/e2e/layout-tokens.spec.ts +++ b/e2e/layout-tokens.spec.ts @@ -149,3 +149,81 @@ test.describe('--network-bar-height', () => { await expectChromeTokenToMatch(page, '--network-bar-height', '.network-status-bar'); }); }); + +// The one place that paints the viewer's markup without the viewer: while it +// decides story-or-comment, the panel is on screen and pads for the header +// itself. React has to know that, or `
` reserves the header a second time +// and the skeleton starts a whole header below the story that replaces it. +test.describe('the chrome under a panel with no viewer yet', () => { + test.use({ viewport: { width: 412, height: 839 }, hasTouch: true }); + + test('is reserved once, by the panel', async ({ page }) => { + await setupApiMocks(page); + // Hold the type lookup open: the window is one round trip otherwise. + await page.route('**/v0/item/12345.json*', async route => { + await new Promise(resolve => setTimeout(resolve, 3000)); + await route.fallback(); + }); + + await page.goto('/#/item/12345'); + await expect(page.getByTestId('swipe-panel')).toBeVisible(); + + const { contentTop, headerBottom } = await page.evaluate(() => { + const panel = document.querySelector('.swipe-snap-panel.active')!; + return { + contentTop: panel.firstElementChild!.getBoundingClientRect().top, + headerBottom: document.querySelector('.app-header')!.getBoundingClientRect().bottom, + }; + }); + + expect(contentTop).toBeCloseTo(headerBottom, 0); + }); +}); + +// The list tree's half of the same sum. `
` reserves both fixed bars for +// whatever page is below it, and each reservation is the token the panels use — +// not a second number that lands near it, which is what `pt-14` was: 3px of +// slack, and the only hand-written chrome offset left in the app. +// +// Only reachable below `md:`, where the header is fixed. Above it the header is +// in flow, `md:pt-0` applies, and the sum is trivially exact — which is why the +// desktop centring test never saw this. +test.describe('the chrome around a list on a narrow viewport', () => { + test.use({ viewport: { width: 412, height: 839 }, hasTouch: true }); + + test('reserves exactly the header it sits under', async ({ page }) => { + await setupApiMocks(page); + // The profile is the page a phone gets in the list tree; a mouse gets every + // page there, at any width under `md:`. + await page.goto('/#/user/pg'); + await expect(page.getByRole('heading', { level: 1, name: 'pg' })).toBeVisible(); + + // The reservation against the thing reserved, rather than against where the + // first child happens to start — a margin there would be nobody's bug. + const { reserved, header } = await page.evaluate(() => ({ + reserved: parseFloat(getComputedStyle(document.querySelector('main')!).paddingTop), + header: document.querySelector('.app-header')!.getBoundingClientRect().height, + })); + + expect(reserved).toBeCloseTo(header, 0); + }); + + test('leaves a full-page state fitting the viewport exactly', async ({ page, context }) => { + await setupApiMocks(page); + await page.goto('/#/no-such-route'); + await expect(page.getByText('Lost in the feed')).toBeVisible(); + // Offline so all three terms are non-zero: the state sizes itself as the + // viewport less both tokens, so it fits only if both of `
`'s + // reservations are those same tokens. This is what the 3px cost — a page + // written to fit, scrolling. + await setOfflineAndWaitForBar(page, context); + + await expect + .poll(() => + page.evaluate( + () => document.documentElement.scrollHeight - document.documentElement.clientHeight, + ), + ) + .toBeLessThanOrEqual(1); + }); +}); diff --git a/e2e/mobile-breakpoint.spec.ts b/e2e/mobile-breakpoint.spec.ts new file mode 100644 index 0000000..00767be --- /dev/null +++ b/e2e/mobile-breakpoint.spec.ts @@ -0,0 +1,244 @@ +import { test, expect, type Page } from '@playwright/test'; +import { setupApiMocks } from './fixtures/api-mocks'; +import { + emulateBrowserFontSize, + enlargeRootFont, + setOfflineAndWaitForBar, +} from './fixtures/layout-helpers'; +import { waitForSwipeReady } from './fixtures/swipe-helpers'; + +// Two questions live here, and they used to be one. +// +// `useIsMobileLayout` is shape: how much chrome fits, asked in `md:`'s own +// 48rem so the answer cannot disagree with the styles around it. It used to ask +// 767px, which only equals 48rem at a 16px root. +// +// `useCanSwipe` is which tree mounts, and it wants a finger as well, because +// the swipe viewer binds touch and nothing else — no keys, no wheel, no +// buttons. Width alone put mouse readers in it: by a narrowed window, or by a +// default font size large enough to make 48rem wider than their screen. +// +// The tree is read off the URL: the swipe viewer rewrites `/` to the story it +// lands on, and the list leaves it alone. + +const TOP_STORY_TITLE = 'Rust Is the Future of JavaScript Infrastructure'; +// The viewer rewrites `/` to the story it lands on; the list leaves it alone. +const ITEM_URL = /\/#\/item\/\d+/; + +const reservation = (page: Page) => + page.evaluate(() => ({ + main: getComputedStyle(document.querySelector('main')!).paddingBottom, + panel: getComputedStyle( + document.querySelector('.swipe-snap-panel.active') ?? document.body, + ).paddingBottom, + bar: getComputedStyle(document.documentElement) + .getPropertyValue('--network-bar-height') + .trim(), + })); + +const mdMatches = (page: Page) => + page.evaluate(() => ({ + md: matchMedia('(min-width: 48rem)').matches, + px: matchMedia('(min-width: 768px)').matches, + })); + +test.describe('A 900px viewport at a 24px default font — with a finger', () => { + test.use({ viewport: { width: 900, height: 800 }, hasTouch: true }); + + test('is mobile on both counts, and mounts the viewer', async ({ page, browserName }) => { + test.skip(browserName !== 'chromium', 'only Chromium can emulate the setting'); + await emulateBrowserFontSize(page, 24); + await setupApiMocks(page); + await page.goto('/#/'); + + // Without this window there is nothing for the assertion below to catch: + // 48rem is 1152px here, so only the old px query would have said desktop. + expect( + await mdMatches(page), + 'this viewport no longer sits between the rem and px md breakpoints', + ).toEqual({ md: false, px: true }); + + await page.waitForURL(ITEM_URL, { timeout: 10_000 }); + await waitForSwipeReady(page); + }); + + test('reserves the bar inside its panels, not around the flow', async ({ + page, + context, + browserName, + }) => { + test.skip(browserName !== 'chromium', 'only Chromium can emulate the setting'); + await emulateBrowserFontSize(page, 24); + await setupApiMocks(page); + await page.goto('/#/'); + await page.waitForURL(ITEM_URL, { timeout: 10_000 }); + // `--network-bar-height` is 0 until the bar is up, so nothing below this + // line would mean anything without it. + await setOfflineAndWaitForBar(page, context); + + // Padding the flow as well would strand an empty strip under a feed that + // has already made room inside the panel the reader is on. + const { main, panel, bar } = await reservation(page); + expect(bar).not.toBe('0px'); + expect(panel).toBe(bar); + expect(main).toBe('0px'); + }); +}); + +test.describe('A 900px viewport at a 24px default font — with a mouse', () => { + test.use({ viewport: { width: 900, height: 800 } }); + // A phone project's finger is baked in at the project level, and this is the + // other device class — the one width alone used to misfile. + test.skip(({ hasTouch }) => hasTouch, 'needs a pointer that can hover'); + + test('gets the mobile header and the list it can actually drive', async ({ + page, + browserName, + }) => { + test.skip(browserName !== 'chromium', 'only Chromium can emulate the setting'); + await emulateBrowserFontSize(page, 24); + await setupApiMocks(page); + await page.goto('/#/'); + + // Same side of the line as the test above — this is the reader #249 would + // otherwise have handed a viewer with no way forward: a low-vision desktop + // reader on a large default font, whose 900px window is under 48rem. + expect(await mdMatches(page)).toEqual({ md: false, px: true }); + + // Shape followed the width: `md:` reveals the pill icons, and it agrees. + await expect(page.locator('header nav a svg').first()).not.toBeVisible(); + + // The tree followed the pointer. + await expect(page.getByTestId('swipe-container')).toHaveCount(0); + expect(page.url()).not.toMatch(ITEM_URL); + + // And it goes somewhere, which was the whole complaint. + await page.getByText('137 comments').first().click(); + await expect(page).toHaveURL(ITEM_URL); + await expect(page.getByRole('link', { name: TOP_STORY_TITLE }).first()).toBeVisible(); + }); + + test('keeps the strip the list would otherwise scroll under', async ({ + page, + context, + browserName, + }) => { + test.skip(browserName !== 'chromium', 'only Chromium can emulate the setting'); + await emulateBrowserFontSize(page, 24); + await setupApiMocks(page); + await page.goto('/#/'); + await expect(page.getByRole('link', { name: TOP_STORY_TITLE }).first()).toBeVisible(); + await setOfflineAndWaitForBar(page, context); + + // Whoever answers this has to answer for the tree, not the width: a width + // query skips a list mounted below `md:` and hides its last row behind the + // bar, which is where this rule used to live. + await expect + .poll(async () => { + const { main, bar } = await reservation(page); + return { main, bar, reserved: bar !== '0px' && main === bar }; + }) + .toMatchObject({ reserved: true }); + }); +}); + +test.describe('Where the rem line falls, either way', () => { + test.use({ viewport: { width: 900, height: 800 } }); + + test('a root font size set in CSS moves neither question', async ({ page }) => { + // `rem` in a media query is the browser's default, not the root element, + // so `html { font-size: 24px }` leaves `md:` where it was — and the hook + // has to ignore it too, or it would draw mobile chrome in desktop styling. + await enlargeRootFont(page, 24); + await setupApiMocks(page); + await page.goto('/#/'); + + expect( + await page.evaluate(() => ({ + root: getComputedStyle(document.documentElement).fontSize, + md: matchMedia('(min-width: 48rem)').matches, + })), + ).toEqual({ root: '24px', md: true }); + + await expect(page.getByRole('link', { name: TOP_STORY_TITLE }).first()).toBeVisible(); + expect(page.url()).not.toMatch(ITEM_URL); + }); + + test('a 12px default font puts a 620px viewport on the desktop side', async ({ + page, + browserName, + }) => { + test.skip(browserName !== 'chromium', 'only Chromium can emulate the setting'); + await emulateBrowserFontSize(page, 12); + await setupApiMocks(page); + await page.setViewportSize({ width: 620, height: 800 }); + await page.goto('/#/'); + + // The mirror of the first: 48rem is 576px here, so the px query is the + // one that would have said mobile. + expect( + await mdMatches(page), + 'this viewport no longer sits between the rem and px md breakpoints', + ).toEqual({ md: true, px: false }); + + await expect(page.getByRole('link', { name: TOP_STORY_TITLE }).first()).toBeVisible(); + expect(page.url()).not.toMatch(ITEM_URL); + + // And the header pays for what it draws: this is the band where `md:` put + // an icon in every pill that the packer had reserved nothing for. + await expect(page.locator('header nav a svg').first()).toBeVisible(); + }); +}); + +// index.html guesses the tree before React exists, to keep the first paint from +// carrying the other layout's scrollbar. A guess in different terms than the +// hook's is a guess that can be wrong, so it asks both of the same questions — +// and these tests hold it to that by cutting the app's own scripts, leaving the +// inline one as the only thing that could have added the class. +test.describe('The first paint, before React', () => { + test.describe('with a finger', () => { + test.use({ viewport: { width: 375, height: 667 }, hasTouch: true }); + + test('bets on swipe mode', async ({ page }) => { + await page.route('**/*.js', route => route.abort()); + await page.goto('/#/', { waitUntil: 'domcontentloaded' }); + + expect( + await page.evaluate(() => document.documentElement.classList.contains('swipe-mode')), + ).toBe(true); + }); + }); + + test.describe('with a finger, on a viewport only rem calls narrow', () => { + test.use({ viewport: { width: 900, height: 800 }, hasTouch: true }); + + test('bets on it there too', async ({ page, browserName }) => { + test.skip(browserName !== 'chromium', 'only Chromium can emulate the setting'); + await emulateBrowserFontSize(page, 24); + await page.route('**/*.js', route => route.abort()); + await page.goto('/#/', { waitUntil: 'domcontentloaded' }); + + // 48rem is 1152px at this setting, so `innerWidth < 768` said no here and + // handed the viewer a first paint with the list's scrollbar in it. + expect( + await page.evaluate(() => document.documentElement.classList.contains('swipe-mode')), + ).toBe(true); + }); + }); + + test.describe('with a mouse', () => { + test.use({ viewport: { width: 375, height: 667 } }); + test.skip(({ hasTouch }) => hasTouch, 'needs a pointer that can hover'); + + test('does not, however narrow the window', async ({ page }) => { + await page.route('**/*.js', route => route.abort()); + await page.goto('/#/', { waitUntil: 'domcontentloaded' }); + + // The old `innerWidth < 768` would have said yes here, then watched + // React mount a list into styling written for the viewer. + expect( + await page.evaluate(() => document.documentElement.classList.contains('swipe-mode')), + ).toBe(false); + }); + }); +}); diff --git a/e2e/navigation.spec.ts b/e2e/navigation.spec.ts index e30024f..ecf7823 100644 --- a/e2e/navigation.spec.ts +++ b/e2e/navigation.spec.ts @@ -1,13 +1,16 @@ import { test, expect, type Page } from '@playwright/test'; import { setupApiMocks } from './fixtures/api-mocks'; -import { getActiveSwipePanel, waitForScrollAtIndex, waitForSwipeReady } from './fixtures/swipe-helpers'; +import { + getActiveSwipePanel, + mountsSwipeViewer, + waitForScrollAtIndex, + waitForSwipeReady, +} from './fixtures/swipe-helpers'; const TOP_STORY_TITLE = 'Rust Is the Future of JavaScript Infrastructure'; async function expectTopStoryVisible(page: Page) { - const isMobileViewport = (page.viewportSize()?.width ?? 1280) < 768; - - if (isMobileViewport) { + if (await mountsSwipeViewer(page)) { await waitForSwipeReady(page, 1); await waitForScrollAtIndex(page, 0); await expect( @@ -16,6 +19,10 @@ async function expectTopStoryVisible(page: Page) { return; } + // A headline is a visible link in either tree, so this branch has to say + // which one it is in — otherwise a wrong answer above just quietly drops the + // panel waits and every assertion still passes. + await expect(page.getByTestId('swipe-container')).toHaveCount(0); await expect( page.getByRole('link', { name: TOP_STORY_TITLE }).first(), ).toBeVisible({ timeout: 10_000 }); @@ -32,8 +39,7 @@ test.describe('Navigation', () => { // On mobile, the swipe viewer auto-rewrites / → /item/. Wait for // this replace-navigation to complete so the subsequent click isn't // swallowed by WebKit during the transition (Mobile Safari flake). - const isMobile = (page.viewportSize()?.width ?? 1280) < 768; - if (isMobile) { + if (await mountsSwipeViewer(page)) { await page.waitForURL(/\/#\/item\/\d+/, { timeout: 10000 }); } @@ -56,8 +62,7 @@ test.describe('Navigation', () => { await page.goto('/#/'); // Same mobile stabilization as Show HN test above. - const isMobile = (page.viewportSize()?.width ?? 1280) < 768; - if (isMobile) { + if (await mountsSwipeViewer(page)) { await page.waitForURL(/\/#\/item\/\d+/, { timeout: 10000 }); } @@ -124,8 +129,9 @@ test.describe('Navigation', () => { test('navigates back to Top from other section', async ({ page }) => { await page.goto('/#/show'); + const isMobile = await mountsSwipeViewer(page); + // On mobile, wait for the show swipe-rewrite to settle before clicking. - const isMobile = (page.viewportSize()?.width ?? 1280) < 768; if (isMobile) { await page.waitForURL(/\/#\/item\/\d+/, { timeout: 10000 }); } @@ -159,7 +165,7 @@ test.describe('Navigation', () => { await page.goto('/#/'); await expectTopStoryVisible(page); - const isMobile = (page.viewportSize()?.width ?? 1280) < 768; + const isMobile = await mountsSwipeViewer(page); const bestLink = page.getByRole('link', { name: /best/i }); await bestLink.scrollIntoViewIfNeeded(); @@ -191,7 +197,7 @@ test.describe('Navigation', () => { }); test.describe('Navigation - Header Visibility', () => { - test.use({ viewport: { width: 375, height: 667 } }); + test.use({ viewport: { width: 375, height: 667 }, hasTouch: true }); test.beforeEach(async ({ page }) => { await setupApiMocks(page); @@ -206,6 +212,11 @@ test.describe('Navigation - Header Visibility', () => { }); test.describe('Navigation - Viewport Resize Transition', () => { + // The transition under test is the width one, so hand the desktop projects a + // finger: without it these viewports are a narrowed window, which now keeps + // the list on purpose. + test.use({ hasTouch: true }); + test.beforeEach(async ({ page }) => { await setupApiMocks(page); }); diff --git a/e2e/network-status-bar.spec.ts b/e2e/network-status-bar.spec.ts index 3e54568..199b26b 100644 --- a/e2e/network-status-bar.spec.ts +++ b/e2e/network-status-bar.spec.ts @@ -8,7 +8,8 @@ import { } from './fixtures/layout-helpers'; test.describe('Network Status Bar - Viewport Switch', () => { - test.use({ viewport: { width: 1280, height: 720 } }); + // Resizes into swipe mode, which takes a finger as well as a narrow window. + test.use({ viewport: { width: 1280, height: 720 }, hasTouch: true }); test.beforeEach(async ({ page }) => { await setupApiMocks(page); @@ -59,7 +60,8 @@ test.describe('Network Status Bar - Viewport Switch', () => { await context.setOffline(true); await expect(page.getByText('No internet connection')).toBeVisible({ timeout: 5000 }); - // Resize triggers swipe mode via the breakpoint hook. + // Narrowing crosses `useCanSwipe`'s width half; the finger the describe + // declares is the other half. await page.setViewportSize({ width: 375, height: 667 }); await page.waitForTimeout(500); diff --git a/e2e/touch-feel.spec.ts b/e2e/touch-feel.spec.ts index 7211fba..5ee638a 100644 --- a/e2e/touch-feel.spec.ts +++ b/e2e/touch-feel.spec.ts @@ -6,10 +6,12 @@ import { waitForSwipeReady } from './fixtures/swipe-helpers'; // that reloads the feed, selection handles eating a drag — and which nothing else // in the suite would notice disappearing. -const MOBILE = { width: 390, height: 780 }; +// A finger as well as a narrow viewport: the swipe viewer asks for both, so a +// narrowed desktop window is a desktop list and has none of this to defend. +const PHONE = { viewport: { width: 390, height: 780 }, hasTouch: true } as const; test.describe('Overscroll containment', () => { - test.use({ viewport: MOBILE }); + test.use(PHONE); test.beforeEach(async ({ page }) => { await setupApiMocks(page); @@ -55,7 +57,7 @@ test.describe('Overscroll containment', () => { }); test.describe('The gate iOS puts in front of :active', () => { - test.use({ viewport: MOBILE }); + test.use(PHONE); test('a passive touchstart listener is registered on the document', async ({ page, @@ -87,13 +89,11 @@ test.describe('The gate iOS puts in front of :active', () => { }); test.describe('Selection during a horizontal drag', () => { - test.use({ viewport: MOBILE }); + test.use(PHONE); test('a finger cannot start one on a text post headline, though its body still can', async ({ page, - hasTouch, }) => { - test.skip(!hasTouch, 'the lock is gated on a coarse pointer'); await setupApiMocks(page); // The Ask fixture carries `url: null`, so its title renders as bare text // instead of a link — the case where a long press raised the handles rather @@ -122,25 +122,6 @@ test.describe('Selection during a horizontal drag', () => { expect(fromHeadline, 'the headline must not be selectable').toBe(''); }); - // The mirror of the test above: same headline, same double-click, opposite - // expectation. The lock exists to keep a long press off a swipe, and a mouse - // has no swipe to take, so it must not pay for one. - test('a mouse keeps that headline selectable', async ({ page, hasTouch }) => { - test.skip(hasTouch, 'a device with a finger has a gesture to protect'); - await setupApiMocks(page); - await page.goto('/#/item/88888'); - await waitForSwipeReady(page); - - const headline = page.locator('.swipe-snap-panel.active h1').first(); - - await expect - .poll(async () => { - await headline.dblclick(); - return page.evaluate(() => window.getSelection()?.toString().length ?? 0); - }, { message: 'the headline must stay selectable for a mouse' }) - .toBeGreaterThan(0); - }); - test('a live selection is collapsed when the gesture locks horizontal', async ({ page }) => { await setupApiMocks(page); await page.goto('/#/'); @@ -212,3 +193,27 @@ test.describe('Selection during a horizontal drag', () => { expect(collapsedAtLock).toBe(true); }); }); + +// The mirror of the headline test above, and it has to look elsewhere for its +// headline: a mouse reader never reaches a swipe panel now, so the page whose +// selection they could lose is the desktop item page. Nothing there is behind +// the coarse-pointer rules, which is the point — this fails if they escape it. +test.describe('Selection with a mouse', () => { + test.use({ viewport: { width: 1280, height: 720 } }); + test.skip(({ hasTouch }) => hasTouch, 'a device with a finger has a gesture to protect'); + + test('a headline stays selectable on the page a mouse is given', async ({ page }) => { + await setupApiMocks(page); + await page.goto('/#/item/88888'); + + const headline = page.getByRole('heading', { level: 1 }).first(); + await expect(headline).toBeVisible(); + + await expect + .poll(async () => { + await headline.dblclick(); + return page.evaluate(() => window.getSelection()?.toString().length ?? 0); + }, { message: 'the headline must stay selectable for a mouse' }) + .toBeGreaterThan(0); + }); +}); diff --git a/e2e/user-submissions.spec.ts b/e2e/user-submissions.spec.ts index ac31bf8..efd36ca 100644 --- a/e2e/user-submissions.spec.ts +++ b/e2e/user-submissions.spec.ts @@ -284,8 +284,8 @@ test.describe('User Submissions - Mobile Swipe', () => { await expect(page.getByRole('button', { name: /try again/i })).toBeVisible(); // Confirm the error UI is rendered inside the swipe viewer's container, - // not the desktop list's wrapper. Catches a regression that flips the - // viewport branch. + // not the desktop list's wrapper. Catches a regression that flips + // `useCanSwipe`, on either of the two questions it asks. await expect(page.getByTestId('swipe-container')).toBeVisible(); }); }); diff --git a/index.html b/index.html index 939b38f..a41b8f4 100644 --- a/index.html +++ b/index.html @@ -100,13 +100,15 @@