|
1
|
import React from "react" |
|
2
|
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest" |
|
3
|
import { cleanup, render } from "@testing-library/react" |
|
4
|
import type { Sidebar } from "types" |
|
5
|
|
|
6
|
// Mock dependencies |
|
7
|
const mockShownSidebar = vi.fn() |
|
8
|
const mockActivePath = vi.fn(() => "/test-path") |
|
9
|
const mockUseSidebar = vi.fn(() => ({ |
|
10
|
shownSidebar: mockShownSidebar(), |
|
11
|
activePath: mockActivePath(), |
|
12
|
})) |
|
13
|
|
|
14
|
let previousActivePath: string | undefined = undefined |
|
15
|
const mockUsePrevious = vi.fn((value: unknown) => { |
|
16
|
const current = previousActivePath |
|
17
|
previousActivePath = value as string |
|
18
|
return current |
|
19
|
}) |
|
20
|
|
|
21
|
vi.mock("@/providers/Sidebar", () => ({ |
|
22
|
useSidebar: () => mockUseSidebar(), |
|
23
|
})) |
|
24
|
|
|
25
|
vi.mock("@uidotdev/usehooks", () => ({ |
|
26
|
usePrevious: (value: unknown) => mockUsePrevious(value), |
|
27
|
})) |
|
28
|
|
|
29
|
import { PaginationProvider, usePagination } from "../index" |
|
30
|
|
|
31
|
const TestComponent = () => { |
|
32
|
const { nextPage, previousPage } = usePagination() |
|
33
|
return ( |
|
34
|
<div> |
|
35
|
<div data-testid="next-page-title">{nextPage?.title || "no-next"}</div> |
|
36
|
<div data-testid="next-page-link">{nextPage?.link || "no-next-link"}</div> |
|
37
|
<div data-testid="next-page-parent"> |
|
38
|
{nextPage?.parentTitle || "no-next-parent"} |
|
39
|
</div> |
|
40
|
<div data-testid="prev-page-title"> |
|
41
|
{previousPage?.title || "no-prev"} |
|
42
|
</div> |
|
43
|
<div data-testid="prev-page-link"> |
|
44
|
{previousPage?.link || "no-prev-link"} |
|
45
|
</div> |
|
46
|
<div data-testid="prev-page-parent"> |
|
47
|
{previousPage?.parentTitle || "no-prev-parent"} |
|
48
|
</div> |
|
49
|
</div> |
|
50
|
) |
|
51
|
} |
|
52
|
|
|
53
|
beforeEach(() => { |
|
54
|
vi.clearAllMocks() |
|
55
|
previousActivePath = undefined |
|
56
|
mockActivePath.mockReturnValue("/test-path") |
|
57
|
}) |
|
58
|
|
|
59
|
afterEach(() => { |
|
60
|
cleanup() |
|
61
|
vi.restoreAllMocks() |
|
62
|
}) |
|
63
|
|
|
64
|
describe("rendering", () => { |
|
65
|
test("renders children", () => { |
|
66
|
const mockSidebar: Sidebar.Sidebar = { |
|
67
|
sidebar_id: "test-sidebar", |
|
68
|
title: "Test Sidebar", |
|
69
|
items: [], |
|
70
|
} |
|
71
|
mockShownSidebar.mockReturnValue(mockSidebar) |
|
72
|
|
|
73
|
const { container } = render( |
|
74
|
<PaginationProvider> |
|
75
|
<div>Test</div> |
|
76
|
</PaginationProvider> |
|
77
|
) |
|
78
|
expect(container).toHaveTextContent("Test") |
|
79
|
}) |
|
80
|
}) |
|
81
|
|
|
82
|
describe("usePagination hook", () => { |
|
83
|
test("throws error when used outside provider", () => { |
|
84
|
// Suppress console.error for this test |
|
85
|
const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {}) |
|
86
|
|
|
87
|
expect(() => { |
|
88
|
render(<TestComponent />) |
|
89
|
}).toThrow("usePagination must be used inside a PaginationProvider") |
|
90
|
|
|
91
|
consoleSpy.mockRestore() |
|
92
|
}) |
|
93
|
|
|
94
|
test("returns undefined for next and previous when no sidebar", () => { |
|
95
|
mockShownSidebar.mockReturnValue(null) |
|
96
|
|
|
97
|
const { getByTestId } = render( |
|
98
|
<PaginationProvider> |
|
99
|
<TestComponent /> |
|
100
|
</PaginationProvider> |
|
101
|
) |
|
102
|
|
|
103
|
expect(getByTestId("next-page-title")).toHaveTextContent("no-next") |
|
104
|
expect(getByTestId("prev-page-title")).toHaveTextContent("no-prev") |
|
105
|
}) |
|
106
|
|
|
107
|
test("returns undefined for next and previous when sidebar has no items", () => { |
|
108
|
const mockSidebar: Sidebar.Sidebar = { |
|
109
|
sidebar_id: "test-sidebar", |
|
110
|
title: "Test Sidebar", |
|
111
|
items: [], |
|
112
|
} |
|
113
|
mockShownSidebar.mockReturnValue(mockSidebar) |
|
114
|
|
|
115
|
const { getByTestId } = render( |
|
116
|
<PaginationProvider> |
|
117
|
<TestComponent /> |
|
118
|
</PaginationProvider> |
|
119
|
) |
|
120
|
|
|
121
|
expect(getByTestId("next-page-title")).toHaveTextContent("no-next") |
|
122
|
expect(getByTestId("prev-page-title")).toHaveTextContent("no-prev") |
|
123
|
}) |
|
124
|
|
|
125
|
test("finds next page in flat sidebar", () => { |
|
126
|
const mockSidebar: Sidebar.Sidebar = { |
|
127
|
sidebar_id: "test-sidebar", |
|
128
|
title: "Test Sidebar", |
|
129
|
items: [ |
|
130
|
{ |
|
131
|
type: "link", |
|
132
|
path: "/page-1", |
|
133
|
title: "Page 1", |
|
134
|
}, |
|
135
|
{ |
|
136
|
type: "link", |
|
137
|
path: "/test-path", |
|
138
|
title: "Current Page", |
|
139
|
}, |
|
140
|
{ |
|
141
|
type: "link", |
|
142
|
path: "/page-3", |
|
143
|
title: "Page 3", |
|
144
|
}, |
|
145
|
], |
|
146
|
} |
|
147
|
mockShownSidebar.mockReturnValue(mockSidebar) |
|
148
|
mockActivePath.mockReturnValue("/test-path") |
|
149
|
|
|
150
|
const { getByTestId } = render( |
|
151
|
<PaginationProvider> |
|
152
|
<TestComponent /> |
|
153
|
</PaginationProvider> |
|
154
|
) |
|
155
|
|
|
156
|
expect(getByTestId("next-page-title")).toHaveTextContent("Page 3") |
|
157
|
expect(getByTestId("next-page-link")).toHaveTextContent("/page-3") |
|
158
|
expect(getByTestId("prev-page-title")).toHaveTextContent("Page 1") |
|
159
|
expect(getByTestId("prev-page-link")).toHaveTextContent("/page-1") |
|
160
|
}) |
|
161
|
|
|
162
|
test("returns undefined for next page when on last item", () => { |
|
163
|
const mockSidebar: Sidebar.Sidebar = { |
|
164
|
sidebar_id: "test-sidebar", |
|
165
|
title: "Test Sidebar", |
|
166
|
items: [ |
|
167
|
{ |
|
168
|
type: "link", |
|
169
|
path: "/page-1", |
|
170
|
title: "Page 1", |
|
171
|
}, |
|
172
|
{ |
|
173
|
type: "link", |
|
174
|
path: "/test-path", |
|
175
|
title: "Current Page", |
|
176
|
}, |
|
177
|
], |
|
178
|
} |
|
179
|
mockShownSidebar.mockReturnValue(mockSidebar) |
|
180
|
mockActivePath.mockReturnValue("/test-path") |
|
181
|
|
|
182
|
const { getByTestId } = render( |
|
183
|
<PaginationProvider> |
|
184
|
<TestComponent /> |
|
185
|
</PaginationProvider> |
|
186
|
) |
|
187
|
|
|
188
|
expect(getByTestId("next-page-title")).toHaveTextContent("no-next") |
|
189
|
expect(getByTestId("prev-page-title")).toHaveTextContent("Page 1") |
|
190
|
}) |
|
191
|
|
|
192
|
test("returns undefined for previous page when on first item", () => { |
|
193
|
const mockSidebar: Sidebar.Sidebar = { |
|
194
|
sidebar_id: "test-sidebar", |
|
195
|
title: "Test Sidebar", |
|
196
|
items: [ |
|
197
|
{ |
|
198
|
type: "link", |
|
199
|
path: "/test-path", |
|
200
|
title: "Current Page", |
|
201
|
}, |
|
202
|
{ |
|
203
|
type: "link", |
|
204
|
path: "/page-2", |
|
205
|
title: "Page 2", |
|
206
|
}, |
|
207
|
], |
|
208
|
} |
|
209
|
mockShownSidebar.mockReturnValue(mockSidebar) |
|
210
|
mockActivePath.mockReturnValue("/test-path") |
|
211
|
|
|
212
|
const { getByTestId } = render( |
|
213
|
<PaginationProvider> |
|
214
|
<TestComponent /> |
|
215
|
</PaginationProvider> |
|
216
|
) |
|
217
|
|
|
218
|
expect(getByTestId("next-page-title")).toHaveTextContent("Page 2") |
|
219
|
expect(getByTestId("prev-page-title")).toHaveTextContent("no-prev") |
|
220
|
}) |
|
221
|
|
|
222
|
test("finds next page in nested sidebar", () => { |
|
223
|
const mockSidebar: Sidebar.Sidebar = { |
|
224
|
sidebar_id: "test-sidebar", |
|
225
|
title: "Test Sidebar", |
|
226
|
items: [ |
|
227
|
{ |
|
228
|
type: "link", |
|
229
|
path: "/test-path", |
|
230
|
title: "Current Page", |
|
231
|
children: [ |
|
232
|
{ |
|
233
|
type: "link", |
|
234
|
path: "/test-path/child-1", |
|
235
|
title: "Child 1", |
|
236
|
}, |
|
237
|
{ |
|
238
|
type: "link", |
|
239
|
path: "/test-path/child-2", |
|
240
|
title: "Child 2", |
|
241
|
}, |
|
242
|
], |
|
243
|
}, |
|
244
|
{ |
|
245
|
type: "link", |
|
246
|
path: "/page-2", |
|
247
|
title: "Page 2", |
|
248
|
}, |
|
249
|
], |
|
250
|
} |
|
251
|
mockShownSidebar.mockReturnValue(mockSidebar) |
|
252
|
mockActivePath.mockReturnValue("/test-path") |
|
253
|
|
|
254
|
const { getByTestId } = render( |
|
255
|
<PaginationProvider> |
|
256
|
<TestComponent /> |
|
257
|
</PaginationProvider> |
|
258
|
) |
|
259
|
|
|
260
|
expect(getByTestId("next-page-title")).toHaveTextContent("Child 1") |
|
261
|
expect(getByTestId("next-page-link")).toHaveTextContent( |
|
262
|
"/test-path/child-1" |
|
263
|
) |
|
264
|
expect(getByTestId("next-page-parent")).toHaveTextContent("Current Page") |
|
265
|
}) |
|
266
|
|
|
267
|
test("finds previous page in nested sidebar", () => { |
|
268
|
const mockSidebar: Sidebar.Sidebar = { |
|
269
|
sidebar_id: "test-sidebar", |
|
270
|
title: "Test Sidebar", |
|
271
|
items: [ |
|
272
|
{ |
|
273
|
type: "category", |
|
274
|
title: "Category 1", |
|
275
|
children: [ |
|
276
|
{ |
|
277
|
type: "link", |
|
278
|
path: "/category-1/item-1", |
|
279
|
title: "Item 1", |
|
280
|
}, |
|
281
|
{ |
|
282
|
type: "link", |
|
283
|
path: "/test-path", |
|
284
|
title: "Current Page", |
|
285
|
}, |
|
286
|
], |
|
287
|
}, |
|
288
|
], |
|
289
|
} |
|
290
|
mockShownSidebar.mockReturnValue(mockSidebar) |
|
291
|
mockActivePath.mockReturnValue("/test-path") |
|
292
|
|
|
293
|
const { getByTestId } = render( |
|
294
|
<PaginationProvider> |
|
295
|
<TestComponent /> |
|
296
|
</PaginationProvider> |
|
297
|
) |
|
298
|
|
|
299
|
expect(getByTestId("prev-page-title")).toHaveTextContent("Item 1") |
|
300
|
expect(getByTestId("prev-page-link")).toHaveTextContent( |
|
301
|
"/category-1/item-1" |
|
302
|
) |
|
303
|
}) |
|
304
|
|
|
305
|
test("handles separators in sidebar", () => { |
|
306
|
const mockSidebar: Sidebar.Sidebar = { |
|
307
|
sidebar_id: "test-sidebar", |
|
308
|
title: "Test Sidebar", |
|
309
|
items: [ |
|
310
|
{ |
|
311
|
type: "link", |
|
312
|
path: "/page-1", |
|
313
|
title: "Page 1", |
|
314
|
}, |
|
315
|
{ |
|
316
|
type: "separator", |
|
317
|
}, |
|
318
|
{ |
|
319
|
type: "link", |
|
320
|
path: "/test-path", |
|
321
|
title: "Current Page", |
|
322
|
}, |
|
323
|
{ |
|
324
|
type: "separator", |
|
325
|
}, |
|
326
|
{ |
|
327
|
type: "link", |
|
328
|
path: "/page-3", |
|
329
|
title: "Page 3", |
|
330
|
}, |
|
331
|
], |
|
332
|
} |
|
333
|
mockShownSidebar.mockReturnValue(mockSidebar) |
|
334
|
mockActivePath.mockReturnValue("/test-path") |
|
335
|
|
|
336
|
const { getByTestId } = render( |
|
337
|
<PaginationProvider> |
|
338
|
<TestComponent /> |
|
339
|
</PaginationProvider> |
|
340
|
) |
|
341
|
|
|
342
|
expect(getByTestId("next-page-title")).toHaveTextContent("Page 3") |
|
343
|
expect(getByTestId("prev-page-title")).toHaveTextContent("Page 1") |
|
344
|
}) |
|
345
|
|
|
346
|
test("finds next page across categories", () => { |
|
347
|
const mockSidebar: Sidebar.Sidebar = { |
|
348
|
sidebar_id: "test-sidebar", |
|
349
|
title: "Test Sidebar", |
|
350
|
items: [ |
|
351
|
{ |
|
352
|
type: "category", |
|
353
|
title: "Category 1", |
|
354
|
children: [ |
|
355
|
{ |
|
356
|
type: "link", |
|
357
|
path: "/category-1/item-1", |
|
358
|
title: "Item 1", |
|
359
|
}, |
|
360
|
{ |
|
361
|
type: "link", |
|
362
|
path: "/test-path", |
|
363
|
title: "Current Page", |
|
364
|
}, |
|
365
|
], |
|
366
|
}, |
|
367
|
{ |
|
368
|
type: "category", |
|
369
|
title: "Category 2", |
|
370
|
children: [ |
|
371
|
{ |
|
372
|
type: "link", |
|
373
|
path: "/category-2/item-1", |
|
374
|
title: "Category 2 Item 1", |
|
375
|
}, |
|
376
|
], |
|
377
|
}, |
|
378
|
], |
|
379
|
} |
|
380
|
mockShownSidebar.mockReturnValue(mockSidebar) |
|
381
|
mockActivePath.mockReturnValue("/test-path") |
|
382
|
|
|
383
|
const { getByTestId } = render( |
|
384
|
<PaginationProvider> |
|
385
|
<TestComponent /> |
|
386
|
</PaginationProvider> |
|
387
|
) |
|
388
|
|
|
389
|
expect(getByTestId("next-page-title")).toHaveTextContent( |
|
390
|
"Category 2 Item 1" |
|
391
|
) |
|
392
|
expect(getByTestId("next-page-link")).toHaveTextContent( |
|
393
|
"/category-2/item-1" |
|
394
|
) |
|
395
|
expect(getByTestId("next-page-parent")).toHaveTextContent("Category 2") |
|
396
|
}) |
|
397
|
|
|
398
|
test("finds previous page across categories", () => { |
|
399
|
const mockSidebar: Sidebar.Sidebar = { |
|
400
|
sidebar_id: "test-sidebar", |
|
401
|
title: "Test Sidebar", |
|
402
|
items: [ |
|
403
|
{ |
|
404
|
type: "category", |
|
405
|
title: "Category 1", |
|
406
|
children: [ |
|
407
|
{ |
|
408
|
type: "link", |
|
409
|
path: "/category-1/item-1", |
|
410
|
title: "Item 1", |
|
411
|
}, |
|
412
|
], |
|
413
|
}, |
|
414
|
{ |
|
415
|
type: "category", |
|
416
|
title: "Category 2", |
|
417
|
children: [ |
|
418
|
{ |
|
419
|
type: "link", |
|
420
|
path: "/test-path", |
|
421
|
title: "Current Page", |
|
422
|
}, |
|
423
|
], |
|
424
|
}, |
|
425
|
], |
|
426
|
} |
|
427
|
mockShownSidebar.mockReturnValue(mockSidebar) |
|
428
|
mockActivePath.mockReturnValue("/test-path") |
|
429
|
|
|
430
|
const { getByTestId } = render( |
|
431
|
<PaginationProvider> |
|
432
|
<TestComponent /> |
|
433
|
</PaginationProvider> |
|
434
|
) |
|
435
|
|
|
436
|
expect(getByTestId("prev-page-title")).toHaveTextContent("Item 1") |
|
437
|
expect(getByTestId("prev-page-link")).toHaveTextContent( |
|
438
|
"/category-1/item-1" |
|
439
|
) |
|
440
|
}) |
|
441
|
|
|
442
|
test("handles sidebar with children property", () => { |
|
443
|
const mockSidebar = { |
|
444
|
sidebar_id: "test-sidebar", |
|
445
|
title: "Test Sidebar", |
|
446
|
children: [ |
|
447
|
{ |
|
448
|
type: "link", |
|
449
|
path: "/page-1", |
|
450
|
title: "Page 1", |
|
451
|
}, |
|
452
|
{ |
|
453
|
type: "link", |
|
454
|
path: "/test-path", |
|
455
|
title: "Current Page", |
|
456
|
}, |
|
457
|
{ |
|
458
|
type: "link", |
|
459
|
path: "/page-3", |
|
460
|
title: "Page 3", |
|
461
|
}, |
|
462
|
], |
|
463
|
} |
|
464
|
mockShownSidebar.mockReturnValue(mockSidebar) |
|
465
|
mockActivePath.mockReturnValue("/test-path") |
|
466
|
|
|
467
|
const { getByTestId } = render( |
|
468
|
<PaginationProvider> |
|
469
|
<TestComponent /> |
|
470
|
</PaginationProvider> |
|
471
|
) |
|
472
|
|
|
473
|
expect(getByTestId("next-page-title")).toHaveTextContent("Page 3") |
|
474
|
expect(getByTestId("prev-page-title")).toHaveTextContent("Page 1") |
|
475
|
}) |
|
476
|
|
|
477
|
test("updates pagination when activePath changes", () => { |
|
478
|
const mockSidebar: Sidebar.Sidebar = { |
|
479
|
sidebar_id: "test-sidebar", |
|
480
|
title: "Test Sidebar", |
|
481
|
items: [ |
|
482
|
{ |
|
483
|
type: "link", |
|
484
|
path: "/page-1", |
|
485
|
title: "Page 1", |
|
486
|
}, |
|
487
|
{ |
|
488
|
type: "link", |
|
489
|
path: "/test-path", |
|
490
|
title: "Current Page", |
|
491
|
}, |
|
492
|
{ |
|
493
|
type: "link", |
|
494
|
path: "/page-3", |
|
495
|
title: "Page 3", |
|
496
|
}, |
|
497
|
], |
|
498
|
} |
|
499
|
mockShownSidebar.mockReturnValue(mockSidebar) |
|
500
|
mockActivePath.mockReturnValue("/test-path") |
|
501
|
previousActivePath = undefined |
|
502
|
|
|
503
|
const { getByTestId, rerender } = render( |
|
504
|
<PaginationProvider> |
|
505
|
<TestComponent /> |
|
506
|
</PaginationProvider> |
|
507
|
) |
|
508
|
|
|
509
|
expect(getByTestId("next-page-title")).toHaveTextContent("Page 3") |
|
510
|
expect(getByTestId("prev-page-title")).toHaveTextContent("Page 1") |
|
511
|
|
|
512
|
// Change active path |
|
513
|
previousActivePath = "/test-path" |
|
514
|
mockActivePath.mockReturnValue("/page-3") |
|
515
|
|
|
516
|
rerender( |
|
517
|
<PaginationProvider> |
|
518
|
<TestComponent /> |
|
519
|
</PaginationProvider> |
|
520
|
) |
|
521
|
|
|
522
|
expect(getByTestId("next-page-title")).toHaveTextContent("no-next") |
|
523
|
expect(getByTestId("prev-page-title")).toHaveTextContent("Current Page") |
|
524
|
}) |
|
525
|
|
|
526
|
test("does not update pagination when activePath has not changed", () => { |
|
527
|
const mockSidebar: Sidebar.Sidebar = { |
|
528
|
sidebar_id: "test-sidebar", |
|
529
|
title: "Test Sidebar", |
|
530
|
items: [ |
|
531
|
{ |
|
532
|
type: "link", |
|
533
|
path: "/test-path", |
|
534
|
title: "Current Page", |
|
535
|
}, |
|
536
|
{ |
|
537
|
type: "link", |
|
538
|
path: "/page-2", |
|
539
|
title: "Page 2", |
|
540
|
}, |
|
541
|
], |
|
542
|
} |
|
543
|
mockShownSidebar.mockReturnValue(mockSidebar) |
|
544
|
mockActivePath.mockReturnValue("/test-path") |
|
545
|
previousActivePath = "/test-path" // Same as current |
|
546
|
|
|
547
|
const { getByTestId, rerender } = render( |
|
548
|
<PaginationProvider> |
|
549
|
<TestComponent /> |
|
550
|
</PaginationProvider> |
|
551
|
) |
|
552
|
|
|
553
|
const initialNextPage = getByTestId("next-page-title").textContent |
|
554
|
|
|
555
|
// Re-render with same activePath |
|
556
|
rerender( |
|
557
|
<PaginationProvider> |
|
558
|
<TestComponent /> |
|
559
|
</PaginationProvider> |
|
560
|
) |
|
561
|
|
|
562
|
// Should remain the same |
|
563
|
expect(getByTestId("next-page-title")).toHaveTextContent(initialNextPage!) |
|
564
|
}) |
|
565
|
|
|
566
|
test("handles complex nested structure", () => { |
|
567
|
const mockSidebar: Sidebar.Sidebar = { |
|
568
|
sidebar_id: "test-sidebar", |
|
569
|
title: "Test Sidebar", |
|
570
|
items: [ |
|
571
|
{ |
|
572
|
type: "category", |
|
573
|
title: "Category 1", |
|
574
|
children: [ |
|
575
|
{ |
|
576
|
type: "sub-category", |
|
577
|
title: "Sub Category 1", |
|
578
|
children: [ |
|
579
|
{ |
|
580
|
type: "link", |
|
581
|
path: "/cat1/sub1/item1", |
|
582
|
title: "Item 1", |
|
583
|
}, |
|
584
|
{ |
|
585
|
type: "link", |
|
586
|
path: "/test-path", |
|
587
|
title: "Current Page", |
|
588
|
}, |
|
589
|
{ |
|
590
|
type: "link", |
|
591
|
path: "/cat1/sub1/item3", |
|
592
|
title: "Item 3", |
|
593
|
}, |
|
594
|
], |
|
595
|
}, |
|
596
|
], |
|
597
|
}, |
|
598
|
], |
|
599
|
} |
|
600
|
mockShownSidebar.mockReturnValue(mockSidebar) |
|
601
|
mockActivePath.mockReturnValue("/test-path") |
|
602
|
|
|
603
|
const { getByTestId } = render( |
|
604
|
<PaginationProvider> |
|
605
|
<TestComponent /> |
|
606
|
</PaginationProvider> |
|
607
|
) |
|
608
|
|
|
609
|
expect(getByTestId("next-page-title")).toHaveTextContent("Item 3") |
|
610
|
expect(getByTestId("next-page-link")).toHaveTextContent("/cat1/sub1/item3") |
|
611
|
expect(getByTestId("prev-page-title")).toHaveTextContent("Item 1") |
|
612
|
expect(getByTestId("prev-page-link")).toHaveTextContent("/cat1/sub1/item1") |
|
613
|
}) |
|
614
|
}) |