Skip to content

Commit 8ce8cba

Browse files
test/use-offset-pagination 🧊 chore: write test for hook
1 parent 75f5fdc commit 8ce8cba

File tree

1 file changed

+325
-0
lines changed

1 file changed

+325
-0
lines changed
Lines changed: 325 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,325 @@
1+
import { act, renderHook } from '@testing-library/react';
2+
3+
import { useOffsetPagination } from './useOffsetPagination';
4+
5+
it('Should use offset pagination', () => {
6+
const { result } = renderHook(() => useOffsetPagination());
7+
8+
expect(result.current.page).toBe(1);
9+
expect(result.current.currentPageSize).toBe(10);
10+
expect(result.current.pageCount).toBe(Number.POSITIVE_INFINITY);
11+
expect(result.current.isFirstPage).toBe(true);
12+
expect(result.current.isLastPage).toBe(false);
13+
expect(result.current.next).toBeTypeOf('function');
14+
expect(result.current.prev).toBeTypeOf('function');
15+
expect(result.current.set).toBeTypeOf('function');
16+
});
17+
18+
it('Should set initial options', () => {
19+
const { result } = renderHook(() =>
20+
useOffsetPagination({
21+
initialPage: 3,
22+
pageSize: 20,
23+
total: 100
24+
})
25+
);
26+
27+
expect(result.current.page).toBe(3);
28+
expect(result.current.currentPageSize).toBe(20);
29+
expect(result.current.pageCount).toBe(5);
30+
expect(result.current.isFirstPage).toBe(false);
31+
expect(result.current.isLastPage).toBe(false);
32+
});
33+
34+
it('Should calculate page count', () => {
35+
const { result } = renderHook(() =>
36+
useOffsetPagination({
37+
total: 95,
38+
pageSize: 10
39+
})
40+
);
41+
42+
expect(result.current.pageCount).toBe(10);
43+
});
44+
45+
it('Should return minimum 1 page when total is 0', () => {
46+
const { result } = renderHook(() =>
47+
useOffsetPagination({
48+
total: 0,
49+
pageSize: 10
50+
})
51+
);
52+
53+
expect(result.current.pageCount).toBe(1);
54+
});
55+
56+
it('Should detect first page', () => {
57+
const { result: result1 } = renderHook(() =>
58+
useOffsetPagination({ initialPage: 1, total: 100, pageSize: 10 })
59+
);
60+
expect(result1.current.isFirstPage).toBe(true);
61+
62+
const { result: result2 } = renderHook(() =>
63+
useOffsetPagination({ initialPage: 2, total: 100, pageSize: 10 })
64+
);
65+
expect(result2.current.isFirstPage).toBe(false);
66+
});
67+
68+
it('Should detect last page', () => {
69+
const { result: result1 } = renderHook(() =>
70+
useOffsetPagination({ initialPage: 10, total: 100, pageSize: 10 })
71+
);
72+
expect(result1.current.isLastPage).toBe(true);
73+
74+
const { result: result2 } = renderHook(() =>
75+
useOffsetPagination({ initialPage: 9, total: 100, pageSize: 10 })
76+
);
77+
expect(result2.current.isLastPage).toBe(false);
78+
});
79+
80+
it('Should go to next page', () => {
81+
const onPageChange = vi.fn();
82+
const { result } = renderHook(() =>
83+
useOffsetPagination({
84+
initialPage: 1,
85+
total: 100,
86+
pageSize: 10,
87+
onPageChange
88+
})
89+
);
90+
91+
act(() => {
92+
result.current.next();
93+
});
94+
95+
expect(result.current.page).toBe(2);
96+
expect(onPageChange).toHaveBeenCalledWith({ page: 2, pageSize: 10 });
97+
});
98+
99+
it('Should go to previous page', () => {
100+
const onPageChange = vi.fn();
101+
const { result } = renderHook(() =>
102+
useOffsetPagination({
103+
initialPage: 3,
104+
total: 100,
105+
pageSize: 10,
106+
onPageChange
107+
})
108+
);
109+
110+
act(() => {
111+
result.current.prev();
112+
});
113+
114+
expect(result.current.page).toBe(2);
115+
expect(onPageChange).toHaveBeenCalledWith({ page: 2, pageSize: 10 });
116+
});
117+
118+
it('Should set specific page', () => {
119+
const onPageChange = vi.fn();
120+
const { result } = renderHook(() =>
121+
useOffsetPagination({
122+
initialPage: 1,
123+
total: 100,
124+
pageSize: 10,
125+
onPageChange
126+
})
127+
);
128+
129+
act(() => {
130+
result.current.set(5);
131+
});
132+
133+
expect(result.current.page).toBe(5);
134+
expect(onPageChange).toHaveBeenCalledWith({ page: 5, pageSize: 10 });
135+
});
136+
137+
it('Should not go beyond last page on next', () => {
138+
const onPageChange = vi.fn();
139+
const { result } = renderHook(() =>
140+
useOffsetPagination({
141+
initialPage: 10,
142+
total: 100,
143+
pageSize: 10,
144+
onPageChange
145+
})
146+
);
147+
148+
expect(result.current.isLastPage).toBe(true);
149+
150+
act(() => {
151+
result.current.next();
152+
});
153+
154+
expect(result.current.page).toBe(10);
155+
expect(onPageChange).toHaveBeenCalledWith({ page: 10, pageSize: 10 });
156+
});
157+
158+
it('Should not go before first page on prev', () => {
159+
const onPageChange = vi.fn();
160+
const { result } = renderHook(() =>
161+
useOffsetPagination({
162+
initialPage: 1,
163+
total: 100,
164+
pageSize: 10,
165+
onPageChange
166+
})
167+
);
168+
169+
expect(result.current.isFirstPage).toBe(true);
170+
171+
act(() => {
172+
result.current.prev();
173+
});
174+
175+
expect(result.current.page).toBe(1);
176+
expect(onPageChange).toHaveBeenCalledWith({ page: 1, pageSize: 10 });
177+
});
178+
179+
it('Should call onPageCountChange when page count changes', () => {
180+
const onPageCountChange = vi.fn();
181+
const { rerender } = renderHook(
182+
({ total }) =>
183+
useOffsetPagination({
184+
total,
185+
pageSize: 10,
186+
onPageCountChange
187+
}),
188+
{ initialProps: { total: 100 } }
189+
);
190+
191+
expect(onPageCountChange).toHaveBeenCalledWith({ page: 1, pageSize: 10 });
192+
193+
rerender({ total: 50 });
194+
195+
expect(onPageCountChange).toHaveBeenCalledTimes(2);
196+
});
197+
198+
it('Should call onPageSizeChange when page size changes', () => {
199+
const onPageSizeChange = vi.fn();
200+
const { rerender } = renderHook(
201+
({ pageSize }) =>
202+
useOffsetPagination({
203+
total: 100,
204+
pageSize,
205+
onPageSizeChange
206+
}),
207+
{ initialProps: { pageSize: 10 } }
208+
);
209+
210+
expect(onPageSizeChange).toHaveBeenCalledWith({ page: 1, pageSize: 10 });
211+
212+
rerender({ pageSize: 20 });
213+
214+
expect(onPageSizeChange).toHaveBeenCalledTimes(2);
215+
expect(onPageSizeChange).toHaveBeenLastCalledWith({ page: 1, pageSize: 20 });
216+
});
217+
218+
it('Should update callback refs without rerender', () => {
219+
const onPageChange1 = vi.fn();
220+
const onPageChange2 = vi.fn();
221+
222+
const { result, rerender } = renderHook(
223+
({ onPageChange }) =>
224+
useOffsetPagination({
225+
total: 100,
226+
pageSize: 10,
227+
onPageChange
228+
}),
229+
{ initialProps: { onPageChange: onPageChange1 } }
230+
);
231+
232+
rerender({ onPageChange: onPageChange2 });
233+
234+
act(() => {
235+
result.current.next();
236+
});
237+
238+
expect(onPageChange1).not.toHaveBeenCalled();
239+
expect(onPageChange2).toHaveBeenCalledWith({ page: 2, pageSize: 10 });
240+
});
241+
242+
it('Should work with infinite total', () => {
243+
const { result } = renderHook(() =>
244+
useOffsetPagination({
245+
total: Number.POSITIVE_INFINITY,
246+
pageSize: 10
247+
})
248+
);
249+
250+
expect(result.current.pageCount).toBe(Number.POSITIVE_INFINITY);
251+
expect(result.current.isLastPage).toBe(false);
252+
253+
act(() => {
254+
result.current.next();
255+
});
256+
257+
expect(result.current.page).toBe(2);
258+
expect(result.current.isLastPage).toBe(false);
259+
});
260+
261+
it('Should work with different page sizes', () => {
262+
const { result: result1 } = renderHook(() =>
263+
useOffsetPagination({
264+
total: 100,
265+
pageSize: 25
266+
})
267+
);
268+
269+
expect(result1.current.pageCount).toBe(4);
270+
271+
const { result: result2 } = renderHook(() =>
272+
useOffsetPagination({
273+
total: 100,
274+
pageSize: 33
275+
})
276+
);
277+
278+
expect(result2.current.pageCount).toBe(4);
279+
});
280+
281+
it('Should handle sequential navigation', () => {
282+
const onPageChange = vi.fn();
283+
const { result } = renderHook(() =>
284+
useOffsetPagination({
285+
initialPage: 5,
286+
total: 100,
287+
pageSize: 10,
288+
onPageChange
289+
})
290+
);
291+
292+
act(() => {
293+
result.current.prev();
294+
});
295+
expect(result.current.page).toBe(4);
296+
297+
act(() => {
298+
result.current.prev();
299+
});
300+
expect(result.current.page).toBe(3);
301+
302+
act(() => {
303+
result.current.next();
304+
});
305+
expect(result.current.page).toBe(4);
306+
307+
act(() => {
308+
result.current.next();
309+
});
310+
expect(result.current.page).toBe(5);
311+
312+
expect(onPageChange).toHaveBeenCalledTimes(4);
313+
});
314+
315+
it('Should handle zero page size', () => {
316+
const { result } = renderHook(() =>
317+
useOffsetPagination({
318+
total: 100,
319+
pageSize: 0
320+
})
321+
);
322+
323+
expect(result.current.pageCount).toBe(Number.POSITIVE_INFINITY);
324+
expect(result.current.currentPageSize).toBe(0);
325+
});

0 commit comments

Comments
 (0)