|
1 | | -import { describe, test, expect, vi } from 'vitest' |
| 1 | +import { describe, test, expect } from 'vitest' |
2 | 2 | import { renderHook, act } from '@testing-library/react' |
3 | 3 | import { usePagination, extractPaginationMeta } from './usePagination' |
4 | | -import type { PaginationItemsPerPage } from '@/components/Common/PaginationControl/PaginationControlTypes' |
5 | 4 |
|
6 | 5 | const createMockHttpMeta = (headers: Record<string, string> = {}) => ({ |
7 | 6 | response: { |
@@ -54,169 +53,124 @@ describe('extractPaginationMeta', () => { |
54 | 53 | }) |
55 | 54 |
|
56 | 55 | describe('usePagination', () => { |
57 | | - const createState = ( |
58 | | - overrides: Partial<{ |
59 | | - currentPage: number |
60 | | - itemsPerPage: PaginationItemsPerPage |
61 | | - setCurrentPage: React.Dispatch<React.SetStateAction<number>> |
62 | | - setItemsPerPage: React.Dispatch<React.SetStateAction<PaginationItemsPerPage>> |
63 | | - }> = {}, |
64 | | - ) => ({ |
65 | | - currentPage: 1, |
66 | | - itemsPerPage: 10 as PaginationItemsPerPage, |
67 | | - setCurrentPage: vi.fn(), |
68 | | - setItemsPerPage: vi.fn(), |
69 | | - ...overrides, |
70 | | - }) |
71 | | - |
72 | | - describe('state management', () => { |
73 | | - test('returns currentPage from state', () => { |
74 | | - const httpMeta = createMockHttpMeta({ 'x-total-pages': '5', 'x-total-count': '50' }) |
75 | | - const state = createState({ currentPage: 3 }) |
| 56 | + describe('initial state', () => { |
| 57 | + test('defaults currentPage to 1', () => { |
| 58 | + const httpMeta = createMockHttpMeta({ 'x-total-pages': '5' }) |
| 59 | + const { result } = renderHook(() => usePagination(httpMeta)) |
| 60 | + expect(result.current.currentPage).toBe(1) |
| 61 | + }) |
76 | 62 |
|
77 | | - const { result } = renderHook(() => usePagination(httpMeta, state)) |
| 63 | + test('defaults itemsPerPage to 5', () => { |
| 64 | + const httpMeta = createMockHttpMeta({ 'x-total-pages': '5' }) |
| 65 | + const { result } = renderHook(() => usePagination(httpMeta)) |
| 66 | + expect(result.current.itemsPerPage).toBe(5) |
| 67 | + }) |
78 | 68 |
|
| 69 | + test('uses initialPage from options', () => { |
| 70 | + const httpMeta = createMockHttpMeta({ 'x-total-pages': '5' }) |
| 71 | + const { result } = renderHook(() => usePagination(httpMeta, { initialPage: 3 })) |
79 | 72 | expect(result.current.currentPage).toBe(3) |
80 | 73 | }) |
81 | 74 |
|
82 | | - test('returns itemsPerPage from state', () => { |
83 | | - const httpMeta = createMockHttpMeta({ 'x-total-pages': '5', 'x-total-count': '50' }) |
84 | | - const state = createState({ itemsPerPage: 50 }) |
85 | | - |
86 | | - const { result } = renderHook(() => usePagination(httpMeta, state)) |
87 | | - |
| 75 | + test('uses initialItemsPerPage from options', () => { |
| 76 | + const httpMeta = createMockHttpMeta({ 'x-total-pages': '5' }) |
| 77 | + const { result } = renderHook(() => usePagination(httpMeta, { initialItemsPerPage: 50 })) |
88 | 78 | expect(result.current.itemsPerPage).toBe(50) |
89 | 79 | }) |
| 80 | + }) |
90 | 81 |
|
| 82 | + describe('navigation handlers', () => { |
91 | 83 | test('handleFirstPage sets page to 1', () => { |
92 | | - const httpMeta = createMockHttpMeta({ 'x-total-pages': '5', 'x-total-count': '50' }) |
93 | | - const setCurrentPage = vi.fn() |
94 | | - const state = createState({ currentPage: 3, setCurrentPage }) |
95 | | - |
96 | | - const { result } = renderHook(() => usePagination(httpMeta, state)) |
| 84 | + const httpMeta = createMockHttpMeta({ 'x-total-pages': '5' }) |
| 85 | + const { result } = renderHook(() => usePagination(httpMeta, { initialPage: 3 })) |
97 | 86 |
|
98 | 87 | act(() => { |
99 | 88 | result.current.handleFirstPage() |
100 | 89 | }) |
101 | 90 |
|
102 | | - expect(setCurrentPage).toHaveBeenCalledWith(1) |
| 91 | + expect(result.current.currentPage).toBe(1) |
103 | 92 | }) |
104 | 93 |
|
105 | 94 | test('handlePreviousPage decrements page', () => { |
106 | | - const httpMeta = createMockHttpMeta({ 'x-total-pages': '5', 'x-total-count': '50' }) |
107 | | - const setCurrentPage = vi.fn() |
108 | | - const state = createState({ currentPage: 3, setCurrentPage }) |
109 | | - |
110 | | - const { result } = renderHook(() => usePagination(httpMeta, state)) |
| 95 | + const httpMeta = createMockHttpMeta({ 'x-total-pages': '5' }) |
| 96 | + const { result } = renderHook(() => usePagination(httpMeta, { initialPage: 3 })) |
111 | 97 |
|
112 | 98 | act(() => { |
113 | 99 | result.current.handlePreviousPage() |
114 | 100 | }) |
115 | 101 |
|
116 | | - expect(setCurrentPage).toHaveBeenCalled() |
117 | | - const updateFn = setCurrentPage.mock.calls[0]?.[0] as ((prev: number) => number) | undefined |
118 | | - expect(updateFn).toBeDefined() |
119 | | - expect(updateFn?.(3)).toBe(2) |
| 102 | + expect(result.current.currentPage).toBe(2) |
120 | 103 | }) |
121 | 104 |
|
122 | 105 | test('handleNextPage increments page', () => { |
123 | | - const httpMeta = createMockHttpMeta({ 'x-total-pages': '5', 'x-total-count': '50' }) |
124 | | - const setCurrentPage = vi.fn() |
125 | | - const state = createState({ currentPage: 3, setCurrentPage }) |
126 | | - |
127 | | - const { result } = renderHook(() => usePagination(httpMeta, state)) |
| 106 | + const httpMeta = createMockHttpMeta({ 'x-total-pages': '5' }) |
| 107 | + const { result } = renderHook(() => usePagination(httpMeta, { initialPage: 3 })) |
128 | 108 |
|
129 | 109 | act(() => { |
130 | 110 | result.current.handleNextPage() |
131 | 111 | }) |
132 | 112 |
|
133 | | - expect(setCurrentPage).toHaveBeenCalled() |
134 | | - const updateFn = setCurrentPage.mock.calls[0]?.[0] as ((prev: number) => number) | undefined |
135 | | - expect(updateFn).toBeDefined() |
136 | | - expect(updateFn?.(3)).toBe(4) |
| 113 | + expect(result.current.currentPage).toBe(4) |
137 | 114 | }) |
138 | 115 |
|
139 | 116 | test('handleLastPage sets page to totalPages', () => { |
140 | | - const httpMeta = createMockHttpMeta({ 'x-total-pages': '5', 'x-total-count': '50' }) |
141 | | - const setCurrentPage = vi.fn() |
142 | | - const state = createState({ setCurrentPage }) |
143 | | - |
144 | | - const { result } = renderHook(() => usePagination(httpMeta, state)) |
| 117 | + const httpMeta = createMockHttpMeta({ 'x-total-pages': '5' }) |
| 118 | + const { result } = renderHook(() => usePagination(httpMeta)) |
145 | 119 |
|
146 | 120 | act(() => { |
147 | 121 | result.current.handleLastPage() |
148 | 122 | }) |
149 | 123 |
|
150 | | - expect(setCurrentPage).toHaveBeenCalledWith(5) |
| 124 | + expect(result.current.currentPage).toBe(5) |
151 | 125 | }) |
152 | 126 |
|
153 | 127 | test('handleItemsPerPageChange updates itemsPerPage and resets to page 1', () => { |
154 | | - const httpMeta = createMockHttpMeta({ 'x-total-pages': '5', 'x-total-count': '50' }) |
155 | | - const setCurrentPage = vi.fn() |
156 | | - const setItemsPerPage = vi.fn() |
157 | | - const state = createState({ currentPage: 3, setCurrentPage, setItemsPerPage }) |
158 | | - |
159 | | - const { result } = renderHook(() => usePagination(httpMeta, state)) |
| 128 | + const httpMeta = createMockHttpMeta({ 'x-total-pages': '5' }) |
| 129 | + const { result } = renderHook(() => usePagination(httpMeta, { initialPage: 3 })) |
160 | 130 |
|
161 | 131 | act(() => { |
162 | 132 | result.current.handleItemsPerPageChange(50) |
163 | 133 | }) |
164 | 134 |
|
165 | | - expect(setItemsPerPage).toHaveBeenCalledWith(50) |
166 | | - expect(setCurrentPage).toHaveBeenCalledWith(1) |
| 135 | + expect(result.current.itemsPerPage).toBe(50) |
| 136 | + expect(result.current.currentPage).toBe(1) |
167 | 137 | }) |
168 | 138 | }) |
169 | 139 |
|
170 | 140 | describe('edge cases', () => { |
171 | 141 | test('handlePreviousPage on page 1 stays on page 1', () => { |
172 | | - const httpMeta = createMockHttpMeta({ 'x-total-pages': '5', 'x-total-count': '50' }) |
173 | | - const setCurrentPage = vi.fn() |
174 | | - const state = createState({ currentPage: 1, setCurrentPage }) |
175 | | - |
176 | | - const { result } = renderHook(() => usePagination(httpMeta, state)) |
| 142 | + const httpMeta = createMockHttpMeta({ 'x-total-pages': '5' }) |
| 143 | + const { result } = renderHook(() => usePagination(httpMeta, { initialPage: 1 })) |
177 | 144 |
|
178 | 145 | act(() => { |
179 | 146 | result.current.handlePreviousPage() |
180 | 147 | }) |
181 | 148 |
|
182 | | - const updateFn = setCurrentPage.mock.calls[0]?.[0] as ((prev: number) => number) | undefined |
183 | | - expect(updateFn).toBeDefined() |
184 | | - expect(updateFn?.(1)).toBe(1) |
| 149 | + expect(result.current.currentPage).toBe(1) |
185 | 150 | }) |
186 | 151 |
|
187 | 152 | test('handleNextPage on last page stays on last page', () => { |
188 | | - const httpMeta = createMockHttpMeta({ 'x-total-pages': '5', 'x-total-count': '50' }) |
189 | | - const setCurrentPage = vi.fn() |
190 | | - const state = createState({ currentPage: 5, setCurrentPage }) |
191 | | - |
192 | | - const { result } = renderHook(() => usePagination(httpMeta, state)) |
| 153 | + const httpMeta = createMockHttpMeta({ 'x-total-pages': '5' }) |
| 154 | + const { result } = renderHook(() => usePagination(httpMeta, { initialPage: 5 })) |
193 | 155 |
|
194 | 156 | act(() => { |
195 | 157 | result.current.handleNextPage() |
196 | 158 | }) |
197 | 159 |
|
198 | | - const updateFn = setCurrentPage.mock.calls[0]?.[0] as ((prev: number) => number) | undefined |
199 | | - expect(updateFn).toBeDefined() |
200 | | - expect(updateFn?.(5)).toBe(5) |
| 160 | + expect(result.current.currentPage).toBe(5) |
201 | 161 | }) |
202 | 162 | }) |
203 | 163 |
|
204 | 164 | describe('header extraction', () => { |
205 | 165 | test('returns totalPages from httpMeta', () => { |
206 | 166 | const httpMeta = createMockHttpMeta({ 'x-total-pages': '10', 'x-total-count': '100' }) |
207 | | - const state = createState() |
208 | | - |
209 | | - const { result } = renderHook(() => usePagination(httpMeta, state)) |
210 | | - |
| 167 | + const { result } = renderHook(() => usePagination(httpMeta)) |
211 | 168 | expect(result.current.totalPages).toBe(10) |
212 | 169 | }) |
213 | 170 |
|
214 | 171 | test('returns totalItems from httpMeta', () => { |
215 | 172 | const httpMeta = createMockHttpMeta({ 'x-total-pages': '10', 'x-total-count': '100' }) |
216 | | - const state = createState() |
217 | | - |
218 | | - const { result } = renderHook(() => usePagination(httpMeta, state)) |
219 | | - |
| 173 | + const { result } = renderHook(() => usePagination(httpMeta)) |
220 | 174 | expect(result.current.totalItems).toBe(100) |
221 | 175 | }) |
222 | 176 | }) |
|
0 commit comments