@yogiswara/honcho-editor-ui 1.3.8 → 1.3.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/hooks/__tests__/useGallerySwipe.test.d.ts +0 -0
- package/dist/hooks/__tests__/useGallerySwipe.test.js +619 -0
- package/dist/hooks/editor/useHonchoEditor.d.ts +27 -83
- package/dist/hooks/editor/useHonchoEditor.js +415 -288
- package/dist/hooks/useAdjustmentHistory.d.ts +91 -0
- package/dist/hooks/useAdjustmentHistory.demo.d.ts +8 -0
- package/dist/hooks/useAdjustmentHistory.demo.js +106 -0
- package/dist/hooks/useAdjustmentHistory.example.d.ts +33 -0
- package/dist/hooks/useAdjustmentHistory.example.js +150 -0
- package/dist/hooks/useAdjustmentHistory.js +277 -0
- package/dist/hooks/useGallerySwipe.d.ts +36 -0
- package/dist/hooks/useGallerySwipe.example.d.ts +24 -0
- package/dist/hooks/useGallerySwipe.example.js +184 -0
- package/dist/hooks/useGallerySwipe.js +321 -0
- package/dist/setupTests.d.ts +1 -0
- package/dist/setupTests.js +1 -0
- package/dist/utils/adjustment.d.ts +5 -0
- package/dist/utils/adjustment.js +32 -0
- package/package.json +11 -2
|
File without changes
|
|
@@ -0,0 +1,619 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// import { renderHook, waitFor, act } from '@testing-library/react';
|
|
3
|
+
// import { useGallerySwipe } from '../useGallerySwipe';
|
|
4
|
+
// import { Controller } from '../editor/useHonchoEditor';
|
|
5
|
+
// import { Gallery, ResponseGalleryPaging } from '../editor/type';
|
|
6
|
+
// // Mock data factory functions
|
|
7
|
+
// const createMockGallery = (id: string, eventId: string = 'event-123'): Gallery => ({
|
|
8
|
+
// id,
|
|
9
|
+
// uid: 'user-123',
|
|
10
|
+
// event_id: eventId,
|
|
11
|
+
// download: {
|
|
12
|
+
// key: `${id}-download`,
|
|
13
|
+
// path: `https://example.com/${id}-download.jpg`,
|
|
14
|
+
// size: 1024000,
|
|
15
|
+
// width: 1920,
|
|
16
|
+
// height: 1080,
|
|
17
|
+
// },
|
|
18
|
+
// download_edited: null,
|
|
19
|
+
// thumbnail: {
|
|
20
|
+
// key: `${id}-thumb`,
|
|
21
|
+
// path: `https://example.com/${id}-thumb.jpg`,
|
|
22
|
+
// size: 50000,
|
|
23
|
+
// width: 300,
|
|
24
|
+
// height: 200,
|
|
25
|
+
// },
|
|
26
|
+
// thumbnail_edited: null,
|
|
27
|
+
// is_original: true,
|
|
28
|
+
// available: true,
|
|
29
|
+
// show_gallery: true,
|
|
30
|
+
// log: {
|
|
31
|
+
// created_at: '2025-01-01T00:00:00Z',
|
|
32
|
+
// updated_at: '2025-01-01T00:00:00Z',
|
|
33
|
+
// },
|
|
34
|
+
// });
|
|
35
|
+
// const createMockPagingResponse = (
|
|
36
|
+
// images: Gallery[],
|
|
37
|
+
// currentPage: number = 1,
|
|
38
|
+
// nextPage: number = 0
|
|
39
|
+
// ): ResponseGalleryPaging => ({
|
|
40
|
+
// gallery: images,
|
|
41
|
+
// limit: 10,
|
|
42
|
+
// current_page: currentPage,
|
|
43
|
+
// prev_page: currentPage > 1 ? currentPage - 1 : 0,
|
|
44
|
+
// next_page: nextPage,
|
|
45
|
+
// sum_of_image: images.length,
|
|
46
|
+
// });
|
|
47
|
+
// // Mock controller factory
|
|
48
|
+
// const createMockController = (): jest.Mocked<Controller> => ({
|
|
49
|
+
// onGetImage: jest.fn(),
|
|
50
|
+
// getImageList: jest.fn(),
|
|
51
|
+
// syncConfig: jest.fn(),
|
|
52
|
+
// handleBack: jest.fn(),
|
|
53
|
+
// getPresets: jest.fn(),
|
|
54
|
+
// createPreset: jest.fn(),
|
|
55
|
+
// deletePreset: jest.fn(),
|
|
56
|
+
// });
|
|
57
|
+
// describe('useGallerySwipe', () => {
|
|
58
|
+
// let mockController: jest.Mocked<Controller>;
|
|
59
|
+
// beforeEach(() => {
|
|
60
|
+
// mockController = createMockController();
|
|
61
|
+
// jest.clearAllMocks();
|
|
62
|
+
// });
|
|
63
|
+
// describe('Initialization', () => {
|
|
64
|
+
// it('should return initial state when parameters are null', () => {
|
|
65
|
+
// const { result } = renderHook(() =>
|
|
66
|
+
// useGallerySwipe(null, null, null)
|
|
67
|
+
// );
|
|
68
|
+
// expect(result.current.currentImageData).toBeNull();
|
|
69
|
+
// expect(result.current.isNextAvailable).toBe(false);
|
|
70
|
+
// expect(result.current.isPrevAvailable).toBe(false);
|
|
71
|
+
// expect(result.current.isLoading).toBe(false);
|
|
72
|
+
// expect(result.current.error).toBeNull();
|
|
73
|
+
// });
|
|
74
|
+
// it('should initialize successfully with valid parameters', async () => {
|
|
75
|
+
// const firebaseUid = 'user-123';
|
|
76
|
+
// const initImageId = 'image-1';
|
|
77
|
+
// const mockGallery = createMockGallery(initImageId);
|
|
78
|
+
// const mockImages = [
|
|
79
|
+
// createMockGallery('image-1'),
|
|
80
|
+
// createMockGallery('image-2'),
|
|
81
|
+
// createMockGallery('image-3'),
|
|
82
|
+
// ];
|
|
83
|
+
// // Mock controller responses
|
|
84
|
+
// mockController.onGetImage.mockResolvedValueOnce(mockGallery);
|
|
85
|
+
// mockController.getImageList.mockResolvedValueOnce(
|
|
86
|
+
// createMockPagingResponse(mockImages, 1, 2)
|
|
87
|
+
// );
|
|
88
|
+
// const { result } = renderHook(() =>
|
|
89
|
+
// useGallerySwipe(firebaseUid, initImageId, mockController)
|
|
90
|
+
// );
|
|
91
|
+
// // Initial loading state
|
|
92
|
+
// expect(result.current.isLoading).toBe(true);
|
|
93
|
+
// // Wait for initialization to complete
|
|
94
|
+
// await waitFor(() => {
|
|
95
|
+
// expect(result.current.isLoading).toBe(false);
|
|
96
|
+
// });
|
|
97
|
+
// // Verify successful initialization
|
|
98
|
+
// expect(result.current.currentImageData).toEqual(mockGallery);
|
|
99
|
+
// expect(result.current.error).toBeNull();
|
|
100
|
+
// expect(result.current.isNextAvailable).toBe(true);
|
|
101
|
+
// expect(result.current.isPrevAvailable).toBe(false);
|
|
102
|
+
// // Verify controller calls
|
|
103
|
+
// expect(mockController.onGetImage).toHaveBeenCalledWith(firebaseUid, initImageId);
|
|
104
|
+
// expect(mockController.getImageList).toHaveBeenCalledWith(firebaseUid, 'event-123', 1);
|
|
105
|
+
// });
|
|
106
|
+
// it('should handle initialization failure gracefully', async () => {
|
|
107
|
+
// const firebaseUid = 'user-123';
|
|
108
|
+
// const initImageId = 'image-1';
|
|
109
|
+
// // Mock controller to throw error
|
|
110
|
+
// mockController.onGetImage.mockRejectedValueOnce(new Error('Failed to fetch image'));
|
|
111
|
+
// const { result } = renderHook(() =>
|
|
112
|
+
// useGallerySwipe(firebaseUid, initImageId, mockController)
|
|
113
|
+
// );
|
|
114
|
+
// // Wait for error to be set
|
|
115
|
+
// await waitFor(() => {
|
|
116
|
+
// expect(result.current.error).toBe('Failed to fetch image');
|
|
117
|
+
// });
|
|
118
|
+
// expect(result.current.currentImageData).toBeNull();
|
|
119
|
+
// expect(result.current.isLoading).toBe(false);
|
|
120
|
+
// });
|
|
121
|
+
// it('should not re-initialize when called with same parameters', async () => {
|
|
122
|
+
// const firebaseUid = 'user-123';
|
|
123
|
+
// const initImageId = 'image-1';
|
|
124
|
+
// const mockGallery = createMockGallery(initImageId);
|
|
125
|
+
// const mockImages = [createMockGallery('image-1')];
|
|
126
|
+
// mockController.onGetImage.mockResolvedValue(mockGallery);
|
|
127
|
+
// mockController.getImageList.mockResolvedValue(
|
|
128
|
+
// createMockPagingResponse(mockImages)
|
|
129
|
+
// );
|
|
130
|
+
// const { result, rerender } = renderHook(
|
|
131
|
+
// ({ uid, imageId, controller }) => useGallerySwipe(uid, imageId, controller),
|
|
132
|
+
// {
|
|
133
|
+
// initialProps: {
|
|
134
|
+
// uid: firebaseUid,
|
|
135
|
+
// imageId: initImageId,
|
|
136
|
+
// controller: mockController,
|
|
137
|
+
// },
|
|
138
|
+
// }
|
|
139
|
+
// );
|
|
140
|
+
// await waitFor(() => {
|
|
141
|
+
// expect(result.current.isLoading).toBe(false);
|
|
142
|
+
// });
|
|
143
|
+
// // Clear mock calls after initial render
|
|
144
|
+
// jest.clearAllMocks();
|
|
145
|
+
// // Rerender with same props
|
|
146
|
+
// rerender({
|
|
147
|
+
// uid: firebaseUid,
|
|
148
|
+
// imageId: initImageId,
|
|
149
|
+
// controller: mockController,
|
|
150
|
+
// });
|
|
151
|
+
// // Should not call controller again
|
|
152
|
+
// expect(mockController.onGetImage).not.toHaveBeenCalled();
|
|
153
|
+
// expect(mockController.getImageList).not.toHaveBeenCalled();
|
|
154
|
+
// });
|
|
155
|
+
// });
|
|
156
|
+
// describe('Image Search Across Pages', () => {
|
|
157
|
+
// it('should find image across multiple pages', async () => {
|
|
158
|
+
// const firebaseUid = 'user-123';
|
|
159
|
+
// const initImageId = 'image-5'; // Image on page 2
|
|
160
|
+
// const mockGallery = createMockGallery(initImageId);
|
|
161
|
+
// // Mock first page without target image
|
|
162
|
+
// const page1Images = [
|
|
163
|
+
// createMockGallery('image-1'),
|
|
164
|
+
// createMockGallery('image-2'),
|
|
165
|
+
// ];
|
|
166
|
+
// // Mock second page with target image
|
|
167
|
+
// const page2Images = [
|
|
168
|
+
// createMockGallery('image-3'),
|
|
169
|
+
// createMockGallery('image-4'),
|
|
170
|
+
// createMockGallery('image-5'),
|
|
171
|
+
// ];
|
|
172
|
+
// mockController.onGetImage.mockResolvedValueOnce(mockGallery);
|
|
173
|
+
// mockController.getImageList
|
|
174
|
+
// .mockResolvedValueOnce(createMockPagingResponse(page1Images, 1, 2))
|
|
175
|
+
// .mockResolvedValueOnce(createMockPagingResponse(page2Images, 2, 0));
|
|
176
|
+
// const { result } = renderHook(() =>
|
|
177
|
+
// useGallerySwipe(firebaseUid, initImageId, mockController)
|
|
178
|
+
// );
|
|
179
|
+
// await waitFor(() => {
|
|
180
|
+
// expect(result.current.isLoading).toBe(false);
|
|
181
|
+
// });
|
|
182
|
+
// // Should have called getImageList twice (page 1 and page 2)
|
|
183
|
+
// expect(mockController.getImageList).toHaveBeenCalledTimes(2);
|
|
184
|
+
// expect(mockController.getImageList).toHaveBeenNthCalledWith(1, firebaseUid, 'event-123', 1);
|
|
185
|
+
// expect(mockController.getImageList).toHaveBeenNthCalledWith(2, firebaseUid, 'event-123', 2);
|
|
186
|
+
// // Should have correct availability
|
|
187
|
+
// expect(result.current.isPrevAvailable).toBe(true); // Has previous images from page 1 and 2
|
|
188
|
+
// expect(result.current.isNextAvailable).toBe(false); // No next page available
|
|
189
|
+
// });
|
|
190
|
+
// it('should handle safety limit when image not found', async () => {
|
|
191
|
+
// const firebaseUid = 'user-123';
|
|
192
|
+
// const initImageId = 'non-existent-image';
|
|
193
|
+
// const mockGallery = createMockGallery(initImageId);
|
|
194
|
+
// mockController.onGetImage.mockResolvedValueOnce(mockGallery);
|
|
195
|
+
// // Mock getImageList to always return different images (never the target)
|
|
196
|
+
// const mockPage = [createMockGallery('other-image')];
|
|
197
|
+
// mockController.getImageList.mockResolvedValue(
|
|
198
|
+
// createMockPagingResponse(mockPage, 1, 2) // Always has next page
|
|
199
|
+
// );
|
|
200
|
+
// const { result } = renderHook(() =>
|
|
201
|
+
// useGallerySwipe(firebaseUid, initImageId, mockController)
|
|
202
|
+
// );
|
|
203
|
+
// await waitFor(() => {
|
|
204
|
+
// expect(result.current.isLoading).toBe(false);
|
|
205
|
+
// });
|
|
206
|
+
// // Should have called getImageList many times due to safety limit
|
|
207
|
+
// expect(mockController.getImageList.mock.calls.length).toBeGreaterThan(50);
|
|
208
|
+
// });
|
|
209
|
+
// });
|
|
210
|
+
// describe('Navigation - Next Image', () => {
|
|
211
|
+
// it('should navigate to next image within current page', async () => {
|
|
212
|
+
// const firebaseUid = 'user-123';
|
|
213
|
+
// const initImageId = 'image-1';
|
|
214
|
+
// const mockImages = [
|
|
215
|
+
// createMockGallery('image-1'),
|
|
216
|
+
// createMockGallery('image-2'),
|
|
217
|
+
// createMockGallery('image-3'),
|
|
218
|
+
// ];
|
|
219
|
+
// const nextImageGallery = createMockGallery('image-2');
|
|
220
|
+
// // Setup initial state
|
|
221
|
+
// mockController.onGetImage
|
|
222
|
+
// .mockResolvedValueOnce(createMockGallery(initImageId))
|
|
223
|
+
// .mockResolvedValueOnce(nextImageGallery);
|
|
224
|
+
// mockController.getImageList.mockResolvedValueOnce(
|
|
225
|
+
// createMockPagingResponse(mockImages)
|
|
226
|
+
// );
|
|
227
|
+
// const { result } = renderHook(() =>
|
|
228
|
+
// useGallerySwipe(firebaseUid, initImageId, mockController)
|
|
229
|
+
// );
|
|
230
|
+
// await waitFor(() => {
|
|
231
|
+
// expect(result.current.isLoading).toBe(false);
|
|
232
|
+
// });
|
|
233
|
+
// // Clear mocks after initialization
|
|
234
|
+
// jest.clearAllMocks();
|
|
235
|
+
// mockController.onGetImage.mockResolvedValueOnce(nextImageGallery);
|
|
236
|
+
// // Navigate to next image
|
|
237
|
+
// await act(async () => {
|
|
238
|
+
// await result.current.onSwipeNext();
|
|
239
|
+
// });
|
|
240
|
+
// await waitFor(() => {
|
|
241
|
+
// expect(result.current.currentImageData).toEqual(nextImageGallery);
|
|
242
|
+
// });
|
|
243
|
+
// // Should have fetched the next image data
|
|
244
|
+
// expect(mockController.onGetImage).toHaveBeenCalledWith(firebaseUid, 'image-2');
|
|
245
|
+
// // Should not have called getImageList again
|
|
246
|
+
// expect(mockController.getImageList).not.toHaveBeenCalled();
|
|
247
|
+
// });
|
|
248
|
+
// it('should load next page when at end of current page', async () => {
|
|
249
|
+
// const firebaseUid = 'user-123';
|
|
250
|
+
// const initImageId = 'image-2';
|
|
251
|
+
// // Initial page
|
|
252
|
+
// const page1Images = [
|
|
253
|
+
// createMockGallery('image-1'),
|
|
254
|
+
// createMockGallery('image-2'),
|
|
255
|
+
// ];
|
|
256
|
+
// // Next page
|
|
257
|
+
// const page2Images = [
|
|
258
|
+
// createMockGallery('image-3'),
|
|
259
|
+
// createMockGallery('image-4'),
|
|
260
|
+
// ];
|
|
261
|
+
// const nextImageGallery = createMockGallery('image-3');
|
|
262
|
+
// // Setup initial state
|
|
263
|
+
// mockController.onGetImage
|
|
264
|
+
// .mockResolvedValueOnce(createMockGallery(initImageId))
|
|
265
|
+
// .mockResolvedValueOnce(nextImageGallery);
|
|
266
|
+
// mockController.getImageList
|
|
267
|
+
// .mockResolvedValueOnce(createMockPagingResponse(page1Images, 1, 2))
|
|
268
|
+
// .mockResolvedValueOnce(createMockPagingResponse(page2Images, 2, 0));
|
|
269
|
+
// const { result } = renderHook(() =>
|
|
270
|
+
// useGallerySwipe(firebaseUid, initImageId, mockController)
|
|
271
|
+
// );
|
|
272
|
+
// await waitFor(() => {
|
|
273
|
+
// expect(result.current.isLoading).toBe(false);
|
|
274
|
+
// });
|
|
275
|
+
// // Clear mocks after initialization
|
|
276
|
+
// jest.clearAllMocks();
|
|
277
|
+
// mockController.onGetImage.mockResolvedValueOnce(nextImageGallery);
|
|
278
|
+
// mockController.getImageList.mockResolvedValueOnce(
|
|
279
|
+
// createMockPagingResponse(page2Images, 2, 0)
|
|
280
|
+
// );
|
|
281
|
+
// // Navigate to next image (should trigger page load)
|
|
282
|
+
// await act(async () => {
|
|
283
|
+
// await result.current.onSwipeNext();
|
|
284
|
+
// });
|
|
285
|
+
// await waitFor(() => {
|
|
286
|
+
// expect(result.current.currentImageData).toEqual(nextImageGallery);
|
|
287
|
+
// });
|
|
288
|
+
// // Should have loaded next page
|
|
289
|
+
// expect(mockController.getImageList).toHaveBeenCalledWith(firebaseUid, 'event-123', 2);
|
|
290
|
+
// expect(mockController.onGetImage).toHaveBeenCalledWith(firebaseUid, 'image-3');
|
|
291
|
+
// });
|
|
292
|
+
// it('should handle end of gallery gracefully', async () => {
|
|
293
|
+
// const firebaseUid = 'user-123';
|
|
294
|
+
// const initImageId = 'image-2';
|
|
295
|
+
// const mockImages = [
|
|
296
|
+
// createMockGallery('image-1'),
|
|
297
|
+
// createMockGallery('image-2'),
|
|
298
|
+
// ];
|
|
299
|
+
// // Setup initial state - no next page available
|
|
300
|
+
// mockController.onGetImage.mockResolvedValueOnce(createMockGallery(initImageId));
|
|
301
|
+
// mockController.getImageList.mockResolvedValueOnce(
|
|
302
|
+
// createMockPagingResponse(mockImages, 1, 0) // No next page
|
|
303
|
+
// );
|
|
304
|
+
// const { result } = renderHook(() =>
|
|
305
|
+
// useGallerySwipe(firebaseUid, initImageId, mockController)
|
|
306
|
+
// );
|
|
307
|
+
// await waitFor(() => {
|
|
308
|
+
// expect(result.current.isLoading).toBe(false);
|
|
309
|
+
// });
|
|
310
|
+
// expect(result.current.isNextAvailable).toBe(false);
|
|
311
|
+
// // Try to navigate next (should set error)
|
|
312
|
+
// await act(async () => {
|
|
313
|
+
// await result.current.onSwipeNext();
|
|
314
|
+
// });
|
|
315
|
+
// await waitFor(() => {
|
|
316
|
+
// expect(result.current.error).toBe('No more images available');
|
|
317
|
+
// });
|
|
318
|
+
// });
|
|
319
|
+
// });
|
|
320
|
+
// describe('Navigation - Previous Image', () => {
|
|
321
|
+
// it('should navigate to previous image', async () => {
|
|
322
|
+
// const firebaseUid = 'user-123';
|
|
323
|
+
// const initImageId = 'image-2';
|
|
324
|
+
// const mockImages = [
|
|
325
|
+
// createMockGallery('image-1'),
|
|
326
|
+
// createMockGallery('image-2'),
|
|
327
|
+
// createMockGallery('image-3'),
|
|
328
|
+
// ];
|
|
329
|
+
// const prevImageGallery = createMockGallery('image-1');
|
|
330
|
+
// // Setup initial state
|
|
331
|
+
// mockController.onGetImage
|
|
332
|
+
// .mockResolvedValueOnce(createMockGallery(initImageId))
|
|
333
|
+
// .mockResolvedValueOnce(prevImageGallery);
|
|
334
|
+
// mockController.getImageList.mockResolvedValueOnce(
|
|
335
|
+
// createMockPagingResponse(mockImages)
|
|
336
|
+
// );
|
|
337
|
+
// const { result } = renderHook(() =>
|
|
338
|
+
// useGallerySwipe(firebaseUid, initImageId, mockController)
|
|
339
|
+
// );
|
|
340
|
+
// await waitFor(() => {
|
|
341
|
+
// expect(result.current.isLoading).toBe(false);
|
|
342
|
+
// });
|
|
343
|
+
// // Clear mocks after initialization
|
|
344
|
+
// jest.clearAllMocks();
|
|
345
|
+
// mockController.onGetImage.mockResolvedValueOnce(prevImageGallery);
|
|
346
|
+
// // Navigate to previous image
|
|
347
|
+
// await act(async () => {
|
|
348
|
+
// await result.current.onSwipePrev();
|
|
349
|
+
// });
|
|
350
|
+
// await waitFor(() => {
|
|
351
|
+
// expect(result.current.currentImageData).toEqual(prevImageGallery);
|
|
352
|
+
// });
|
|
353
|
+
// expect(mockController.onGetImage).toHaveBeenCalledWith(firebaseUid, 'image-1');
|
|
354
|
+
// });
|
|
355
|
+
// it('should handle beginning of gallery gracefully', async () => {
|
|
356
|
+
// const firebaseUid = 'user-123';
|
|
357
|
+
// const initImageId = 'image-1';
|
|
358
|
+
// const mockImages = [
|
|
359
|
+
// createMockGallery('image-1'),
|
|
360
|
+
// createMockGallery('image-2'),
|
|
361
|
+
// ];
|
|
362
|
+
// // Setup initial state
|
|
363
|
+
// mockController.onGetImage.mockResolvedValueOnce(createMockGallery(initImageId));
|
|
364
|
+
// mockController.getImageList.mockResolvedValueOnce(
|
|
365
|
+
// createMockPagingResponse(mockImages)
|
|
366
|
+
// );
|
|
367
|
+
// const { result } = renderHook(() =>
|
|
368
|
+
// useGallerySwipe(firebaseUid, initImageId, mockController)
|
|
369
|
+
// );
|
|
370
|
+
// await waitFor(() => {
|
|
371
|
+
// expect(result.current.isLoading).toBe(false);
|
|
372
|
+
// });
|
|
373
|
+
// expect(result.current.isPrevAvailable).toBe(false);
|
|
374
|
+
// // Try to navigate previous (should set error)
|
|
375
|
+
// await act(async () => {
|
|
376
|
+
// await result.current.onSwipePrev();
|
|
377
|
+
// });
|
|
378
|
+
// await waitFor(() => {
|
|
379
|
+
// expect(result.current.error).toBe('Already at the first image');
|
|
380
|
+
// });
|
|
381
|
+
// });
|
|
382
|
+
// });
|
|
383
|
+
// describe('Availability Calculations', () => {
|
|
384
|
+
// it('should calculate next availability correctly with more pages', async () => {
|
|
385
|
+
// const firebaseUid = 'user-123';
|
|
386
|
+
// const initImageId = 'image-2';
|
|
387
|
+
// const mockImages = [
|
|
388
|
+
// createMockGallery('image-1'),
|
|
389
|
+
// createMockGallery('image-2'),
|
|
390
|
+
// ];
|
|
391
|
+
// mockController.onGetImage.mockResolvedValueOnce(createMockGallery(initImageId));
|
|
392
|
+
// mockController.getImageList.mockResolvedValueOnce(
|
|
393
|
+
// createMockPagingResponse(mockImages, 1, 2) // Has next page
|
|
394
|
+
// );
|
|
395
|
+
// const { result } = renderHook(() =>
|
|
396
|
+
// useGallerySwipe(firebaseUid, initImageId, mockController)
|
|
397
|
+
// );
|
|
398
|
+
// await waitFor(() => {
|
|
399
|
+
// expect(result.current.isLoading).toBe(false);
|
|
400
|
+
// });
|
|
401
|
+
// // At last image of current page, but has next page
|
|
402
|
+
// expect(result.current.isNextAvailable).toBe(true);
|
|
403
|
+
// expect(result.current.isPrevAvailable).toBe(true);
|
|
404
|
+
// });
|
|
405
|
+
// it('should calculate availability correctly without more pages', async () => {
|
|
406
|
+
// const firebaseUid = 'user-123';
|
|
407
|
+
// const initImageId = 'image-2';
|
|
408
|
+
// const mockImages = [
|
|
409
|
+
// createMockGallery('image-1'),
|
|
410
|
+
// createMockGallery('image-2'),
|
|
411
|
+
// ];
|
|
412
|
+
// mockController.onGetImage.mockResolvedValueOnce(createMockGallery(initImageId));
|
|
413
|
+
// mockController.getImageList.mockResolvedValueOnce(
|
|
414
|
+
// createMockPagingResponse(mockImages, 1, 0) // No next page
|
|
415
|
+
// );
|
|
416
|
+
// const { result } = renderHook(() =>
|
|
417
|
+
// useGallerySwipe(firebaseUid, initImageId, mockController)
|
|
418
|
+
// );
|
|
419
|
+
// await waitFor(() => {
|
|
420
|
+
// expect(result.current.isLoading).toBe(false);
|
|
421
|
+
// });
|
|
422
|
+
// // At last image with no next page
|
|
423
|
+
// expect(result.current.isNextAvailable).toBe(false);
|
|
424
|
+
// expect(result.current.isPrevAvailable).toBe(true);
|
|
425
|
+
// });
|
|
426
|
+
// it('should disable navigation during loading', async () => {
|
|
427
|
+
// const firebaseUid = 'user-123';
|
|
428
|
+
// const initImageId = 'image-1';
|
|
429
|
+
// // Mock slow response
|
|
430
|
+
// mockController.onGetImage.mockImplementation(() =>
|
|
431
|
+
// new Promise(resolve => setTimeout(() => resolve(createMockGallery(initImageId)), 100))
|
|
432
|
+
// );
|
|
433
|
+
// mockController.getImageList.mockResolvedValueOnce(
|
|
434
|
+
// createMockPagingResponse([createMockGallery('image-1')])
|
|
435
|
+
// );
|
|
436
|
+
// const { result } = renderHook(() =>
|
|
437
|
+
// useGallerySwipe(firebaseUid, initImageId, mockController)
|
|
438
|
+
// );
|
|
439
|
+
// // During loading
|
|
440
|
+
// expect(result.current.isLoading).toBe(true);
|
|
441
|
+
// expect(result.current.isNextAvailable).toBe(false);
|
|
442
|
+
// expect(result.current.isPrevAvailable).toBe(false);
|
|
443
|
+
// await waitFor(() => {
|
|
444
|
+
// expect(result.current.isLoading).toBe(false);
|
|
445
|
+
// });
|
|
446
|
+
// });
|
|
447
|
+
// });
|
|
448
|
+
// describe('Error Handling', () => {
|
|
449
|
+
// it('should handle navigation errors gracefully', async () => {
|
|
450
|
+
// const firebaseUid = 'user-123';
|
|
451
|
+
// const initImageId = 'image-1';
|
|
452
|
+
// const mockImages = [
|
|
453
|
+
// createMockGallery('image-1'),
|
|
454
|
+
// createMockGallery('image-2'),
|
|
455
|
+
// ];
|
|
456
|
+
// // Setup successful initialization
|
|
457
|
+
// mockController.onGetImage.mockResolvedValueOnce(createMockGallery(initImageId));
|
|
458
|
+
// mockController.getImageList.mockResolvedValueOnce(
|
|
459
|
+
// createMockPagingResponse(mockImages)
|
|
460
|
+
// );
|
|
461
|
+
// const { result } = renderHook(() =>
|
|
462
|
+
// useGallerySwipe(firebaseUid, initImageId, mockController)
|
|
463
|
+
// );
|
|
464
|
+
// await waitFor(() => {
|
|
465
|
+
// expect(result.current.isLoading).toBe(false);
|
|
466
|
+
// });
|
|
467
|
+
// // Mock error on next navigation
|
|
468
|
+
// mockController.onGetImage.mockRejectedValueOnce(new Error('Network error'));
|
|
469
|
+
// await act(async () => {
|
|
470
|
+
// await result.current.onSwipeNext();
|
|
471
|
+
// });
|
|
472
|
+
// await waitFor(() => {
|
|
473
|
+
// expect(result.current.error).toBe('Network error');
|
|
474
|
+
// });
|
|
475
|
+
// });
|
|
476
|
+
// it('should handle page loading errors', async () => {
|
|
477
|
+
// const firebaseUid = 'user-123';
|
|
478
|
+
// const initImageId = 'image-1';
|
|
479
|
+
// const mockImages = [createMockGallery('image-1')];
|
|
480
|
+
// // Setup successful initialization with next page available
|
|
481
|
+
// mockController.onGetImage.mockResolvedValueOnce(createMockGallery(initImageId));
|
|
482
|
+
// mockController.getImageList
|
|
483
|
+
// .mockResolvedValueOnce(createMockPagingResponse(mockImages, 1, 2))
|
|
484
|
+
// .mockRejectedValueOnce(new Error('Failed to load page'));
|
|
485
|
+
// const { result } = renderHook(() =>
|
|
486
|
+
// useGallerySwipe(firebaseUid, initImageId, mockController)
|
|
487
|
+
// );
|
|
488
|
+
// await waitFor(() => {
|
|
489
|
+
// expect(result.current.isLoading).toBe(false);
|
|
490
|
+
// });
|
|
491
|
+
// // Try to navigate next (should trigger page load error)
|
|
492
|
+
// await act(async () => {
|
|
493
|
+
// await result.current.onSwipeNext();
|
|
494
|
+
// });
|
|
495
|
+
// await waitFor(() => {
|
|
496
|
+
// expect(result.current.error).toBe('No more images available');
|
|
497
|
+
// });
|
|
498
|
+
// });
|
|
499
|
+
// });
|
|
500
|
+
// describe('Edge Cases', () => {
|
|
501
|
+
// it('should handle empty image list gracefully', async () => {
|
|
502
|
+
// const firebaseUid = 'user-123';
|
|
503
|
+
// const initImageId = 'image-1';
|
|
504
|
+
// const mockGallery = createMockGallery(initImageId);
|
|
505
|
+
// mockController.onGetImage.mockResolvedValueOnce(mockGallery);
|
|
506
|
+
// mockController.getImageList.mockResolvedValueOnce(
|
|
507
|
+
// createMockPagingResponse([], 1, 0) // Empty image list
|
|
508
|
+
// );
|
|
509
|
+
// const { result } = renderHook(() =>
|
|
510
|
+
// useGallerySwipe(firebaseUid, initImageId, mockController)
|
|
511
|
+
// );
|
|
512
|
+
// await waitFor(() => {
|
|
513
|
+
// expect(result.current.isLoading).toBe(false);
|
|
514
|
+
// });
|
|
515
|
+
// expect(result.current.isNextAvailable).toBe(false);
|
|
516
|
+
// expect(result.current.isPrevAvailable).toBe(false);
|
|
517
|
+
// });
|
|
518
|
+
// it('should handle rapid navigation calls gracefully', async () => {
|
|
519
|
+
// const firebaseUid = 'user-123';
|
|
520
|
+
// const initImageId = 'image-1';
|
|
521
|
+
// const mockImages = [
|
|
522
|
+
// createMockGallery('image-1'),
|
|
523
|
+
// createMockGallery('image-2'),
|
|
524
|
+
// createMockGallery('image-3'),
|
|
525
|
+
// ];
|
|
526
|
+
// mockController.onGetImage.mockResolvedValue(createMockGallery('image-2'));
|
|
527
|
+
// mockController.getImageList.mockResolvedValueOnce(
|
|
528
|
+
// createMockPagingResponse(mockImages)
|
|
529
|
+
// );
|
|
530
|
+
// const { result } = renderHook(() =>
|
|
531
|
+
// useGallerySwipe(firebaseUid, initImageId, mockController)
|
|
532
|
+
// );
|
|
533
|
+
// await waitFor(() => {
|
|
534
|
+
// expect(result.current.isLoading).toBe(false);
|
|
535
|
+
// });
|
|
536
|
+
// // Clear mocks after initialization
|
|
537
|
+
// jest.clearAllMocks();
|
|
538
|
+
// mockController.onGetImage.mockResolvedValue(createMockGallery('image-2'));
|
|
539
|
+
// // Make navigation calls - hook should handle gracefully
|
|
540
|
+
// await act(async () => {
|
|
541
|
+
// await result.current.onSwipeNext();
|
|
542
|
+
// });
|
|
543
|
+
// await act(async () => {
|
|
544
|
+
// await result.current.onSwipeNext();
|
|
545
|
+
// });
|
|
546
|
+
// // Wait for any pending operations
|
|
547
|
+
// await waitFor(() => {
|
|
548
|
+
// expect(result.current.isLoading).toBe(false);
|
|
549
|
+
// });
|
|
550
|
+
// // Should have handled both calls successfully
|
|
551
|
+
// expect(mockController.onGetImage).toHaveBeenCalledTimes(2);
|
|
552
|
+
// });
|
|
553
|
+
// it('should handle controller returning null gracefully', async () => {
|
|
554
|
+
// const firebaseUid = 'user-123';
|
|
555
|
+
// const initImageId = 'image-1';
|
|
556
|
+
// mockController.onGetImage.mockResolvedValueOnce(null as any);
|
|
557
|
+
// const { result } = renderHook(() =>
|
|
558
|
+
// useGallerySwipe(firebaseUid, initImageId, mockController)
|
|
559
|
+
// );
|
|
560
|
+
// await waitFor(() => {
|
|
561
|
+
// expect(result.current.error).toBe('Failed to fetch initial image data');
|
|
562
|
+
// });
|
|
563
|
+
// expect(result.current.currentImageData).toBeNull();
|
|
564
|
+
// expect(result.current.isLoading).toBe(false);
|
|
565
|
+
// });
|
|
566
|
+
// });
|
|
567
|
+
// describe('Parameter Changes', () => {
|
|
568
|
+
// it('should re-initialize when firebaseUid changes', async () => {
|
|
569
|
+
// const initImageId = 'image-1';
|
|
570
|
+
// const mockGallery = createMockGallery(initImageId);
|
|
571
|
+
// const mockImages = [createMockGallery('image-1')];
|
|
572
|
+
// mockController.onGetImage.mockResolvedValue(mockGallery);
|
|
573
|
+
// mockController.getImageList.mockResolvedValue(
|
|
574
|
+
// createMockPagingResponse(mockImages)
|
|
575
|
+
// );
|
|
576
|
+
// const { result, rerender } = renderHook(
|
|
577
|
+
// ({ uid }) => useGallerySwipe(uid, initImageId, mockController),
|
|
578
|
+
// { initialProps: { uid: 'user-123' } }
|
|
579
|
+
// );
|
|
580
|
+
// await waitFor(() => {
|
|
581
|
+
// expect(result.current.isLoading).toBe(false);
|
|
582
|
+
// });
|
|
583
|
+
// expect(mockController.onGetImage).toHaveBeenCalledTimes(1);
|
|
584
|
+
// // Change firebaseUid
|
|
585
|
+
// rerender({ uid: 'user-456' });
|
|
586
|
+
// await waitFor(() => {
|
|
587
|
+
// expect(mockController.onGetImage).toHaveBeenCalledTimes(2);
|
|
588
|
+
// });
|
|
589
|
+
// expect(mockController.onGetImage).toHaveBeenLastCalledWith('user-456', initImageId);
|
|
590
|
+
// });
|
|
591
|
+
// it('should re-initialize when initImageId changes', async () => {
|
|
592
|
+
// const firebaseUid = 'user-123';
|
|
593
|
+
// const mockGallery1 = createMockGallery('image-1');
|
|
594
|
+
// const mockGallery2 = createMockGallery('image-2');
|
|
595
|
+
// const mockImages = [createMockGallery('image-1'), createMockGallery('image-2')];
|
|
596
|
+
// mockController.onGetImage
|
|
597
|
+
// .mockResolvedValueOnce(mockGallery1)
|
|
598
|
+
// .mockResolvedValueOnce(mockGallery2);
|
|
599
|
+
// mockController.getImageList.mockResolvedValue(
|
|
600
|
+
// createMockPagingResponse(mockImages)
|
|
601
|
+
// );
|
|
602
|
+
// const { result, rerender } = renderHook(
|
|
603
|
+
// ({ imageId }) => useGallerySwipe(firebaseUid, imageId, mockController),
|
|
604
|
+
// { initialProps: { imageId: 'image-1' } }
|
|
605
|
+
// );
|
|
606
|
+
// await waitFor(() => {
|
|
607
|
+
// expect(result.current.isLoading).toBe(false);
|
|
608
|
+
// });
|
|
609
|
+
// expect(result.current.currentImageData).toEqual(mockGallery1);
|
|
610
|
+
// // Change initImageId
|
|
611
|
+
// rerender({ imageId: 'image-2' });
|
|
612
|
+
// await waitFor(() => {
|
|
613
|
+
// expect(result.current.currentImageData).toEqual(mockGallery2);
|
|
614
|
+
// });
|
|
615
|
+
// expect(mockController.onGetImage).toHaveBeenCalledTimes(2);
|
|
616
|
+
// expect(mockController.onGetImage).toHaveBeenLastCalledWith(firebaseUid, 'image-2');
|
|
617
|
+
// });
|
|
618
|
+
// });
|
|
619
|
+
// });
|