From ddc20c291800273afc107bfdab46013fd1e261e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gromit=20=28=EC=A0=84=EB=AF=BC=EC=9E=AC=29?= <64779472+ssi02014@users.noreply.github.com> Date: Thu, 16 May 2024 17:33:40 +0900 Subject: [PATCH] =?UTF-8?q?test(utils):=20getMIMETypeFromResponse=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20(#131)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../getMIMETypeFromFile.spec.ts | 19 ++++++----- .../src/file/getMIMETypeFromFile/index.ts | 2 +- .../getMIMETypeFromResponse.spec.ts | 32 +++++++++++++++++++ .../src/file/getMIMETypeFromResponse/index.ts | 9 +----- 4 files changed, 45 insertions(+), 17 deletions(-) create mode 100644 packages/utils/src/file/getMIMETypeFromResponse/getMIMETypeFromResponse.spec.ts diff --git a/packages/utils/src/file/getMIMETypeFromFile/getMIMETypeFromFile.spec.ts b/packages/utils/src/file/getMIMETypeFromFile/getMIMETypeFromFile.spec.ts index b8bc3b7fe..becf40d8d 100644 --- a/packages/utils/src/file/getMIMETypeFromFile/getMIMETypeFromFile.spec.ts +++ b/packages/utils/src/file/getMIMETypeFromFile/getMIMETypeFromFile.spec.ts @@ -3,24 +3,27 @@ import { getMIMETypeFromFile } from '.'; describe('getMIMETypeFromFile', () => { it('should return the MIME type of a valid file', () => { const file = new File([''], 'example.png', { type: 'image/png' }); - const result = getMIMETypeFromFile(file); - expect(result).toBe('image/png'); + const mimeType = getMIMETypeFromFile(file); + expect(mimeType).toBe('image/png'); }); it('should return an empty string if the file type is an empty string', () => { const file = new File([''], 'example', { type: '' }); - const result = getMIMETypeFromFile(file); - expect(result).toBe(''); + const mimeType = getMIMETypeFromFile(file); + expect(mimeType).toBe(''); }); it('should return an empty string if the file type is invalid', () => { const file = new File([''], 'example.unknown'); - const result = getMIMETypeFromFile(file); - expect(result).toBe(''); + const mimeType = getMIMETypeFromFile(file); + expect(mimeType).toBe(''); }); it('should return an empty string if the argument is invalid', () => { - const result = getMIMETypeFromFile(NaN as any); - expect(result).toBe(''); + const mimeType1 = getMIMETypeFromFile(NaN as unknown as File); + expect(mimeType1).toBe(''); + + const mimeType2 = getMIMETypeFromFile(123 as unknown as File); + expect(mimeType2).toBe(''); }); }); diff --git a/packages/utils/src/file/getMIMETypeFromFile/index.ts b/packages/utils/src/file/getMIMETypeFromFile/index.ts index 95b5a2429..7e17bdde4 100644 --- a/packages/utils/src/file/getMIMETypeFromFile/index.ts +++ b/packages/utils/src/file/getMIMETypeFromFile/index.ts @@ -1,3 +1,3 @@ export const getMIMETypeFromFile = (file: File) => { - return file?.type ?? ''; + return file.type ?? ''; }; diff --git a/packages/utils/src/file/getMIMETypeFromResponse/getMIMETypeFromResponse.spec.ts b/packages/utils/src/file/getMIMETypeFromResponse/getMIMETypeFromResponse.spec.ts new file mode 100644 index 000000000..bcd8de359 --- /dev/null +++ b/packages/utils/src/file/getMIMETypeFromResponse/getMIMETypeFromResponse.spec.ts @@ -0,0 +1,32 @@ +import { getMIMETypeFromResponse } from '.'; + +describe('getMIMETypeFromResponse', () => { + it('should return the correct MIME type from the response headers', () => { + const headers = new Headers(); + headers.set('Content-Type', 'image/png'); + + const mockResponse = new Response(null, { headers }); + + const mimeType = getMIMETypeFromResponse(mockResponse); + expect(mimeType).toBe('image/png'); + }); + + it('should return an empty string if the Content-Type header is not present', () => { + const headers = new Headers(); + const mockResponse = new Response(null, { headers }); + + const mimeType = getMIMETypeFromResponse(mockResponse); + expect(mimeType).toBe(''); + }); + + it('should return an empty string when given an invalid argument', () => { + const mockResponse1 = {} as unknown as Response; + const mockResponse2 = 123 as unknown as Response; + + const mimeType1 = getMIMETypeFromResponse(mockResponse1); + expect(mimeType1).toBe(''); + + const mimeType2 = getMIMETypeFromResponse(mockResponse2); + expect(mimeType2).toBe(''); + }); +}); diff --git a/packages/utils/src/file/getMIMETypeFromResponse/index.ts b/packages/utils/src/file/getMIMETypeFromResponse/index.ts index 77a21687c..a3b0440b9 100644 --- a/packages/utils/src/file/getMIMETypeFromResponse/index.ts +++ b/packages/utils/src/file/getMIMETypeFromResponse/index.ts @@ -1,10 +1,3 @@ export const getMIMETypeFromResponse = (response: Response) => { - try { - return response.headers.get('Content-Type') ?? ''; - } catch (err: any) { - console.error( - `Failed to get the MIME type from Response. message: ${err.message}` - ); - return ''; - } + return response.headers?.get('Content-Type') ?? ''; };