Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions e2e/domain-filter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand Down
14 changes: 14 additions & 0 deletions e2e/fixtures/swipe-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> {
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,
Expand Down
10 changes: 5 additions & 5 deletions e2e/header-overflow.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)/);
});

Expand Down
78 changes: 78 additions & 0 deletions e2e/layout-tokens.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<main>` 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. `<main>` 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 `<main>`'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);
});
});
244 changes: 244 additions & 0 deletions e2e/mobile-breakpoint.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
Loading