-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpaginationUtils.spec.ts
More file actions
263 lines (238 loc) · 7.6 KB
/
paginationUtils.spec.ts
File metadata and controls
263 lines (238 loc) · 7.6 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import { describe, expect, it, vi } from 'vitest'
import type { OptionalPaginationParams } from './apiSchemas.ts'
import { encodeCursor } from './cursorCodec.ts'
import { createPaginatedResponse, getPaginatedEntriesByHasMore } from './paginationUtils.ts'
describe('paginationUtils', () => {
describe('createPaginatedResponse', () => {
it('array is empty', () => {
const mockedArray: Entity[] = []
const result = createPaginatedResponse(mockedArray, 2)
expect(result).toEqual({
data: [],
meta: { count: 0, hasMore: false },
})
})
describe('pageLimit', () => {
const mockedArray = [{ id: 'a' }, { id: 'b' }, { id: 'c' }, { id: 'd' }]
it('pageLimit less than input array', () => {
const result = createPaginatedResponse(mockedArray, 2)
expect(result).toEqual({
data: [mockedArray[0], mockedArray[1]],
meta: { count: 2, cursor: 'b', hasMore: true },
})
})
it('pageLimit equal to input array', () => {
const result = createPaginatedResponse(mockedArray, 4)
expect(result).toEqual({
data: mockedArray,
meta: { count: 4, cursor: 'd', hasMore: false },
})
})
it('pageLimit greater than input array', () => {
const result = createPaginatedResponse(mockedArray, 6)
expect(result).toEqual({
data: mockedArray,
meta: { count: 4, cursor: 'd', hasMore: false },
})
})
})
describe('cursor', () => {
it('empty cursorKeys produce error', () => {
const mockedArray = [{ id: 'a' }]
expect(() => createPaginatedResponse(mockedArray, 1, [])).toThrowError(
'cursorKeys cannot be an empty array',
)
})
it('cursor using id as default', () => {
const mockedArray = [{ id: 'a' }, { id: 'b' }, { id: 'c' }]
const result = createPaginatedResponse(mockedArray, 2)
expect(result).toEqual({
data: [mockedArray[0], mockedArray[1]],
meta: { count: 2, cursor: 'b', hasMore: true },
})
})
it('cursor using single prop', () => {
// not using id as prop to test type checking
const mockedArray = [
{ extra: 'a', name: 'hello' },
{ extra: 'b', name: 'world' },
]
const result = createPaginatedResponse(mockedArray, 3, ['name'])
expect(result).toEqual({
data: mockedArray,
meta: { count: 2, cursor: 'world', hasMore: false },
})
})
it('cursor with multiple fields', () => {
const mockedArray = [
{
id: '1',
name: 'apple',
description: 'red',
},
{
id: '2',
name: 'banana',
description: 'yellow',
},
{
id: '3',
name: 'orange',
description: 'orange',
},
]
const result = createPaginatedResponse(mockedArray, 3, ['id', 'name'])
expect(result).toEqual({
data: mockedArray,
meta: {
count: 3,
cursor: encodeCursor({ id: '3', name: 'orange' }),
hasMore: false,
},
})
})
it('cursor using single number prop is encoded', () => {
const mockedArray = [
{ id: '1', sequenceNumber: 100 },
{ id: '2', sequenceNumber: 200 },
{ id: '3', sequenceNumber: 300 },
]
const result = createPaginatedResponse(mockedArray, 3, ['sequenceNumber'])
expect(result).toEqual({
data: mockedArray,
meta: {
count: 3,
cursor: encodeCursor(300), // Number is encoded
hasMore: false,
},
})
})
})
})
describe('getPaginatedEntriesByHasMore', () => {
it('should call api 1 time and return value', async () => {
const spy = vi.spyOn(market, 'getApples').mockResolvedValueOnce({
data: [{ id: 'red' }],
meta: {
count: 1,
cursor: 'red',
hasMore: false,
},
})
const result = await getPaginatedEntriesByHasMore({ limit: 1 }, (params) => {
return market.getApples(params)
})
expect(spy).toHaveBeenCalledTimes(1)
expect(spy.mock.calls[0]![0]).toStrictEqual({ limit: 1 })
expect(result).toEqual([{ id: 'red' }])
})
it('should call api 1 time', async () => {
const spy = vi.spyOn(market, 'getApples').mockResolvedValueOnce({
data: [],
meta: {
count: 0,
hasMore: false,
},
})
const result = await getPaginatedEntriesByHasMore({ limit: 1 }, (params) => {
return market.getApples(params)
})
expect(spy).toHaveBeenCalledTimes(1)
expect(spy.mock.calls[0]![0]).toStrictEqual({ limit: 1 })
expect(result).toEqual([])
})
it('should call api 2 time', async () => {
const spy = vi
.spyOn(market, 'getApples')
.mockResolvedValueOnce({
data: [{ id: 'red' }],
meta: {
count: 1,
cursor: 'red',
hasMore: true,
},
})
.mockResolvedValueOnce({
data: [{ id: 'blue' }],
meta: {
count: 1,
cursor: 'blue',
hasMore: false,
},
})
const result = await getPaginatedEntriesByHasMore({ limit: 1 }, (params) => {
return market.getApples(params)
})
expect(spy).toHaveBeenCalledTimes(2)
expect(spy.mock.calls[0]![0]).toStrictEqual({ limit: 1 })
expect(spy.mock.calls[1]![0]).toStrictEqual({ limit: 1, after: 'red' })
expect(result).toEqual([{ id: 'red' }, { id: 'blue' }])
})
it('should respect initial cursor', async () => {
const spy = vi.spyOn(market, 'getApples').mockResolvedValueOnce({
data: [{ id: 'red' }],
meta: {
count: 1,
cursor: 'red',
hasMore: false,
},
})
const result = await getPaginatedEntriesByHasMore({ limit: 1, after: 'red' }, (params) => {
return market.getApples(params)
})
expect(spy).toHaveBeenCalledTimes(1)
expect(spy.mock.calls[0]![0]).toStrictEqual({ limit: 1, after: 'red' })
expect(result).toEqual([{ id: 'red' }])
})
it('should skip undefined even if provided explicitly', async () => {
const spy = vi.spyOn(market, 'getApples').mockResolvedValueOnce({
data: [{ id: 'red' }],
meta: {
count: 1,
cursor: 'red',
hasMore: false,
},
})
const undefinedCursorResult = await getPaginatedEntriesByHasMore(
{ limit: 1, after: undefined },
(params) => {
return market.getApples(params)
},
)
const undefinedLimitResult = await getPaginatedEntriesByHasMore(
{ limit: undefined },
(params) => {
return market.getApples(params)
},
)
expect(spy).toHaveBeenCalledTimes(2)
expect(spy.mock.calls[0]![0]).toStrictEqual({ limit: 1 })
expect(spy.mock.calls[1]![0]).toStrictEqual({})
expect(undefinedCursorResult).toEqual([{ id: 'red' }])
expect(undefinedLimitResult).toEqual([{ id: 'red' }])
})
})
})
type Entity = {
id: string
}
type GetApplesResponse = {
data: Entity[]
meta: {
count: number
cursor?: string
hasMore: boolean
}
}
const market = {
getApples: (params: OptionalPaginationParams): Promise<GetApplesResponse> => {
return Promise.resolve({
data: [{ id: 'red' }],
meta: {
count: params.limit ?? 1,
cursor: 'red',
hasMore: false,
},
})
},
}