-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FE] refactor: 기존 이미지 업로드로 수정 (#599)
* refactor: 이미지 파일 이름 수정 삭제 * feat: useFormData 추가 * refactor: 꿀조합 폼 요청 폼데이터로 수정 * refactor: 리뷰 폼 요청 폼데이터로 수정 * refactor: 회원 정보 수정 폼 요청 폼데이터로 수정 * refactor: s3 관련 로직 삭제 * test: useImageUploader 훅 테스트 작성
- Loading branch information
1 parent
69ecf96
commit 5e0a91a
Showing
22 changed files
with
188 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { useImageUploader } from '@/hooks/common'; | ||
import { renderHook, act } from '@testing-library/react'; | ||
|
||
const originalCreateObjectUrl = URL.createObjectURL; | ||
const originalRevokeObjectUrl = URL.revokeObjectURL; | ||
|
||
beforeAll(() => { | ||
URL.createObjectURL = jest.fn(() => 'mocked url'); | ||
URL.revokeObjectURL = jest.fn(); | ||
}); | ||
|
||
afterAll(() => { | ||
URL.createObjectURL = originalCreateObjectUrl; | ||
URL.revokeObjectURL = originalRevokeObjectUrl; | ||
}); | ||
|
||
it('uploadImage를 사용하여 이미지 파일을 업로드할 수 있다.', () => { | ||
const { result } = renderHook(() => useImageUploader()); | ||
|
||
const file = new File(['dummy content'], 'example.png', { type: 'image/png' }); | ||
|
||
act(() => { | ||
result.current.uploadImage(file); | ||
}); | ||
|
||
expect(result.current.imageFile).toBe(file); | ||
expect(result.current.previewImage).toBe('mocked url'); | ||
expect(URL.createObjectURL).toHaveBeenCalledWith(file); | ||
}); | ||
|
||
it('이미지 파일이 아니면 "이미지 파일만 업로드 가능합니다." 메시지를 보여주는 alert 창이 뜬다.', () => { | ||
const { result } = renderHook(() => useImageUploader()); | ||
|
||
const file = new File(['dummy content'], 'example.txt', { type: 'text/plain' }); | ||
|
||
global.alert = jest.fn(); | ||
|
||
act(() => { | ||
result.current.uploadImage(file); | ||
}); | ||
|
||
expect(global.alert).toHaveBeenCalledWith('이미지 파일만 업로드 가능합니다.'); | ||
}); | ||
|
||
it('deleteImage를 사용하여 이미지 파일을 삭제할 수 있다.', () => { | ||
const { result } = renderHook(() => useImageUploader()); | ||
|
||
const file = new File(['dummy content'], 'example.png', { type: 'image/png' }); | ||
|
||
act(() => { | ||
result.current.uploadImage(file); | ||
}); | ||
|
||
act(() => { | ||
result.current.deleteImage(); | ||
}); | ||
|
||
expect(result.current.imageFile).toBeNull(); | ||
expect(result.current.previewImage).toBe(''); | ||
expect(URL.revokeObjectURL).toHaveBeenCalledWith('mocked url'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
interface useFormDataProps<T> { | ||
imageKey: string; | ||
imageFile: File | File[] | null; | ||
formContentKey: string; | ||
formContent: T; | ||
} | ||
|
||
const useFormData = <T>({ imageKey, imageFile, formContentKey, formContent }: useFormDataProps<T>) => { | ||
const formData = new FormData(); | ||
const formContentString = JSON.stringify(formContent); | ||
const formContentBlob = new Blob([formContentString], { type: 'application/json' }); | ||
|
||
formData.append(formContentKey, formContentBlob); | ||
|
||
if (!imageFile) return formData; | ||
|
||
if (Array.isArray(imageFile)) { | ||
imageFile.forEach((file) => { | ||
formData.append(imageKey, file, file.name); | ||
}); | ||
return formData; | ||
} | ||
|
||
formData.append(imageKey, imageFile, imageFile.name); | ||
|
||
return formData; | ||
}; | ||
|
||
export default useFormData; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.