|
| 1 | +import { isImageMimeType, isPdfMimeType, isVideoMimeType, isAudioMimeType, isTextMimeType, isApplicationMimeType } from "./mimeType"; |
| 2 | + |
| 3 | +describe("mimeType tests", () => { |
| 4 | + // Common invalid inputs |
| 5 | + const invalidInputs: [string | null | undefined | number | Date, boolean][] = [ |
| 6 | + [null as unknown as string, false], |
| 7 | + [undefined as unknown as string, false], |
| 8 | + [0 as unknown as string, false], |
| 9 | + [new Date() as unknown as string, false], |
| 10 | + ["", false], |
| 11 | + [" ", false], |
| 12 | + ["not-a-mime-type", false], |
| 13 | + ]; |
| 14 | + |
| 15 | + test.each([ |
| 16 | + ...invalidInputs, |
| 17 | + // Valid image MIME types |
| 18 | + ["image/jpeg", true], |
| 19 | + ["image/png", true], |
| 20 | + ["image/gif", true], |
| 21 | + ["image/webp", true], |
| 22 | + ["image/svg+xml", true], |
| 23 | + ["IMAGE/JPEG", true], // Case insensitive |
| 24 | + [" image/gif ", true], // Whitespace trimmed |
| 25 | + // Invalid cases |
| 26 | + ["application/pdf", false], |
| 27 | + ["text/plain", false], |
| 28 | + ["video/mp4", false], |
| 29 | + ["image", false], |
| 30 | + ["image/", false], |
| 31 | + ["/image", false], |
| 32 | + ])("isImageMimeType", (mimeType, expected) => { |
| 33 | + expect(isImageMimeType(mimeType as string)).toBe(expected); |
| 34 | + }); |
| 35 | + |
| 36 | + test.each([ |
| 37 | + ...invalidInputs, |
| 38 | + // Valid PDF MIME type |
| 39 | + ["application/pdf", true], |
| 40 | + ["APPLICATION/PDF", true], // Case insensitive |
| 41 | + [" application/pdf ", true], // Whitespace trimmed |
| 42 | + // Invalid cases |
| 43 | + ["image/jpeg", false], |
| 44 | + ["application/json", false], |
| 45 | + ["pdf", false], |
| 46 | + ["application/", false], |
| 47 | + ])("isPdfMimeType", (mimeType, expected) => { |
| 48 | + expect(isPdfMimeType(mimeType as string)).toBe(expected); |
| 49 | + }); |
| 50 | + |
| 51 | + test.each([ |
| 52 | + ...invalidInputs, |
| 53 | + // Valid video MIME types |
| 54 | + ["video/mp4", true], |
| 55 | + ["video/mpeg", true], |
| 56 | + ["video/webm", true], |
| 57 | + ["VIDEO/MP4", true], // Case insensitive |
| 58 | + [" video/webm ", true], // Whitespace trimmed |
| 59 | + // Invalid cases |
| 60 | + ["image/jpeg", false], |
| 61 | + ["audio/mp3", false], |
| 62 | + ["video", false], |
| 63 | + ["video/", false], |
| 64 | + ["/video", false], |
| 65 | + ])("isVideoMimeType", (mimeType, expected) => { |
| 66 | + expect(isVideoMimeType(mimeType as string)).toBe(expected); |
| 67 | + }); |
| 68 | + |
| 69 | + test.each([ |
| 70 | + ...invalidInputs, |
| 71 | + // Valid audio MIME types |
| 72 | + ["audio/mp3", true], |
| 73 | + ["audio/mpeg", true], |
| 74 | + ["audio/wav", true], |
| 75 | + ["AUDIO/MP3", true], // Case insensitive |
| 76 | + [" audio/ogg ", true], // Whitespace trimmed |
| 77 | + // Invalid cases |
| 78 | + ["video/mp4", false], |
| 79 | + ["image/jpeg", false], |
| 80 | + ["audio", false], |
| 81 | + ["audio/", false], |
| 82 | + ["/audio", false], |
| 83 | + ])("isAudioMimeType", (mimeType, expected) => { |
| 84 | + expect(isAudioMimeType(mimeType as string)).toBe(expected); |
| 85 | + }); |
| 86 | + |
| 87 | + test.each([ |
| 88 | + ...invalidInputs, |
| 89 | + // Valid text MIME types |
| 90 | + ["text/plain", true], |
| 91 | + ["text/html", true], |
| 92 | + ["text/css", true], |
| 93 | + ["TEXT/PLAIN", true], // Case insensitive |
| 94 | + [" text/css ", true], // Whitespace trimmed |
| 95 | + // Invalid cases |
| 96 | + ["application/pdf", false], |
| 97 | + ["image/jpeg", false], |
| 98 | + ["text", false], |
| 99 | + ["text/", false], |
| 100 | + ["/text", false], |
| 101 | + ])("isTextMimeType", (mimeType, expected) => { |
| 102 | + expect(isTextMimeType(mimeType as string)).toBe(expected); |
| 103 | + }); |
| 104 | + |
| 105 | + test.each([ |
| 106 | + ...invalidInputs, |
| 107 | + // Valid application MIME types |
| 108 | + ["application/pdf", true], |
| 109 | + ["application/json", true], |
| 110 | + ["application/xml", true], |
| 111 | + ["APPLICATION/PDF", true], // Case insensitive |
| 112 | + [" application/xml ", true], // Whitespace trimmed |
| 113 | + // Invalid cases |
| 114 | + ["text/plain", false], |
| 115 | + ["image/jpeg", false], |
| 116 | + ["application", false], |
| 117 | + ["application/", false], |
| 118 | + ["/application", false], |
| 119 | + ])("isApplicationMimeType", (mimeType, expected) => { |
| 120 | + expect(isApplicationMimeType(mimeType as string)).toBe(expected); |
| 121 | + }); |
| 122 | +}); |
0 commit comments