diff --git a/e2e/domain-filter.spec.ts b/e2e/domain-filter.spec.ts index f9d675d..7e81ca9 100644 --- a/e2e/domain-filter.spec.ts +++ b/e2e/domain-filter.spec.ts @@ -7,6 +7,10 @@ import { smoothScrollAndAwaitSettled, waitForScrollAtIndex, } from './fixtures/swipe-helpers'; +import { + expectLocatorCenteredBetweenChrome, + setOfflineAndWaitForBar, +} from './fixtures/layout-helpers'; test.describe('Domain Filter', () => { test.beforeEach(async ({ page }) => { @@ -58,6 +62,28 @@ test.describe('Domain Filter - Edge Cases', () => { }); }); +test.describe('Domain Filter - Centered state layouts', () => { + test.use({ viewport: { width: 1280, height: 720 }, deviceScaleFactor: 1 }); + + test.beforeEach(async ({ page }) => { + await setupApiMocks(page); + }); + + test('keeps the empty domain state centered with the offline bar visible', async ({ page, context }) => { + await stubEmptyDomainSearch(page, 'unknown-domain.xyz'); + + await page.goto('/#/from/unknown-domain.xyz'); + + const stateRoot = page.getByRole('heading', { + name: 'No submissions found from "unknown-domain.xyz"', + }).locator('xpath=..'); + await expect(stateRoot).toBeVisible(); + + await setOfflineAndWaitForBar(page, context); + await expectLocatorCenteredBetweenChrome(stateRoot); + }); +}); + test.describe('Domain Filter - Header "from" button', () => { // Scoped to desktop viewport because the "stays active after opening a // story" and "browser back restores the list" tests below click @@ -167,11 +193,13 @@ test.describe('Domain Filter - Header "from" button', () => { }); test.describe('Domain Filter - Empty domain', () => { + test.use({ viewport: { width: 1280, height: 720 }, deviceScaleFactor: 1 }); + test.beforeEach(async ({ page }) => { await setupApiMocks(page); }); - test('shows error with link to home when domain is empty', async ({ page }) => { + test('shows error with link to home when domain is empty', async ({ page, context }) => { await page.goto('/#/from/'); // Document title should fall back to default @@ -180,6 +208,14 @@ test.describe('Domain Filter - Empty domain', () => { // Should show error message await expect(page.getByText('No domain specified')).toBeVisible(); + await setOfflineAndWaitForBar(page, context); + + // `DomainStories` uses the shared `page-state-center-padded` helper. + const stateRoot = page.getByRole('heading', { + name: 'No domain specified', + }).locator('xpath=..'); + await expectLocatorCenteredBetweenChrome(stateRoot); + // Should have a link back to home that works const homeLink = page.getByRole('link', { name: /Back to Home/i }); await expect(homeLink).toBeVisible(); diff --git a/e2e/fixtures/layout-helpers.ts b/e2e/fixtures/layout-helpers.ts new file mode 100644 index 0000000..f6da9ab --- /dev/null +++ b/e2e/fixtures/layout-helpers.ts @@ -0,0 +1,90 @@ +import type { BrowserContext, Locator, Page } from '@playwright/test'; +import { expect } from '@playwright/test'; + +interface CenteredBetweenChromeOptions { + tolerance?: number; + topSelector?: string; + bottomSelector?: string; +} + +/** + * Force the app into its offline-bar state after the route has already loaded. + * This lets geometry assertions include the fixed bottom chrome without making + * the page's initial data fetch nondeterministic. + */ +export async function setOfflineAndWaitForBar(page: Page, context: BrowserContext) { + await context.setOffline(true); + await page.evaluate(() => { + Object.defineProperty(navigator, 'onLine', { + value: false, + writable: true, + configurable: true, + }); + window.dispatchEvent(new Event('offline')); + }); + + await expect(page.getByText('No internet connection')).toBeVisible({ timeout: 5000 }); + + await page.waitForFunction(() => { + const bar = document.querySelector('.network-status-bar'); + if (!bar) return false; + + const rect = bar.getBoundingClientRect(); + return Math.abs(rect.bottom - window.innerHeight) <= 2; + }, { timeout: 5000 }); +} + +/** + * Assert that a block is centered in the usable viewport between the fixed + * header and the optional fixed bottom bar. + */ +export async function expectLocatorCenteredBetweenChrome( + locator: Locator, + { + tolerance = 8, + topSelector = 'header', + bottomSelector = '.network-status-bar', + }: CenteredBetweenChromeOptions = {}, +) { + await expect(locator).toBeVisible(); + + const geometry = await locator.evaluate((el, selectors) => { + const childRects = Array.from(el.children) + .map((child) => child.getBoundingClientRect()) + .filter((rect) => rect.width > 0 && rect.height > 0); + + const rect = childRects.length > 0 + ? childRects.reduce( + (bounds, childRect) => ({ + top: Math.min(bounds.top, childRect.top), + right: Math.max(bounds.right, childRect.right), + bottom: Math.max(bounds.bottom, childRect.bottom), + left: Math.min(bounds.left, childRect.left), + }), + { + top: childRects[0].top, + right: childRects[0].right, + bottom: childRects[0].bottom, + left: childRects[0].left, + }, + ) + : el.getBoundingClientRect(); + + const topBoundary = document.querySelector(selectors.topSelector)?.getBoundingClientRect().bottom ?? 0; + const bottomBoundary = document.querySelector(selectors.bottomSelector)?.getBoundingClientRect().top ?? window.innerHeight; + const expectedCenterX = window.innerWidth / 2; + const expectedCenterY = topBoundary + ((bottomBoundary - topBoundary) / 2); + const width = 'width' in rect ? rect.width : rect.right - rect.left; + const height = 'height' in rect ? rect.height : rect.bottom - rect.top; + const centerX = rect.left + (width / 2); + const centerY = rect.top + (height / 2); + + return { + deltaX: Math.abs(centerX - expectedCenterX), + deltaY: Math.abs(centerY - expectedCenterY), + }; + }, { topSelector, bottomSelector }); + + expect(geometry.deltaX).toBeLessThanOrEqual(tolerance); + expect(geometry.deltaY).toBeLessThanOrEqual(tolerance); +} \ No newline at end of file diff --git a/e2e/network-status-bar.spec.ts b/e2e/network-status-bar.spec.ts index 100f9cf..14f7775 100644 --- a/e2e/network-status-bar.spec.ts +++ b/e2e/network-status-bar.spec.ts @@ -2,6 +2,10 @@ import { test, expect } from '@playwright/test'; import { test as fixtureTest } from './fixtures/test'; import { setupApiMocks } from './fixtures/api-mocks'; import { smoothScrollAndAwaitSettled } from './fixtures/swipe-helpers'; +import { + expectLocatorCenteredBetweenChrome, + setOfflineAndWaitForBar, +} from './fixtures/layout-helpers'; test.describe('Network Status Bar - Viewport Switch', () => { test.use({ viewport: { width: 1280, height: 720 } }); @@ -126,6 +130,79 @@ fixtureTest.describe('Network Status Bar - Offline Item Detail to Mobile', () => }); }); +test.describe('Network Status Bar - Centered state layouts', () => { + test.use({ viewport: { width: 1280, height: 720 }, deviceScaleFactor: 1 }); + + test.beforeEach(async ({ page }) => { + await setupApiMocks(page); + }); + + test('keeps the full-page not-found state centered with the offline bar visible', async ({ page, context }) => { + await page.goto('/#/missing-route'); + + const stateRoot = page.getByRole('heading', { + name: 'Lost in the feed', + }).locator('xpath=..'); + await expect(stateRoot).toBeVisible(); + + await setOfflineAndWaitForBar(page, context); + await expectLocatorCenteredBetweenChrome(stateRoot); + }); +}); + +test.describe('Network Status Bar - Mobile content spacing', () => { + test.use({ viewport: { width: 375, height: 667 }, hasTouch: true }); + + test.beforeEach(async ({ page }) => { + await setupApiMocks(page); + }); + + test('keeps the story title gap stable when the offline bar appears', async ({ page, context }) => { + await page.goto('/#/item/12345'); + + const title = page.getByRole('heading', { level: 1 }).first(); + await expect(title).toBeVisible(); + await page.waitForFunction(() => { + const heading = document.querySelector('h1'); + const stage = heading?.closest('.page-stage'); + return !!stage + && stage.classList.contains('fade-skeleton') + && !stage.classList.contains('play-real'); + }); + + const beforeGap = await page.evaluate(() => { + const heading = document.querySelector('h1'); + const header = document.querySelector('header'); + if (!(heading instanceof HTMLElement) || !(header instanceof HTMLElement)) return null; + return heading.getBoundingClientRect().top - header.getBoundingClientRect().bottom; + }); + + expect(beforeGap).not.toBeNull(); + + await setOfflineAndWaitForBar(page, context); + + const afterMetrics = await page.evaluate(() => { + const heading = document.querySelector('h1'); + const header = document.querySelector('header'); + const panel = document.querySelector('[data-testid="swipe-panel"].active') ?? document.querySelector('[data-testid="swipe-panel"]'); + if (!(heading instanceof HTMLElement) || !(header instanceof HTMLElement) || !(panel instanceof HTMLElement)) { + return null; + } + + return { + gap: heading.getBoundingClientRect().top - header.getBoundingClientRect().bottom, + paddingTop: getComputedStyle(panel).paddingTop, + paddingBottom: getComputedStyle(panel).paddingBottom, + }; + }); + + expect(afterMetrics).not.toBeNull(); + expect(afterMetrics?.paddingTop).toBe('53px'); + expect(afterMetrics?.paddingBottom).toBe('32px'); + expect(Math.abs((afterMetrics?.gap ?? 0) - (beforeGap ?? 0))).toBeLessThanOrEqual(2); + }); +}); + test.describe('Swipe viewer - offline end-of-feed behavior', () => { test.use({ viewport: { width: 375, height: 667 }, hasTouch: true }); diff --git a/e2e/user-submissions.spec.ts b/e2e/user-submissions.spec.ts index e34aad1..ac31bf8 100644 --- a/e2e/user-submissions.spec.ts +++ b/e2e/user-submissions.spec.ts @@ -7,6 +7,10 @@ import { smoothScrollAndAwaitSettled, waitForScrollAtIndex, } from './fixtures/swipe-helpers'; +import { + expectLocatorCenteredBetweenChrome, + setOfflineAndWaitForBar, +} from './fixtures/layout-helpers'; test.describe('User Submissions - Desktop', () => { // Desktop viewport — `MobileUserSubmissionsWrapper` falls through to the @@ -247,7 +251,7 @@ test.describe('User Submissions - Mobile Swipe', () => { ), { timeout: 5_000 }).toBe(1); }); - test('shows empty state for a user with no submissions on mobile', async ({ page }) => { + test('shows empty state for a user with no submissions on mobile', async ({ page, context }) => { await stubEmptyUserSubmissions(page, 'noresults'); await page.goto('/#/submitted/noresults'); @@ -255,6 +259,14 @@ test.describe('User Submissions - Mobile Swipe', () => { // The mobile swipe viewer's empty branch surfaces the same copy as the // desktop list (centralized in `formatNoUserSubmissionsTitle`). await expect(page.getByText('No submissions found by "noresults"')).toBeVisible({ timeout: 10_000 }); + + await setOfflineAndWaitForBar(page, context); + + // Mobile empty states use the shared swipe centering helper. + const stateRoot = page.getByRole('heading', { + name: 'No submissions found by "noresults"', + }).locator('xpath=..'); + await expectLocatorCenteredBetweenChrome(stateRoot); }); test('shows error state when Algolia returns 503 on mobile swipe', async ({ page }) => { diff --git a/src/components/InfiniteStoryListPage.test.tsx b/src/components/InfiniteStoryListPage.test.tsx index a4b3e87..9d1a3fe 100644 --- a/src/components/InfiniteStoryListPage.test.tsx +++ b/src/components/InfiniteStoryListPage.test.tsx @@ -119,7 +119,10 @@ describe('InfiniteStoryListPage', () => { emptyTitle: 'No stories from this domain', }); - expect(screen.getByText('No stories from this domain')).toBeInTheDocument(); + const title = screen.getByText('No stories from this domain'); + + expect(title).toBeInTheDocument(); + expect(title.closest('.page-state-center-padded')).not.toBeNull(); }); it('omits the empty StateView when emptyTitle is undefined', () => { @@ -140,9 +143,12 @@ describe('InfiniteStoryListPage', () => { result: makeResult({ error: 'Network down' }), }); - expect(screen.getByText('Failed to load items')).toBeInTheDocument(); + const title = screen.getByText('Failed to load items'); + + expect(title).toBeInTheDocument(); expect(screen.getByText('Network down')).toBeInTheDocument(); expect(screen.getByRole('button', { name: 'Try Again' })).toBeInTheDocument(); + expect(title.closest('.page-state-center-padded')).not.toBeNull(); }); it('renders the cold-error StateView with an overridden failTitle', () => { diff --git a/src/components/InfiniteStoryListPage.tsx b/src/components/InfiniteStoryListPage.tsx index 4f359d2..28c2bf2 100644 --- a/src/components/InfiniteStoryListPage.tsx +++ b/src/components/InfiniteStoryListPage.tsx @@ -86,13 +86,12 @@ export function InfiniteStoryListPage({ // layout (no skeleton overlay), so they sit above the wrapper. if (error && stories.length === 0 && !isRetrying) { return ( -