|
| 1 | +import { act, renderHook, waitFor } from '@testing-library/react'; |
| 2 | +import useImagePreSetup, { |
| 3 | + convertFile, |
| 4 | + DEFAULT_WEBP_QUALITY, |
| 5 | + urlFromFileHandler, |
| 6 | + validateWebPQuality, |
| 7 | +} from './useImagePreSetup'; |
| 8 | + |
| 9 | +import { imgTo } from '../utils'; |
| 10 | + |
| 11 | +jest.mock('../utils/imgTo'); |
| 12 | + |
| 13 | +describe('useImagePreSetup', () => { |
| 14 | + let consoleWarnSpy: jest.SpyInstance; |
| 15 | + let consoleErrorSpy: jest.SpyInstance; |
| 16 | + let mockFiles: File[]; |
| 17 | + const mockUrls: string[] = ['mock-url1', 'mock-url2', 'mock-url3']; |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + mockFiles = [ |
| 21 | + new File(['content1'], 'example1.png', { type: 'image/png' }), |
| 22 | + new File(['content2'], 'example2.png', { type: 'image/jpeg' }), |
| 23 | + new File(['content3'], 'example3.png', { type: 'image/gif' }), |
| 24 | + ]; |
| 25 | + |
| 26 | + global.URL.createObjectURL = jest.fn(() => mockUrls.shift() || 'mock-url'); |
| 27 | + global.URL.revokeObjectURL = jest.fn(); |
| 28 | + |
| 29 | + consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); |
| 30 | + }); |
| 31 | + |
| 32 | + afterEach(() => { |
| 33 | + jest.clearAllMocks(); |
| 34 | + consoleWarnSpy.mockRestore(); |
| 35 | + }); |
| 36 | + |
| 37 | + test('파일이 주어지면 변환함수가 올바르게 호출되고 로딩 상태가 정상적으로 변경된다.', async () => { |
| 38 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 39 | + (imgTo as jest.Mock).mockImplementation((_url: string) => { |
| 40 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 41 | + return (_type: string) => { |
| 42 | + return Promise.resolve(new Blob()); |
| 43 | + }; |
| 44 | + }); |
| 45 | + |
| 46 | + const { result } = renderHook(() => |
| 47 | + useImagePreSetup({ imageFiles: mockFiles, convertToWebP: true }) |
| 48 | + ); |
| 49 | + |
| 50 | + expect(result.current.isLoading).toBe(true); |
| 51 | + |
| 52 | + await waitFor(() => { |
| 53 | + expect(imgTo).toHaveBeenCalledTimes(mockFiles.length); |
| 54 | + }); |
| 55 | + |
| 56 | + await waitFor(() => { |
| 57 | + expect(result.current.isLoading).toBe(false); |
| 58 | + }); |
| 59 | + |
| 60 | + expect(imgTo).toHaveBeenCalledTimes(mockFiles.length); |
| 61 | + expect(result.current.isError).toBe(false); |
| 62 | + expect(result.current.previewUrls.length).toBe(mockFiles.length); |
| 63 | + expect(result.current.webpImages.length).toBe(mockFiles.length); |
| 64 | + }); |
| 65 | + |
| 66 | + test('파일이 주어졌을 때, URL이 생성되어 반환된다.', async () => { |
| 67 | + for (const file of mockFiles) { |
| 68 | + const result = await urlFromFileHandler(file); |
| 69 | + |
| 70 | + expect(global.URL.createObjectURL).toHaveBeenCalledWith(file); |
| 71 | + |
| 72 | + expect(result).toEqual({ |
| 73 | + webpBlob: null, |
| 74 | + previewUrl: 'mock-url', |
| 75 | + }); |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | + test('파일이 없거나 빈 배열이 주어지면 처리하지 않는다.', () => { |
| 80 | + const { result: resultNull } = renderHook(() => |
| 81 | + useImagePreSetup({ imageFiles: null, convertToWebP: true }) |
| 82 | + ); |
| 83 | + |
| 84 | + expect(resultNull.current.isLoading).toBe(false); |
| 85 | + expect(resultNull.current.isError).toBe(false); |
| 86 | + expect(resultNull.current.previewUrls).toEqual([]); |
| 87 | + expect(resultNull.current.webpImages).toEqual([]); |
| 88 | + |
| 89 | + const { result: resultEmpty } = renderHook(() => |
| 90 | + useImagePreSetup({ imageFiles: [], convertToWebP: true }) |
| 91 | + ); |
| 92 | + |
| 93 | + expect(resultEmpty.current.isLoading).toBe(false); |
| 94 | + expect(resultEmpty.current.isError).toBe(false); |
| 95 | + expect(resultEmpty.current.previewUrls).toEqual([]); |
| 96 | + expect(resultEmpty.current.webpImages).toEqual([]); |
| 97 | + }); |
| 98 | + |
| 99 | + test('컴포넌트 언마운트 시 URL이 적절히 해제된다.', async () => { |
| 100 | + const { unmount } = renderHook(() => |
| 101 | + useImagePreSetup({ imageFiles: mockFiles, convertToWebP: true }) |
| 102 | + ); |
| 103 | + |
| 104 | + expect(global.URL.revokeObjectURL).not.toHaveBeenCalled(); |
| 105 | + |
| 106 | + act(() => { |
| 107 | + unmount(); |
| 108 | + }); |
| 109 | + |
| 110 | + mockUrls.forEach((url) => { |
| 111 | + expect(global.URL.revokeObjectURL).toHaveBeenCalledWith(url); |
| 112 | + }); |
| 113 | + expect(global.URL.revokeObjectURL).toHaveBeenCalledTimes(mockUrls.length); |
| 114 | + }); |
| 115 | + |
| 116 | + test('convertToWebP가 false일 때 경고가 출력된다.', () => { |
| 117 | + const webPQuality = 0.5; |
| 118 | + |
| 119 | + validateWebPQuality(false, webPQuality); |
| 120 | + |
| 121 | + expect(consoleWarnSpy).toHaveBeenCalledWith( |
| 122 | + 'webPQuality`는 WebP로의 변환 품질을 설정하는 옵션입니다. `convertToWebP`를 true로 설정해야만 `webPQuality`가 적용됩니다.' |
| 123 | + ); |
| 124 | + }); |
| 125 | + |
| 126 | + test('유효 범위를 벗어난 webPQuality 값이 주어졌을 때, 이 값이 반환된다.', () => { |
| 127 | + const validWebPQuality = 1.5; |
| 128 | + |
| 129 | + const result = validateWebPQuality(true, validWebPQuality); |
| 130 | + |
| 131 | + expect(result).toBe(0.8); |
| 132 | + expect(consoleWarnSpy).toHaveBeenCalledWith( |
| 133 | + `webPQuality 값이 유효 범위(0 ~ 1)를 벗어나 기본값(${DEFAULT_WEBP_QUALITY})이 사용됩니다.` |
| 134 | + ); |
| 135 | + }); |
| 136 | + |
| 137 | + test('convertHandler가 실패할 경우, console.error가 호출되고 올바른 반환값을 가지는지 검증한다.', async () => { |
| 138 | + consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); |
| 139 | + |
| 140 | + const mockFile = new File(['content'], 'example.png', { |
| 141 | + type: 'image/png', |
| 142 | + }); |
| 143 | + |
| 144 | + const failingHandler = jest |
| 145 | + .fn() |
| 146 | + .mockRejectedValue(new Error('Conversion error')); |
| 147 | + |
| 148 | + const result = await convertFile(mockFile, failingHandler, 0); |
| 149 | + |
| 150 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 151 | + '1번째 파일 처리 중 오류 발생:', |
| 152 | + new Error('Conversion error') |
| 153 | + ); |
| 154 | + |
| 155 | + expect(result).toEqual({ webpBlob: null, previewUrl: null }); |
| 156 | + |
| 157 | + consoleErrorSpy.mockRestore(); |
| 158 | + }); |
| 159 | +}); |
0 commit comments