|
|
@@ -1,79 +1,71 @@
|
|
|
import { describe, test, expect } from 'vitest';
|
|
|
|
|
|
-import { validateImageContentType } from './image-content-type-validator';
|
|
|
+import { validateImageContentType, type SupportedImageMimeType } from './image-content-type-validator';
|
|
|
|
|
|
describe('imageContentTypeValidator', () => {
|
|
|
describe('validateImageContentType', () => {
|
|
|
- test('should accept valid single MIME type', () => {
|
|
|
- const result = validateImageContentType('image/png');
|
|
|
- expect(result.isValid).toBe(true);
|
|
|
- expect(result.contentType).toBe('image/png');
|
|
|
- expect(result.error).toBeUndefined();
|
|
|
- });
|
|
|
-
|
|
|
- test('should accept valid MIME type when multiple types are provided (last type is valid)', () => {
|
|
|
- const result = validateImageContentType('text/html, image/jpeg');
|
|
|
- expect(result.isValid).toBe(true);
|
|
|
- expect(result.contentType).toBe('image/jpeg');
|
|
|
- expect(result.error).toBeUndefined();
|
|
|
- });
|
|
|
-
|
|
|
- test('should reject when last MIME type is invalid', () => {
|
|
|
- const result = validateImageContentType('image/png, text/html');
|
|
|
- expect(result.isValid).toBe(false);
|
|
|
- expect(result.contentType).toBe('text/html');
|
|
|
- expect(result.error).toContain('Invalid file type');
|
|
|
- });
|
|
|
-
|
|
|
- test('should handle empty string', () => {
|
|
|
- const result = validateImageContentType('');
|
|
|
- expect(result.isValid).toBe(false);
|
|
|
- expect(result.contentType).toBe('');
|
|
|
- expect(result.error).toContain('Invalid file type');
|
|
|
- });
|
|
|
-
|
|
|
- test('should handle whitespace in MIME types', () => {
|
|
|
- const result = validateImageContentType('image/png , image/jpeg');
|
|
|
- expect(result.isValid).toBe(true);
|
|
|
- expect(result.contentType).toBe('image/jpeg');
|
|
|
- expect(result.error).toBeUndefined();
|
|
|
- });
|
|
|
+ describe('valid cases', () => {
|
|
|
+ test.each([
|
|
|
+ ['single MIME type', 'image/png'],
|
|
|
+ ['MIME type with whitespace', 'image/png '],
|
|
|
+ ['multiple MIME types (last is valid)', 'text/html, image/jpeg'],
|
|
|
+ ['multiple MIME types with whitespace', 'image/png , image/jpeg'],
|
|
|
+ ])('should accept %s', (_, input) => {
|
|
|
+ const result = validateImageContentType(input);
|
|
|
+ expect(result.isValid).toBe(true);
|
|
|
+ expect(result.error).toBeUndefined();
|
|
|
+ });
|
|
|
|
|
|
- test('should reject non-image MIME types', () => {
|
|
|
- const result = validateImageContentType('application/json');
|
|
|
- expect(result.isValid).toBe(false);
|
|
|
- expect(result.contentType).toBe('application/json');
|
|
|
- expect(result.error).toContain('Invalid file type');
|
|
|
- });
|
|
|
+ describe('should accept all supported image types', () => {
|
|
|
+ const supportedTypes: SupportedImageMimeType[] = [
|
|
|
+ 'image/png',
|
|
|
+ 'image/jpeg',
|
|
|
+ 'image/jpg',
|
|
|
+ 'image/gif',
|
|
|
+ 'image/webp',
|
|
|
+ 'image/avif',
|
|
|
+ 'image/heic',
|
|
|
+ 'image/heif',
|
|
|
+ 'image/tiff',
|
|
|
+ 'image/svg+xml',
|
|
|
+ ];
|
|
|
|
|
|
- test('should reject non-string input', () => {
|
|
|
- const result = validateImageContentType(undefined as unknown as string);
|
|
|
- expect(result.isValid).toBe(false);
|
|
|
- expect(result.contentType).toBeNull();
|
|
|
- expect(result.error).toBe('Invalid MIME type format');
|
|
|
+ test.each(supportedTypes)('%s', (mimeType) => {
|
|
|
+ const result = validateImageContentType(mimeType);
|
|
|
+ expect(result.isValid).toBe(true);
|
|
|
+ expect(result.contentType).toBe(mimeType);
|
|
|
+ expect(result.error).toBeUndefined();
|
|
|
+ });
|
|
|
+ });
|
|
|
});
|
|
|
|
|
|
- test('should accept all supported image types', () => {
|
|
|
- const supportedTypes = [
|
|
|
- 'image/png',
|
|
|
- 'image/jpeg',
|
|
|
- 'image/jpg',
|
|
|
- 'image/gif',
|
|
|
- 'image/webp',
|
|
|
- 'image/avif',
|
|
|
- 'image/heic',
|
|
|
- 'image/heif',
|
|
|
- 'image/tiff',
|
|
|
- 'image/svg+xml',
|
|
|
- ];
|
|
|
+ describe('invalid cases', () => {
|
|
|
+ describe('invalid MIME types', () => {
|
|
|
+ test.each([
|
|
|
+ ['non-image MIME type', 'application/json'],
|
|
|
+ ['multiple MIME types (last is invalid)', 'image/png, text/html'],
|
|
|
+ ['empty string', ''],
|
|
|
+ ['unknown image type', 'image/unknown'],
|
|
|
+ ])('should reject %s', (_, input) => {
|
|
|
+ const result = validateImageContentType(input);
|
|
|
+ expect(result.isValid).toBe(false);
|
|
|
+ expect(result.error).toContain('Invalid file type');
|
|
|
+ });
|
|
|
+ });
|
|
|
|
|
|
- // Use for...of instead of forEach to avoid extra argument
|
|
|
- for (const mimeType of supportedTypes) {
|
|
|
- const result = validateImageContentType(mimeType);
|
|
|
- expect(result.isValid).toBe(true, `${mimeType} should be valid`);
|
|
|
- expect(result.contentType).toBe(mimeType);
|
|
|
- expect(result.error).toBeUndefined();
|
|
|
- }
|
|
|
+ describe('invalid input types', () => {
|
|
|
+ test.each([
|
|
|
+ ['undefined', undefined],
|
|
|
+ ['null', null],
|
|
|
+ ['number', 42],
|
|
|
+ ['object', {}],
|
|
|
+ ])('should reject %s', (_, input) => {
|
|
|
+ const result = validateImageContentType(input as unknown as string);
|
|
|
+ expect(result.isValid).toBe(false);
|
|
|
+ expect(result.contentType).toBeNull();
|
|
|
+ expect(result.error).toBe('Invalid MIME type format');
|
|
|
+ });
|
|
|
+ });
|
|
|
});
|
|
|
});
|
|
|
});
|