Skip to content

Commit

Permalink
test(utils): getMIMETypeFromResponse 테스트 코드 추가 (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssi02014 authored May 16, 2024
1 parent 75e45fc commit ddc20c2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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('');
});
});
2 changes: 1 addition & 1 deletion packages/utils/src/file/getMIMETypeFromFile/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const getMIMETypeFromFile = (file: File) => {
return file?.type ?? '';
return file.type ?? '';
};
Original file line number Diff line number Diff line change
@@ -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('');
});
});
9 changes: 1 addition & 8 deletions packages/utils/src/file/getMIMETypeFromResponse/index.ts
Original file line number Diff line number Diff line change
@@ -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') ?? '';
};

0 comments on commit ddc20c2

Please sign in to comment.