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/fixtures/api-mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,8 @@ export async function setupApiMocks(page: Page) {
} else if (tags.includes('front_page')) {
hits = [mockAlgoliaItem1, mockAlgoliaItem2, mockAlgoliaItem3];
nbHits = 3;
} else if (tags === 'story') {
// Day-based pagination (tags=story + numericFilters) — distinct items
} else if (tags === '(story,poll)') {
// Day-based pagination (tags=(story,poll) + numericFilters) — distinct items
// from the 'front_page' branch above so pagination is observable.
hits = [mockPaginationItem1, mockPaginationItem2, mockPaginationItem3];
nbHits = 3;
Expand Down Expand Up @@ -445,7 +445,7 @@ export async function setupApiMocks(page: Page) {

// Algolia: Search by date endpoint. Two callers share this URL:
// - `useDomainInfiniteStories` issues `?query=<domain>` (no `tags`)
// - `useUserInfiniteStories` issues `?tags=story,author_<username>` (no `query`)
// - `useUserInfiniteStories` issues `?tags=(story,poll),author_<username>` (no `query`)
// The author branch is matched FIRST so the absence of a `tags` substring
// can never be mis-routed to the domain branch.
await page.route(`${ALGOLIA_API}/search_by_date*`, async (route) => {
Expand Down
2 changes: 1 addition & 1 deletion e2e/fixtures/mock-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ export const mockUserProfile = {
};

// User-submitted stories (Algolia format) returned by the
// `tags=story,author_pg` branch of `search_by_date`. Distinct objectIDs (66666,
// `tags=(story,poll),author_pg` branch of `search_by_date`. Distinct objectIDs (66666,
// 66667) so tests asserting URL rewrites or back-navigation can pin specific
// indices without colliding with top/best/show/ask/domain mock IDs.
export const mockUserStory1 = {
Expand Down
56 changes: 45 additions & 11 deletions e2e/item-detail.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { test, expect } from '@playwright/test';
import { createFirebaseWsHandler, FIREBASE_WS_PATTERN, setupApiMocks } from './fixtures/api-mocks';
import { expectLocatorCenteredBetweenChrome, setOfflineAndWaitForBar } from './fixtures/layout-helpers';

test.describe('Item Detail', () => {
test.beforeEach(async ({ page }) => {
Expand Down Expand Up @@ -267,26 +266,61 @@ test.describe('Item Detail - Desktop', () => {
});
});

const pollItems = {
126809: { id: 126809, title: 'Poll: Ban Polls?', by: 'pg', score: 46, time: 1204403652, descendants: 0, parts: [126810, 126811], type: 'poll' },
126810: { id: 126810, text: 'Yes, ban them', by: 'pg', score: 30, time: 1204403652, poll: 126809, type: 'pollopt' },
126811: { id: 126811, text: 'No, leave them', by: 'pg', score: 10, time: 1204403652, poll: 126809, type: 'pollopt' },
};

test.describe('Item Detail - Poll', () => {
test.beforeEach(async ({ page }) => {
await setupApiMocks(page);
// Registered last, so it wins over setupApiMocks' handler.
await page.routeWebSocket(FIREBASE_WS_PATTERN, createFirebaseWsHandler({ itemOverrides: pollItems }));
});

test("lists a poll's options with their points and shares", async ({ page }) => {
await page.goto('/#/item/126809');

await expect(page.getByRole('heading', { level: 1, name: 'Poll: Ban Polls?' })).toBeVisible({ timeout: 10_000 });
const options = page.getByRole('region', { name: 'Poll options' }).getByRole('listitem');
await expect(options).toHaveText([/Yes, ban them\s*30 points · 75%/, /No, leave them\s*10 points · 25%/]);
});

test('shows its options when its comments fail to load', async ({ page }) => {
await page.route('**/search*', async (route) => {
const tags = new URL(route.request().url()).searchParams.get('tags') ?? '';
if (tags.includes('story_126809')) {
await route.fulfill({ status: 503, json: { message: 'Service temporarily unavailable' } });
return;
}
await route.fallback();
});

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

await expect(page.getByRole('region', { name: 'Poll options' }).getByRole('listitem')).toHaveCount(2, { timeout: 10_000 });
});
});

test.describe('Item Detail - Poll option on a phone', () => {
test.use({ viewport: { width: 375, height: 667 }, hasTouch: true });

test.beforeEach(async ({ page }) => {
await setupApiMocks(page);
// Registered last, so it wins over setupApiMocks' handler.
await page.routeWebSocket(FIREBASE_WS_PATTERN, createFirebaseWsHandler({
itemOverrides: {
126810: { id: 126810, text: 'Yes, ban them', by: 'pg', score: 30, time: 1204403652, poll: 126809, type: 'pollopt' },
},
}));
await page.routeWebSocket(FIREBASE_WS_PATTERN, createFirebaseWsHandler({ itemOverrides: pollItems }));
});

test('centers the notice between the header and the offline bar', async ({ page, context }) => {
// The swipe viewer only holds stories, so a poll option opens as a page.
test('opens as a page that links to its poll', async ({ page }) => {
await page.goto('/#/item/126810');
const heading = page.getByRole('heading', { name: "Poll options aren't supported" });
await expect(heading).toBeVisible({ timeout: 10_000 });

await setOfflineAndWaitForBar(page, context);
await expect(page.getByText('Yes, ban them', { exact: true })).toBeVisible({ timeout: 10_000 });
await expect(page.getByTestId('swipe-container')).toHaveCount(0);

await expectLocatorCenteredBetweenChrome(heading.locator('xpath=..'));
await page.getByRole('link', { name: 'Poll: Ban Polls?' }).click();
await expect(page).toHaveURL(/\/item\/126809$/);
await expect(page.getByRole('region', { name: 'Poll options' }).getByRole('listitem')).toHaveCount(2);
});
});
2 changes: 1 addition & 1 deletion e2e/mobile-swipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,7 @@ test.describe('Mobile Swipe Viewer - Back past the cached page', () => {
async function goBackToStory21(page: Page) {
await page.route(`${ALGOLIA_API}/search*`, async (route) => {
const tags = new URL(route.request().url()).searchParams.get('tags');
if (tags === 'story') {
if (tags === '(story,poll)') {
await route.abort();
} else {
await route.fallback();
Expand Down
17 changes: 9 additions & 8 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,10 @@ export function MobileStoryRoute({ feed }: { feed?: FeedType }) {
// Branch 5: Direct URL (no state) → resolve type first. The story viewer
// must be the element Branch 4 renders for `{ from: 'top' }`: the first
// swipe writes that state, and a different element there would remount
// the viewer and drop the story this link opened. A job has no comments
// to swipe through and no feed lists it, so it gets the item page.
if (resolvedType === 'job') return <ItemDetail key={id} />;
// the viewer and drop the story this link opened. A job or poll option has
// no comments to swipe through and no feed lists it, so it gets the item
// page.
if (resolvedType === 'page') return <ItemDetail key={id} />;
if (resolvedType === 'story') return renderSwipeViewer({ from: 'top' }, id, feedEntries);
return <MobileItemResolverSkeleton />;
}
Expand Down Expand Up @@ -180,10 +181,10 @@ function ItemNotFound() {
}

// Resolves item type for a direct URL hit on mobile (no `location.state`):
// a comment re-routes to Branch 1, a job reports `'job'`, and anything else
// reports `'story'` so the wrapper mounts the story viewer.
function useResolveDirectLink(id: string | undefined, enabled: boolean): 'story' | 'job' | null {
const [resolved, setResolved] = useState<{ id: string; type: 'story' | 'job' } | null>(null);
// a comment re-routes to Branch 1, a job or poll option reports `'page'`, and
// anything else reports `'story'` so the wrapper mounts the story viewer.
function useResolveDirectLink(id: string | undefined, enabled: boolean): 'story' | 'page' | null {
const [resolved, setResolved] = useState<{ id: string; type: 'story' | 'page' } | null>(null);
const navigate = useNavigate();

useEffect(() => {
Expand All @@ -197,7 +198,7 @@ function useResolveDirectLink(id: string | undefined, enabled: boolean): 'story'
// Navigate with state so MobileStoryRoute Branch 1 picks it up
void navigate(`/item/${id}`, { replace: true, state: { isComment: true } });
} else {
setResolved({ id, type: item.type === 'job' ? 'job' : 'story' });
setResolved({ id, type: item.type === 'job' || item.type === 'pollopt' ? 'page' : 'story' });
}
})
.catch(() => {
Expand Down
Loading