Skip to content

Commit 2e7f238

Browse files
committed
add test that checks that we detect new pages on more rows syncing
1 parent 0c3e372 commit 2e7f238

1 file changed

Lines changed: 86 additions & 0 deletions

File tree

packages/react-db/tests/useLiveInfiniteQuery.test.tsx

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,4 +707,90 @@ describe(`useLiveInfiniteQuery`, () => {
707707
expect(result.current.pageParams).toEqual([100, 101])
708708
})
709709
})
710+
711+
it(`should detect hasNextPage change when new items are synced`, async () => {
712+
// Start with exactly 20 items (2 pages)
713+
const posts = createMockPosts(20)
714+
const collection = createCollection(
715+
mockSyncCollectionOptions<Post>({
716+
id: `sync-detection-test`,
717+
getKey: (post: Post) => post.id,
718+
initialData: posts,
719+
})
720+
)
721+
722+
const { result } = renderHook(() => {
723+
return useLiveInfiniteQuery(
724+
(q) =>
725+
q
726+
.from({ posts: collection })
727+
.orderBy(({ posts: p }) => p.createdAt, `desc`),
728+
{
729+
pageSize: 10,
730+
getNextPageParam: (lastPage) =>
731+
lastPage.length === 10 ? lastPage.length : undefined,
732+
}
733+
)
734+
})
735+
736+
await waitFor(() => {
737+
expect(result.current.isReady).toBe(true)
738+
})
739+
740+
// Load both pages
741+
act(() => {
742+
result.current.fetchNextPage()
743+
})
744+
745+
await waitFor(() => {
746+
expect(result.current.pages).toHaveLength(2)
747+
})
748+
749+
// Should have no next page (exactly 20 items, 2 full pages, peek returns nothing)
750+
expect(result.current.hasNextPage).toBe(false)
751+
expect(result.current.data).toHaveLength(20)
752+
753+
// Add 5 more items to the collection
754+
act(() => {
755+
collection.utils.begin()
756+
for (let i = 0; i < 5; i++) {
757+
collection.utils.write({
758+
type: `insert`,
759+
value: {
760+
id: `new-${i}`,
761+
title: `New Post ${i}`,
762+
content: `Content ${i}`,
763+
createdAt: Date.now() + i,
764+
category: `tech`,
765+
},
766+
})
767+
}
768+
collection.utils.commit()
769+
})
770+
771+
// Should now detect that there's a next page available
772+
await waitFor(() => {
773+
expect(result.current.hasNextPage).toBe(true)
774+
})
775+
776+
// Data should still be 20 items (we haven't fetched the next page yet)
777+
expect(result.current.data).toHaveLength(20)
778+
expect(result.current.pages).toHaveLength(2)
779+
780+
// Fetch the next page
781+
act(() => {
782+
result.current.fetchNextPage()
783+
})
784+
785+
await waitFor(() => {
786+
expect(result.current.pages).toHaveLength(3)
787+
})
788+
789+
// Third page should have the new items
790+
expect(result.current.pages[2]).toHaveLength(5)
791+
expect(result.current.data).toHaveLength(25)
792+
793+
// No more pages available now
794+
expect(result.current.hasNextPage).toBe(false)
795+
})
710796
})

0 commit comments

Comments
 (0)