|
1 |
| -import { getMimeTypesUIKitAccepts, SUPPORTED_MIMES } from '../index'; |
| 1 | +import { getMimeTypesUIKitAccepts, SUPPORTED_MIMES, SUPPORTED_FILE_EXTENSIONS } from '../index'; |
2 | 2 |
|
3 | 3 | describe('Global-utils/getMimeTypesUIKitAccepts', () => {
|
4 |
| - it('when no input, should return all SUPPORTED_MIMES.', () => { |
5 |
| - const allMimeTypes: string[] = Object.values(SUPPORTED_MIMES) |
6 |
| - .reduce((accumulator: string[], mimes: string[]): string[] => { |
7 |
| - return accumulator.concat(mimes); |
8 |
| - }); |
9 |
| - expect(getMimeTypesUIKitAccepts()).toBe(allMimeTypes.join()); |
10 |
| - }); |
11 |
| - it('when given [image], should return IMAGE mime types.', () => { |
12 |
| - expect(getMimeTypesUIKitAccepts(['image'])).toBe(SUPPORTED_MIMES.IMAGE.join()); |
13 |
| - }); |
14 |
| - it('when given [image, video], should return IMAGE and VIDEO mime types.', () => { |
15 |
| - expect(getMimeTypesUIKitAccepts(['image', 'video'])) |
16 |
| - .toBe([SUPPORTED_MIMES.IMAGE.join(), SUPPORTED_MIMES.VIDEO.join()].join()); |
17 |
| - }); |
18 |
| - it('when given empty array, should return all SUPPORTED_MIMES.', () => { |
19 |
| - const allMimeTypes: string[] = Object.values(SUPPORTED_MIMES) |
20 |
| - .reduce((accumulator: string[], mimes: string[]): string[] => { |
21 |
| - return accumulator.concat(mimes); |
22 |
| - }); |
23 |
| - expect(getMimeTypesUIKitAccepts([])).toBe(allMimeTypes.join()); |
24 |
| - }); |
25 |
| - it('when given mime types, should return mime types string.', () => { |
26 |
| - const accept = getMimeTypesUIKitAccepts(['image/png', 'image/heic']); |
27 |
| - const expected = 'image/png,image/heic'; |
28 |
| - expect(accept).toBe(expected); |
| 4 | + const allTypesAndExtensions = [ |
| 5 | + ...Object.values(SUPPORTED_MIMES).flat(), |
| 6 | + ...Object.values(SUPPORTED_FILE_EXTENSIONS).flat(), |
| 7 | + ].join(); |
| 8 | + |
| 9 | + it('should return all supported MIME types and file extensions when no input is provided', () => { |
| 10 | + expect(getMimeTypesUIKitAccepts()).toBe(allTypesAndExtensions); |
| 11 | + }); |
| 12 | + |
| 13 | + it('should return all supported MIME types and file extensions when an empty array is provided', () => { |
| 14 | + expect(getMimeTypesUIKitAccepts([])).toBe(allTypesAndExtensions); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should return IMAGE mime types and extensions when [image] is provided', () => { |
| 18 | + const expected = [...SUPPORTED_MIMES.IMAGE, ...SUPPORTED_FILE_EXTENSIONS.IMAGE].join(); |
| 19 | + expect(getMimeTypesUIKitAccepts(['image'])).toBe(expected); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should return VIDEO mime types and extensions when [video] is provided', () => { |
| 23 | + const expected = [...SUPPORTED_MIMES.VIDEO, ...SUPPORTED_FILE_EXTENSIONS.VIDEO].join(); |
| 24 | + expect(getMimeTypesUIKitAccepts(['video'])).toBe(expected); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should return AUDIO mime types and extensions when [audio] is provided', () => { |
| 28 | + const expected = [...SUPPORTED_MIMES.AUDIO, ...SUPPORTED_FILE_EXTENSIONS.AUDIO].join(); |
| 29 | + expect(getMimeTypesUIKitAccepts(['audio'])).toBe(expected); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should return ARCHIVE mime types and extensions when [archive] is provided', () => { |
| 33 | + const expected = [...SUPPORTED_MIMES.ARCHIVE, ...SUPPORTED_FILE_EXTENSIONS.ARCHIVE].join(); |
| 34 | + expect(getMimeTypesUIKitAccepts(['archive'])).toBe(expected); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should not include archive types when not specified', () => { |
| 38 | + const result = getMimeTypesUIKitAccepts(['image', 'video']); |
| 39 | + expect(result).not.toContain('application/zip'); |
| 40 | + expect(result).not.toContain('.7z'); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should return combined IMAGE and VIDEO mime types and extensions when [image, video] is provided', () => { |
| 44 | + const expected = [ |
| 45 | + ...SUPPORTED_MIMES.IMAGE, |
| 46 | + ...SUPPORTED_FILE_EXTENSIONS.IMAGE, |
| 47 | + ...SUPPORTED_MIMES.VIDEO, |
| 48 | + ...SUPPORTED_FILE_EXTENSIONS.VIDEO, |
| 49 | + ].join(); |
| 50 | + expect(getMimeTypesUIKitAccepts(['image', 'video'])).toBe(expected); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should return exact mime types or extensions when specific ones are provided', () => { |
| 54 | + const input = ['image/png', '.pdf', 'audio/mp3']; |
| 55 | + expect(getMimeTypesUIKitAccepts(input)).toBe(input.join(',')); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should handle a mix of category names, specific mime types, and file extensions', () => { |
| 59 | + const input = ['image', 'application/pdf', '.txt']; |
| 60 | + const expected = [ |
| 61 | + ...SUPPORTED_MIMES.IMAGE, |
| 62 | + ...SUPPORTED_FILE_EXTENSIONS.IMAGE, |
| 63 | + 'application/pdf', |
| 64 | + '.txt', |
| 65 | + ].join(); |
| 66 | + expect(getMimeTypesUIKitAccepts(input)).toBe(expected); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should ignore unsupported category names', () => { |
| 70 | + const input = ['image', 'video', 'unsupported']; |
| 71 | + const expected = [ |
| 72 | + ...SUPPORTED_MIMES.IMAGE, |
| 73 | + ...SUPPORTED_FILE_EXTENSIONS.IMAGE, |
| 74 | + ...SUPPORTED_MIMES.VIDEO, |
| 75 | + ...SUPPORTED_FILE_EXTENSIONS.VIDEO, |
| 76 | + 'unsupported', |
| 77 | + ].join(); |
| 78 | + expect(getMimeTypesUIKitAccepts(input)).toBe(expected); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should handle duplicate inputs', () => { |
| 82 | + const input = ['image', 'image', 'video', 'image/png', '.jpg', '.jpg']; |
| 83 | + const expected = [ |
| 84 | + ...SUPPORTED_MIMES.IMAGE, |
| 85 | + ...SUPPORTED_FILE_EXTENSIONS.IMAGE, |
| 86 | + ...SUPPORTED_MIMES.VIDEO, |
| 87 | + ...SUPPORTED_FILE_EXTENSIONS.VIDEO, |
| 88 | + ].join(); |
| 89 | + expect(getMimeTypesUIKitAccepts(input)).toBe(expected); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should return all supported types and extensions for null input', () => { |
| 93 | + expect(getMimeTypesUIKitAccepts(null as any)).toBe(allTypesAndExtensions); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should return all supported types and extensions for undefined input', () => { |
| 97 | + expect(getMimeTypesUIKitAccepts(undefined as any)).toBe(allTypesAndExtensions); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should handle case-insensitive category names', () => { |
| 101 | + const result = getMimeTypesUIKitAccepts(['IMAGE', 'Video', 'AuDiO']); |
| 102 | + |
| 103 | + SUPPORTED_MIMES.IMAGE.forEach(mime => expect(result).toContain(mime)); |
| 104 | + SUPPORTED_FILE_EXTENSIONS.IMAGE.forEach(ext => expect(result).toContain(ext)); |
| 105 | + |
| 106 | + SUPPORTED_MIMES.VIDEO.forEach(mime => expect(result).toContain(mime)); |
| 107 | + SUPPORTED_FILE_EXTENSIONS.VIDEO.forEach(ext => expect(result).toContain(ext)); |
| 108 | + |
| 109 | + SUPPORTED_MIMES.AUDIO.forEach(mime => expect(result).toContain(mime)); |
| 110 | + SUPPORTED_FILE_EXTENSIONS.AUDIO.forEach(ext => expect(result).toContain(ext)); |
| 111 | + |
| 112 | + // assert if there are any duplicates |
| 113 | + const uniqueResult = new Set(result.split(',')); |
| 114 | + expect(uniqueResult.size).toBe(result.split(',').length); |
| 115 | + |
| 116 | + // assert if there are no other types |
| 117 | + expect(result).not.toContain('IMAGE'); |
| 118 | + expect(result).not.toContain('Video'); |
| 119 | + expect(result).not.toContain('AuDiO'); |
29 | 120 | });
|
30 | 121 | });
|
0 commit comments