Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

cursor-pagination.integration.test.ts

67 lines2.5 KB

1// Ordinary integration tests for the cursor adapter through production pagination:
2// response delivery with a shallower peer after deep exhaustion. The window law is
3// owned by cursor-pagination-oracle.integration.test.ts.
4import { describe, expect, it, vi } from 'vitest'
5import { createLiveQueryWindowController } from '../../db/src/live-query-window-controller.js'
6import {
7 createFixture,
8 observeRows,
9} from './cursor-pagination-integration-fixture.js'
10​
11describe(`cursor adapter through production pagination`, () => {
12 it(`holds real response delivery and preserves a shallower peer after deep exhaustion`, async () => {
13 const rows = Array.from({ length: 9 }, (_, id) => ({
14 id,
15 rank: Math.floor(id / 6),
16 group: 0,
17 }))
18 const scope = { group: undefined, descending: false }
19 const fixture = createFixture(rows, scope, 2, true)
20 const shallow = createLiveQueryWindowController(fixture.live, {
21 pageSize: 3,
22 })
23 const deep = createLiveQueryWindowController(fixture.live, { pageSize: 10 })
24 const publications: Array<Array<number>> = []
25 const stopShallow = shallow.subscribe(() =>
26 publications.push(shallow.getSnapshot().data.map((row) => row.id)),
27 )
28 const stopDeep = deep.subscribe(() => {})
29 let fulfilledPreloads = 0
30 const pending = Promise.allSettled(
31 [shallow.preload(), deep.preload()].map((promise) =>
32 promise.then(() => {
33 fulfilledPreloads++
34 }),
35 ),
36 )
37 try {
38 await vi.waitFor(() => expect(fixture.received()).toBe(1))
39 expect(fulfilledPreloads).toBe(0)
40 expect(shallow.getSnapshot().data).toEqual([])
41 expect(publications.every((batch) => batch.length === 0)).toBe(true)
42 fixture.gate.resolve()
43 expect((await pending).map((result) => result.status)).toEqual([
44 `fulfilled`,
45 `fulfilled`,
46 ])
47 expect(shallow.getSnapshot().hasNextPage).toBe(true)
48 expect(deep.getSnapshot().hasNextPage).toBe(false)
49 stopDeep()
50 deep.dispose()
51 await shallow.preload()
52 expect(observeRows(shallow.getSnapshot().data)).toEqual(rows.slice(0, 3))
53 expect(shallow.getSnapshot().hasNextPage).toBe(true)
54 await shallow.fetchNextPage()
55 expect(observeRows(shallow.getSnapshot().data)).toEqual(rows.slice(0, 6))
56 expect(fixture.backend.calls).toHaveLength(5)
57 } finally {
58 fixture.gate.resolve()
59 await pending
60 stopShallow()
61 stopDeep()
62 shallow.dispose()
63 deep.dispose()
64 await fixture.cleanup()
65 }
66 })
67})