-
Notifications
You must be signed in to change notification settings - Fork 3
/
pagination.test.ts
171 lines (148 loc) · 4.01 KB
/
pagination.test.ts
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { CursorPageIterator, PageIterator } from "./pagination.ts";
import { assertEquals } from "std/assert/mod.ts";
type MockArtist = {
id: string;
type: "artist";
name: string;
};
const totalMockItems = 50;
const mockArtists: MockArtist[] = Array(totalMockItems)
.fill(0)
.map((_, i) => ({
id: i.toString(),
type: "artist",
name: "Radiohead",
}));
Deno.test("PageIterator: asyncIterator", async () => {
const pageIterator = new PageIterator((offset) => {
const limit = 20;
return Promise.resolve({
items: mockArtists.slice(offset, offset + limit),
limit,
next: offset + limit < totalMockItems ? "http://example.com" : null,
total: totalMockItems,
});
});
const iter1 = pageIterator[Symbol.asyncIterator]();
for (let i = 0; i < totalMockItems; i++) {
const result = await iter1.next();
assertEquals(result, {
done: false,
value: mockArtists[i],
});
}
assertEquals(await iter1.next(), {
done: true,
value: null,
});
const iter2 = pageIterator[Symbol.asyncIterator](-totalMockItems);
for (let i = 0; i < totalMockItems; i++) {
const result = await iter2.next();
assertEquals(result, {
done: false,
value: mockArtists[i],
});
}
assertEquals(await iter2.next(), {
done: true,
value: null,
});
});
Deno.test("PageIterator: collect", async () => {
const pageIterator = new PageIterator((offset) => {
const limit = 20;
return Promise.resolve({
href: "#",
items: mockArtists.slice(offset, offset + limit),
offset,
limit,
next: offset + limit < totalMockItems ? "http://example.com" : null,
previous: offset > 0 ? "http://example.com" : null,
total: totalMockItems,
});
});
const items = await pageIterator.collect();
assertEquals(items, mockArtists);
const items2 = await pageIterator.collect(10);
assertEquals(items2, mockArtists.slice(0, 10));
});
Deno.test("CursorPageIterator: asyncIterator", async () => {
const cursorPageIterator = new CursorPageIterator((opts) => {
const limit = 20;
if (opts.after === undefined) {
return Promise.resolve({
items: mockArtists.slice(0, limit),
next: limit < totalMockItems ? "http://example.com" : null,
cursors: {
after: mockArtists.at(limit - 1)?.id,
},
});
}
const afterArtistIndex = mockArtists.findIndex((artist) =>
artist.id === opts.after
);
if (afterArtistIndex === -1) {
throw new Error("Invalid cursor");
}
return Promise.resolve({
items: mockArtists.slice(
afterArtistIndex + 1,
afterArtistIndex + 1 + limit,
),
next: afterArtistIndex + limit < totalMockItems
? "http://example.com"
: null,
cursors: {
after: mockArtists.at(afterArtistIndex + limit)?.id,
},
});
});
const iter = cursorPageIterator[Symbol.asyncIterator]();
for (let i = 0; i < totalMockItems; i++) {
const result = await iter.next();
assertEquals(result, {
done: false,
value: mockArtists[i],
});
}
assertEquals(await iter.next(), {
done: true,
value: null,
});
});
Deno.test("CursorPageIterator: asyncIterator backwards", async () => {
const initialBefore = 30;
const cursorPageIterator = new CursorPageIterator((opts) => {
const limit = 20;
const beforeArtistIndex = mockArtists.findIndex((artist) =>
artist.id === opts.before
);
if (beforeArtistIndex === -1) {
throw new Error("Invalid cursor");
}
const lastIndex = beforeArtistIndex - limit - 1;
const hasPreviousPage = !(lastIndex < 0);
return Promise.resolve({
items: mockArtists.slice(
hasPreviousPage ? lastIndex : 0,
beforeArtistIndex,
),
next: hasPreviousPage ? "http://example.com" : null,
cursors: {
before: hasPreviousPage ? mockArtists.at(lastIndex)?.id : undefined,
},
});
}, { direction: "backward", initialBefore: initialBefore.toString() });
const iter = cursorPageIterator[Symbol.asyncIterator]();
for (let i = initialBefore - 1; i >= 0; i--) {
const result = await iter.next();
assertEquals(result, {
done: false,
value: mockArtists[i],
});
}
assertEquals(await iter.next(), {
done: true,
value: null,
});
});