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
18 changes: 18 additions & 0 deletions e2e/mobile-swipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,24 @@ test.describe('Mobile Swipe Viewer', () => {
expect(ids.filter((id) => mockTopItemIds.includes(id))).toEqual([]);
});

test('a feed opened from its tab starts at the top, not where the last story was scrolled to', async ({ page }) => {
await page.goto('/#/');
await waitForSwipeReady(page, 6);
await expect(page).toHaveURL(/\/item\/12345/, { timeout: 5000 });

// Both stories taller than the viewport, so a short one can't clamp the
// page back to the top on its own.
await page.addStyleTag({ content: '[data-testid="swipe-panel"].active { min-height: 3000px !important; }' });
await page.evaluate(() => window.scrollTo({ top: 1000, behavior: 'instant' }));
await expect.poll(() => page.evaluate(() => window.scrollY)).toBe(1000);

await page.getByRole('link', { name: 'best', exact: true }).click();

await expect(page).toHaveURL(/\/item\/33001/, { timeout: 5000 });
await expect(getActiveSwipePanel(page)).toHaveAttribute('data-item-id', '33001');
await expect.poll(() => page.evaluate(() => window.scrollY)).toBe(0);
});

test('a feed refresh that lands after a swipe keeps the story on screen', async ({ page }) => {
// An hour-old cached Top feed in the reverse of the live order, and Firebase
// reads that take 1.5s, so the refresh lands after the swipe.
Expand Down
20 changes: 20 additions & 0 deletions src/components/SwipeStoryViewer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,8 @@ describe('SwipeStoryViewerCore — swipe-position restore', () => {
document.dispatchEvent(new Event('visibilitychange'));
});
};
// Opening the feed scrolled to the top already; only the restore counts.
vi.mocked(window.scrollTo).mockClear();
try {
showTab('hidden');
showTab('visible');
Expand Down Expand Up @@ -591,6 +593,24 @@ describe('SwipeStoryViewerCore — swipe-position restore', () => {
expect(readSwipePosition({ viewer: { from: 'best' } })).toBeNull();
expect(readSwipePosition({ viewer: { from: 'top' } })?.storyId).toBe(2);
});

it('opens a feed from its tab at the top of the page', () => {
const stories = [createStoryItem({ id: 1 }), createStoryItem({ id: 2 })];

render(<SwipeStoryViewerCore {...baseProps} stories={stories} backState={{ from: 'best' }} />);

expect(window.scrollTo).toHaveBeenCalledWith(0, 0);
});

it('leaves the page where it is when the viewer opens on a story', () => {
const stories = [createStoryItem({ id: 1 }), createStoryItem({ id: 2 })];

render(
<SwipeStoryViewerCore {...baseProps} stories={stories} initialItemId="1" backState={{ from: 'best' }} />,
);

expect(window.scrollTo).not.toHaveBeenCalled();
});
});

describe('SwipeStoryViewerCore — end-of-feed panel', () => {
Expand Down
6 changes: 6 additions & 0 deletions src/components/SwipeStoryViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,12 @@ export function SwipeStoryViewerCore({
useLayoutEffect(() => {
if (opensFresh) clearSwipePosition(backState);
}, [opensFresh, backState]);

// The page is still scrolled where the previous screen left it, and panels
// don't reset it.
useLayoutEffect(() => {
if (opensFresh) window.scrollTo(0, 0);
}, [opensFresh]);

useEffect(() => {
// Save position + scrollY when page is about to be hidden/cached
Expand Down
Loading