-
-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathpagination-cursor.test.ts
More file actions
117 lines (96 loc) · 3.01 KB
/
pagination-cursor.test.ts
File metadata and controls
117 lines (96 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { Collection } from '#/src/collection.js'
import z from 'zod'
const userSchema = z.object({
id: z.number(),
})
it('returns an empty array if the cursor points at a deleted record', async () => {
const users = new Collection({ schema: userSchema })
await users.createMany(5, (index) => ({
id: index + 1,
}))
const cursor = users.findFirst((q) => q.where({ id: 5 }))!
users.delete(cursor)
expect(
users.findMany(undefined, { cursor }),
'Returns an empty array if the cursor points at a deleted record',
).toEqual([])
})
it('returns all the matching records after the cursor', async () => {
const users = new Collection({ schema: userSchema })
await users.createMany(10, (index) => ({
id: index + 1,
}))
const cursor = users.findFirst((q) => q.where({ id: 7 }))!
expect(
users.findMany(undefined, { cursor }),
'Supports match-all queries',
).toEqual([{ id: 7 }, { id: 8 }, { id: 9 }, { id: 10 }])
})
it('returns the `take` number of results after the cursor', async () => {
const users = new Collection({ schema: userSchema })
await users.createMany(10, (index) => ({
id: index + 1,
}))
const cursor = users.findFirst((q) => q.where({ id: 7 }))!
expect(
users.findMany(undefined, { cursor, take: 3 }),
'Supports match-all queries',
).toEqual([{ id: 7 }, { id: 8 }, { id: 9 }])
})
it('supports skipping the cursor', async () => {
const users = new Collection({ schema: userSchema })
await users.createMany(10, (index) => ({
id: index + 1,
}))
const cursor = users.findFirst((q) => q.where({ id: 7 }))!
expect(
users.findMany(undefined, { cursor, skip: 1, take: 3 }),
'Supports match-all queries',
).toEqual([{ id: 8 }, { id: 9 }, { id: 10 }])
})
it('supports negative values for `take`', async () => {
const users = new Collection({ schema: userSchema })
await users.createMany(10, (index) => ({
id: index + 1,
}))
const cursor = users.findFirst((q) => q.where({ id: 10 }))
expect(
users.findMany(undefined, {
cursor,
take: -3,
}),
).toEqual([{ id: 10 }, { id: 9 }, { id: 8 }])
expect(
users.findMany(undefined, {
cursor,
skip: 1,
take: -3,
}),
'Supports skipping the cursor',
).toEqual([{ id: 9 }, { id: 8 }, { id: 7 }])
expect(
users.findMany((q) => q.where({ id: (id) => id > 2 }), {
cursor: users.findFirst((q) => q.where({ id: 8 })),
take: -3,
}),
).toEqual([{ id: 8 }, { id: 7 }, { id: 6 }])
expect(
users.findMany((q) => q.where({ id: (id) => id > 2 }), {
cursor: users.findFirst((q) => q.where({ id: 8 })),
skip: 1,
take: -3,
}),
).toEqual([{ id: 7 }, { id: 6 }, { id: 5 }])
expect(
users.findMany((q) => q.where({ id: (id) => id > 2 }), {
cursor: users.findFirst((q) => q.where({ id: 3 })),
take: -3,
}),
).toEqual([{ id: 3 }])
expect(
users.findMany((q) => q.where({ id: (id) => id > 2 }), {
cursor: users.findFirst((q) => q.where({ id: 2 })),
take: -3,
}),
).toEqual([])
})