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
3 changes: 2 additions & 1 deletion src/components/CommentArticle.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ describe('CommentArticle', () => {
});

it('omits the parent link when parentId is null', () => {
// Top-level comments have no parent — drop the pill, don't render a dangling link.
// A top-level comment's parent is its story, so only an item that isn't a
// comment has none: drop the pill rather than render a dangling link.
renderArticle({ comment: { ...mockComment, parentId: null } });

expect(screen.queryByRole('link', { name: 'parent' })).not.toBeInTheDocument();
Expand Down
5 changes: 5 additions & 0 deletions src/components/StateView.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ describe('StateView', () => {
expect(heading).toBeInTheDocument();
});

it('hides the illustration, including its decorative glyphs, from screen readers', () => {
render(<StateView variant="not-found" />);
expect(screen.getByText('{').closest('[aria-hidden="true"]')).not.toBeNull();
});

it('does NOT render an <h2> in compact mode (single-row layout)', () => {
render(<StateView variant="empty" compact title="No replies yet" />);
expect(
Expand Down
2 changes: 1 addition & 1 deletion src/components/StateView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export function StateView({ variant, title, description, action, compact, classN

return (
<div className={className ?? 'flex flex-col items-center justify-center text-center p-6'}>
<div className="mb-4">
<div className="mb-4" aria-hidden="true">
<Scene />
</div>
{resolvedTitle && (
Expand Down
8 changes: 4 additions & 4 deletions src/components/SwipeCommentViewer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ describe('SwipeCommentViewer', () => {
}, { timeout: 30000 });
}, 35000);

it('renders single panel when comment has no parent', async () => {
it('renders a single panel for an item with no parent', async () => {
vi.spyOn(hnSdk, 'readItem').mockImplementation(async (id) => {
if (Number(id) === 7777) {
return { id: 7777, by: 'solo', text: 'Top-level comment with no parent.', time: Math.floor(Date.now() / 1000) - 300, type: 'comment' };
return { id: 7777, by: 'solo', text: 'An item with no parent.', time: Math.floor(Date.now() / 1000) - 300, type: 'story' };
}
return null;
});
Expand All @@ -121,9 +121,9 @@ describe('SwipeCommentViewer', () => {
http.get(`${ALGOLIA_API}/items/:id`, () => {
return HttpResponse.json({
id: 7777,
type: 'comment',
type: 'story',
author: 'solo',
text: 'Top-level comment with no parent.',
text: 'An item with no parent.',
created_at_i: Math.floor(Date.now() / 1000) - 300,
parent_id: null,
story_id: null,
Expand Down
17 changes: 17 additions & 0 deletions src/hooks/usePrefetchItem.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,23 @@ describe('usePrefetchItems', () => {
renderHook(() => usePrefetchItems(0, undefined as unknown as Item[], 3));
}).not.toThrow();
});

it('does not restart an in-flight prefetch when the viewer remounts', async () => {
const spy = vi.spyOn(hn, 'prefetchItemComments').mockReturnValue(new Promise(() => { /* in flight */ }));
const items = [{ id: 1 }, { id: 2 }, { id: 3 }] as Item[];

const first = renderHook(() => usePrefetchItems(0, items, 2));
await act(async () => { vi.advanceTimersByTime(250); });
// A tab switch or Back mounts a new viewer with the same stories ahead.
first.unmount();
renderHook(() => usePrefetchItems(0, items, 2));
await act(async () => { vi.advanceTimersByTime(250); });

expect(spy.mock.calls.map(([id]) => id)).toEqual([2, 3]);
cancelAllPrefetches();
expect(spy.mock.calls.every(([, signal]) => signal?.aborted)).toBe(true);
spy.mockRestore();
});
});

describe('cancelAllPrefetches', () => {
Expand Down
2 changes: 2 additions & 0 deletions src/hooks/usePrefetchItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ async function processQueue() {
if (bestId === null) break;

prefetchQueue.delete(bestId);
// A remounted swipe viewer re-queues stories that may still be downloading.
if (activePrefetches.has(bestId)) continue;

const cached = getCachedItem(bestId);
if (cached?.isFresh) {
Expand Down
6 changes: 3 additions & 3 deletions src/hooks/useSiblingComments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ describe('useSiblingComments', () => {
});
});

it('returns single comment when parent_id is null', async () => {
it('returns just this item when it has no parent', async () => {
vi.spyOn(hnSdk, 'readItem').mockImplementation(async (id) => {
if (Number(id) === 7777) {
return { id: 7777, by: 'someone', text: 'A top-level comment.', time: Math.floor(Date.now() / 1000) - 300, type: 'comment' };
return { id: 7777, by: 'someone', text: 'An item with no parent.', time: Math.floor(Date.now() / 1000) - 300, type: 'story' };
}
return null;
});
Expand Down Expand Up @@ -188,7 +188,7 @@ describe('useSiblingComments', () => {

vi.spyOn(hnSdk, 'readItem').mockImplementation(async (id) => {
if (Number(id) === 7777) {
return { id: 7777, by: 'someone', text: 'A top-level comment.', time: Math.floor(Date.now() / 1000) - 300, type: 'comment' };
return { id: 7777, by: 'someone', text: 'An item with no parent.', time: Math.floor(Date.now() / 1000) - 300, type: 'story' };
}
return null;
});
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/useSiblingComments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ export function useSiblingComments(commentId: number | string): UseSiblingCommen
const item = await fetchFirebaseItem(numericId, signal);
if (signal.aborted) return;

// No parent → top-level comment or deleted; show just this one.
// Every comment has a parent, even a deleted one, so this is an item that
// isn't a comment (a story, job, poll or poll option); show just this one.
if (item.parent == null) {
setSiblingIds([numericId]);
setCurrentIndex(0);
Expand Down