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
29 changes: 21 additions & 8 deletions e2e/comment-detail.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ test.describe('Comment Detail - Error + Retry', () => {
// Auto-retry exhausts 3 backoff attempts (2s+4s+8s) before surfacing error UI.
test.setTimeout(60000);

test('shows error state with retry button when Algolia fails', async ({ page }) => {
test('keeps the comment and offers to retry its replies when Algolia fails', async ({ page }) => {
await setupApiMocks(page);

await page.route(`${ALGOLIA_API}/items/1001`, async (route) => {
Expand All @@ -106,12 +106,18 @@ test.describe('Comment Detail - Error + Retry', () => {

await page.goto('/#/item/1001');

// The comment itself comes from Firebase.
const firebaseText = page.getByText(/really well-written piece/);
await expect(firebaseText).toBeVisible();

// 30s timeout covers the 2+4+8 backoff plus slack.
await expect(page.getByText('Failed to load comment')).toBeVisible({ timeout: 30000 });
await expect(page.getByRole('button', { name: /retry/i })).toBeVisible();
await expect(page.getByRole('button', { name: /retry/i })).toBeVisible({ timeout: 30000 });
await expect(page.getByText('Failed to load replies')).toBeVisible();
await expect(firebaseText).toBeVisible();
await expect(page.getByText('Failed to load comment')).not.toBeVisible();
});

test('recovers after retry when Algolia temporarily fails', async ({ page }) => {
test('loads the replies on retry once Algolia recovers', async ({ page }) => {
await setupApiMocks(page);

let shouldFail = true;
Expand All @@ -127,7 +133,14 @@ test.describe('Comment Detail - Error + Retry', () => {
created_at_i: Math.floor(Date.now() / 1000) - 1800,
parent_id: 12345,
story_id: 12345,
children: [],
children: [{
id: 2001,
author: 'tptacek',
text: 'A reply that loaded on retry.',
created_at_i: Math.floor(Date.now() / 1000) - 900,
parent_id: 1001,
children: [],
}],
type: 'comment',
},
});
Expand All @@ -136,15 +149,15 @@ test.describe('Comment Detail - Error + Retry', () => {

await page.goto('/#/item/1001');

await expect(page.getByText('Failed to load comment')).toBeVisible({ timeout: 30000 });
const retryButton = page.getByRole('button', { name: /retry/i });
await expect(retryButton).toBeVisible();
await expect(retryButton).toBeVisible({ timeout: 30000 });

// Flip the failure switch BEFORE the retry click so the next request succeeds.
shouldFail = false;
await retryButton.click();

await expect(page.getByText('patio11').first()).toBeVisible({ timeout: 10000 });
await expect(page.getByText('A reply that loaded on retry.')).toBeVisible({ timeout: 10000 });
await expect(page.getByText(/wasm-bindgen/i).first()).toBeVisible();
await expect(retryButton).not.toBeVisible();
});
});
6 changes: 2 additions & 4 deletions e2e/story-browsing.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,12 @@ test.describe('Item Browsing - Desktop Back between feeds', () => {
await expect(page).toHaveURL(/\/show$/);
await expect(page.locator('[data-story-id="99999"]')).toBeVisible();

// The list stays mounted, so Back to Best resets it rather than remounting.
// Best's list mounts again and restores the session the story saved.
await page.goBack();
await expect(page).toHaveURL(/\/best$/);
// reset() drops the session and re-arms hasMore, so the end marker only
// returns once the load after it finishes.
await expect.poll(() => page.evaluate(() => sessionStorage.getItem('feed:session:best'))).toBeNull();
await expect(page.getByText("You've reached the end")).toBeVisible();
await expect(cards).toHaveCount(3);
expect(await page.evaluate(() => sessionStorage.getItem('feed:session:best'))).not.toBeNull();
});
});

Expand Down
83 changes: 74 additions & 9 deletions src/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { describe, it, expect, beforeEach, vi } from 'vitest';
import { screen, fireEvent } from '@testing-library/react';
import { Routes, Route, Link, useNavigate } from 'react-router';
import { render } from './test/test-utils';
import { MobileItemDetailWrapper, MobileStoryListWrapper } from './App';
import { MobileStoryRoute } from './App';
import { fetchItemOnly } from './api/hn';
import { saveSwipePosition } from './utils/swipePosition';
import { createStoryItem } from './test/factories';
Expand All @@ -16,18 +16,21 @@ vi.mock('./hooks/useCanSwipe', () => ({ useCanSwipe: () => true }));
// mounts (and with what props) without their data hooks / async state.
vi.mock('./components/SwipeStoryViewer', async () => {
const { useState } = await import('react');
let mounts = 0;
return {
SwipeStoryViewer: function SwipeStoryViewer({ type, initialItemId }: { type: string; initialItemId?: string }) {
// Frozen at mount: a reused instance keeps the feed and item it was mounted for.
const [mountedType] = useState(type);
const [mountedItem] = useState(initialItemId ?? '');
const [mount] = useState(() => ++mounts);
return (
<div
data-testid="feed-viewer"
data-type={type}
data-mounted-type={mountedType}
data-item={initialItemId ?? ''}
data-mounted-item={mountedItem}
data-mount={mount}
/>
);
},
Expand Down Expand Up @@ -55,7 +58,7 @@ vi.mock('./api/hn', async (importOriginal) => ({
function renderWrapper(id: number | string, state?: LocationState) {
return render(
<Routes>
<Route path="/item/:id" element={<MobileItemDetailWrapper />} />
<Route path="/item/:id" element={<MobileStoryRoute />} />
</Routes>,
{ initialEntries: [{ pathname: `/item/${id}`, state }] },
);
Expand All @@ -66,7 +69,7 @@ function seedSnapshot(viewer: LocationState, storyId: number) {
saveSwipePosition({ viewer, storyId, index: 0, scrollY: 0, stories });
}

describe('MobileItemDetailWrapper — viewer recovery (stateless reload)', () => {
describe('MobileStoryRoute — viewer recovery (stateless reload)', () => {
beforeEach(() => {
sessionStorage.clear();
});
Expand Down Expand Up @@ -150,7 +153,7 @@ describe('MobileItemDetailWrapper — viewer recovery (stateless reload)', () =>
});
});

describe('MobileItemDetailWrapper — direct link', () => {
describe('MobileStoryRoute — direct link', () => {
beforeEach(() => {
sessionStorage.clear();
});
Expand All @@ -169,7 +172,7 @@ describe('MobileItemDetailWrapper — direct link', () => {
vi.mocked(fetchItemOnly).mockResolvedValueOnce(createStoryItem({ id: 88888 }));
render(
<Routes>
<Route path="/item/:id" element={<><MobileItemDetailWrapper /><SwipeToNextStory /></>} />
<Route path="/item/:id" element={<><MobileStoryRoute /><SwipeToNextStory /></>} />
</Routes>,
{ initialEntries: ['/item/88888'] },
);
Expand All @@ -185,7 +188,7 @@ describe('MobileItemDetailWrapper — direct link', () => {
});
});

describe('MobileItemDetailWrapper — ids that are not numbers', () => {
describe('MobileStoryRoute — ids that are not numbers', () => {
it.each(['abc', '1.5', '-1'])('shows "Item not found" for /item/%s instead of a feed', (id) => {
renderWrapper(id, { from: 'top' });

Expand All @@ -196,13 +199,42 @@ describe('MobileItemDetailWrapper — ids that are not numbers', () => {
});
});

describe('MobileStoryListWrapper', () => {
describe('MobileStoryRoute — feed routes', () => {
beforeEach(() => {
sessionStorage.clear();
});

// What the feed's viewer does once its first story shows.
function MoveToFirstStory({ feed }: { feed: LocationState['from'] }) {
const navigate = useNavigate();
return (
<button onClick={() => void navigate('/item/1000', { replace: true, state: { from: feed } })}>
first story
</button>
);
}

function renderFeedRoutes() {
return render(
<>
<Routes>
<Route path="/" element={<MobileStoryRoute feed="top" />} />
<Route path="/best" element={<MobileStoryRoute feed="best" />} />
<Route path="/item/:id" element={<MobileStoryRoute />} />
</Routes>
<MoveToFirstStory feed="best" />
<Link to="/best">best</Link>
</>,
{ initialEntries: ['/best'] },
);
}

it('mounts a new viewer when you switch feeds before the URL moves to a story', () => {
render(
<>
<Routes>
<Route path="/" element={<MobileStoryListWrapper type="top" />} />
<Route path="/best" element={<MobileStoryListWrapper type="best" />} />
<Route path="/" element={<MobileStoryRoute feed="top" />} />
<Route path="/best" element={<MobileStoryRoute feed="best" />} />
</Routes>
<Link to="/best">best</Link>
</>,
Expand All @@ -215,4 +247,37 @@ describe('MobileStoryListWrapper', () => {
expect(viewer).toHaveAttribute('data-type', 'best');
expect(viewer).toHaveAttribute('data-mounted-type', 'best');
});

it('keeps the viewer when it moves the URL to its first story', () => {
renderFeedRoutes();
const mount = screen.getByTestId('feed-viewer').getAttribute('data-mount');

fireEvent.click(screen.getByRole('button', { name: 'first story' }));

const viewer = screen.getByTestId('feed-viewer');
expect(viewer).toHaveAttribute('data-item', '1000');
expect(viewer).toHaveAttribute('data-mounted-item', '');
expect(viewer).toHaveAttribute('data-mount', mount);
});

it('starts the feed over when its tab is opened from one of its stories', () => {
renderFeedRoutes();
fireEvent.click(screen.getByRole('button', { name: 'first story' }));
const mount = screen.getByTestId('feed-viewer').getAttribute('data-mount');

fireEvent.click(screen.getByRole('link', { name: 'best' }));

const viewer = screen.getByTestId('feed-viewer');
expect(viewer).toHaveAttribute('data-item', '');
expect(viewer).not.toHaveAttribute('data-mount', mount);
});

it('keeps the viewer when its tab is opened again before the URL moves', () => {
renderFeedRoutes();
const mount = screen.getByTestId('feed-viewer').getAttribute('data-mount');

fireEvent.click(screen.getByRole('link', { name: 'best' }));

expect(screen.getByTestId('feed-viewer')).toHaveAttribute('data-mount', mount);
});
});
Loading
Loading