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
38 changes: 37 additions & 1 deletion e2e/domain-filter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand Down
90 changes: 90 additions & 0 deletions e2e/fixtures/layout-helpers.ts
Original file line number Diff line number Diff line change
@@ -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);
}
77 changes: 77 additions & 0 deletions e2e/network-status-bar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } });
Expand Down Expand Up @@ -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 });

Expand Down
14 changes: 13 additions & 1 deletion e2e/user-submissions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -247,14 +251,22 @@ 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');

// 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 }) => {
Expand Down
10 changes: 8 additions & 2 deletions src/components/InfiniteStoryListPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand Down
Loading