@@ -2,139 +2,69 @@ import { describe, expect, test } from 'vitest';
22import { ThumbnailUtils } from '../../src/utils/thumbnail.utils' ;
33
44describe ( 'Thumbnail Utils tests' , ( ) => {
5- describe ( 'isFileThumbnailable' , ( ) => {
6- test ( 'when a valid image extension is given, then true is returned' , ( ) => {
7- expect ( ThumbnailUtils . isFileThumbnailable ( 'jpg' ) ) . toBe ( true ) ;
8- expect ( ThumbnailUtils . isFileThumbnailable ( 'jpeg' ) ) . toBe ( true ) ;
9- expect ( ThumbnailUtils . isFileThumbnailable ( 'png' ) ) . toBe ( true ) ;
10- expect ( ThumbnailUtils . isFileThumbnailable ( 'webp' ) ) . toBe ( true ) ;
11- expect ( ThumbnailUtils . isFileThumbnailable ( 'gif' ) ) . toBe ( true ) ;
12- expect ( ThumbnailUtils . isFileThumbnailable ( 'tif' ) ) . toBe ( true ) ;
13- expect ( ThumbnailUtils . isFileThumbnailable ( 'tiff' ) ) . toBe ( true ) ;
14- } ) ;
15-
16- test ( 'when an extension has mixed case, then true is returned' , ( ) => {
17- expect ( ThumbnailUtils . isFileThumbnailable ( 'JPG' ) ) . toBe ( true ) ;
18- expect ( ThumbnailUtils . isFileThumbnailable ( 'PNG' ) ) . toBe ( true ) ;
19- expect ( ThumbnailUtils . isFileThumbnailable ( 'Webp' ) ) . toBe ( true ) ;
20- expect ( ThumbnailUtils . isFileThumbnailable ( 'GIF' ) ) . toBe ( true ) ;
21- } ) ;
22-
23- test ( 'when an extension has surrounding whitespace, then true is returned' , ( ) => {
24- expect ( ThumbnailUtils . isFileThumbnailable ( ' jpg ' ) ) . toBe ( true ) ;
25- expect ( ThumbnailUtils . isFileThumbnailable ( ' png ' ) ) . toBe ( true ) ;
26- expect ( ThumbnailUtils . isFileThumbnailable ( '\tgif\t' ) ) . toBe ( true ) ;
27- } ) ;
28-
29- test ( 'when a non-thumbnailable extension is given, then false is returned' , ( ) => {
30- expect ( ThumbnailUtils . isFileThumbnailable ( 'pdf' ) ) . toBe ( false ) ;
31- expect ( ThumbnailUtils . isFileThumbnailable ( 'doc' ) ) . toBe ( false ) ;
32- expect ( ThumbnailUtils . isFileThumbnailable ( 'txt' ) ) . toBe ( false ) ;
33- expect ( ThumbnailUtils . isFileThumbnailable ( 'mp4' ) ) . toBe ( false ) ;
34- expect ( ThumbnailUtils . isFileThumbnailable ( 'bmp' ) ) . toBe ( false ) ;
35- expect ( ThumbnailUtils . isFileThumbnailable ( 'raw' ) ) . toBe ( false ) ;
36- expect ( ThumbnailUtils . isFileThumbnailable ( 'heic' ) ) . toBe ( false ) ;
37- } ) ;
38-
39- test ( 'when an empty or blank string is given, then false is returned' , ( ) => {
40- expect ( ThumbnailUtils . isFileThumbnailable ( '' ) ) . toBe ( false ) ;
41- expect ( ThumbnailUtils . isFileThumbnailable ( ' ' ) ) . toBe ( false ) ;
42- expect ( ThumbnailUtils . isFileThumbnailable ( '\t\n' ) ) . toBe ( false ) ;
43- } ) ;
44-
45- test ( 'when an invalid extension is given, then false is returned' , ( ) => {
46- expect ( ThumbnailUtils . isFileThumbnailable ( 'unknown' ) ) . toBe ( false ) ;
47- expect ( ThumbnailUtils . isFileThumbnailable ( 'jpgg' ) ) . toBe ( false ) ;
48- } ) ;
49- } ) ;
50-
51- describe ( 'isPDFThumbnailable' , ( ) => {
52- test ( 'when a pdf extension is given, then true is returned' , ( ) => {
53- expect ( ThumbnailUtils . isPDFThumbnailable ( 'pdf' ) ) . toBe ( true ) ;
54- } ) ;
55-
56- test ( 'when an extension has mixed case, then true is returned' , ( ) => {
57- expect ( ThumbnailUtils . isPDFThumbnailable ( 'PDF' ) ) . toBe ( true ) ;
58- expect ( ThumbnailUtils . isPDFThumbnailable ( 'Pdf' ) ) . toBe ( true ) ;
59- expect ( ThumbnailUtils . isPDFThumbnailable ( 'pDf' ) ) . toBe ( true ) ;
60- } ) ;
61-
62- test ( 'when an extension has surrounding whitespace, then true is returned' , ( ) => {
63- expect ( ThumbnailUtils . isPDFThumbnailable ( ' pdf ' ) ) . toBe ( true ) ;
64- expect ( ThumbnailUtils . isPDFThumbnailable ( ' PDF ' ) ) . toBe ( true ) ;
65- expect ( ThumbnailUtils . isPDFThumbnailable ( '\tpdf\n' ) ) . toBe ( true ) ;
66- } ) ;
67-
68- test ( 'when a non-pdf extension is given, then false is returned' , ( ) => {
69- expect ( ThumbnailUtils . isPDFThumbnailable ( 'jpg' ) ) . toBe ( false ) ;
70- expect ( ThumbnailUtils . isPDFThumbnailable ( 'png' ) ) . toBe ( false ) ;
71- expect ( ThumbnailUtils . isPDFThumbnailable ( 'doc' ) ) . toBe ( false ) ;
72- expect ( ThumbnailUtils . isPDFThumbnailable ( 'docx' ) ) . toBe ( false ) ;
73- expect ( ThumbnailUtils . isPDFThumbnailable ( 'txt' ) ) . toBe ( false ) ;
74- } ) ;
75-
76- test ( 'when an empty or blank string is given, then false is returned' , ( ) => {
77- expect ( ThumbnailUtils . isPDFThumbnailable ( '' ) ) . toBe ( false ) ;
78- expect ( ThumbnailUtils . isPDFThumbnailable ( ' ' ) ) . toBe ( false ) ;
79- expect ( ThumbnailUtils . isPDFThumbnailable ( '\t\n' ) ) . toBe ( false ) ;
80- } ) ;
81-
82- test ( 'when an invalid extension is given, then false is returned' , ( ) => {
83- expect ( ThumbnailUtils . isPDFThumbnailable ( 'pdff' ) ) . toBe ( false ) ;
84- expect ( ThumbnailUtils . isPDFThumbnailable ( 'pd' ) ) . toBe ( false ) ;
85- } ) ;
86- } ) ;
87-
885 describe ( 'isImageThumbnailable' , ( ) => {
6+ const validSize = 1024 ;
7+
898 test ( 'when a thumbnailable image extension is given, then true is returned' , ( ) => {
90- expect ( ThumbnailUtils . isImageThumbnailable ( 'jpg' ) ) . toBe ( true ) ;
91- expect ( ThumbnailUtils . isImageThumbnailable ( 'jpeg' ) ) . toBe ( true ) ;
92- expect ( ThumbnailUtils . isImageThumbnailable ( 'png' ) ) . toBe ( true ) ;
93- expect ( ThumbnailUtils . isImageThumbnailable ( 'webp' ) ) . toBe ( true ) ;
94- expect ( ThumbnailUtils . isImageThumbnailable ( 'gif' ) ) . toBe ( true ) ;
95- expect ( ThumbnailUtils . isImageThumbnailable ( 'tif' ) ) . toBe ( true ) ;
96- expect ( ThumbnailUtils . isImageThumbnailable ( 'tiff' ) ) . toBe ( true ) ;
9+ expect ( ThumbnailUtils . isImageThumbnailable ( 'jpg' , validSize ) ) . toBe ( true ) ;
10+ expect ( ThumbnailUtils . isImageThumbnailable ( 'jpeg' , validSize ) ) . toBe ( true ) ;
11+ expect ( ThumbnailUtils . isImageThumbnailable ( 'png' , validSize ) ) . toBe ( true ) ;
12+ expect ( ThumbnailUtils . isImageThumbnailable ( 'webp' , validSize ) ) . toBe ( true ) ;
13+ expect ( ThumbnailUtils . isImageThumbnailable ( 'gif' , validSize ) ) . toBe ( true ) ;
14+ expect ( ThumbnailUtils . isImageThumbnailable ( 'tif' , validSize ) ) . toBe ( true ) ;
15+ expect ( ThumbnailUtils . isImageThumbnailable ( 'tiff' , validSize ) ) . toBe ( true ) ;
9716 } ) ;
9817
9918 test ( 'when an extension has mixed case, then true is returned' , ( ) => {
100- expect ( ThumbnailUtils . isImageThumbnailable ( 'JPG' ) ) . toBe ( true ) ;
101- expect ( ThumbnailUtils . isImageThumbnailable ( 'PNG' ) ) . toBe ( true ) ;
102- expect ( ThumbnailUtils . isImageThumbnailable ( 'GIF' ) ) . toBe ( true ) ;
103- expect ( ThumbnailUtils . isImageThumbnailable ( 'Jpeg' ) ) . toBe ( true ) ;
19+ expect ( ThumbnailUtils . isImageThumbnailable ( 'JPG' , validSize ) ) . toBe ( true ) ;
20+ expect ( ThumbnailUtils . isImageThumbnailable ( 'PNG' , validSize ) ) . toBe ( true ) ;
21+ expect ( ThumbnailUtils . isImageThumbnailable ( 'GIF' , validSize ) ) . toBe ( true ) ;
22+ expect ( ThumbnailUtils . isImageThumbnailable ( 'Jpeg' , validSize ) ) . toBe ( true ) ;
10423 } ) ;
10524
10625 test ( 'when an extension has surrounding whitespace, then true is returned' , ( ) => {
107- expect ( ThumbnailUtils . isImageThumbnailable ( ' jpg ' ) ) . toBe ( true ) ;
108- expect ( ThumbnailUtils . isImageThumbnailable ( ' png ' ) ) . toBe ( true ) ;
109- expect ( ThumbnailUtils . isImageThumbnailable ( '\twebp\n' ) ) . toBe ( true ) ;
26+ expect ( ThumbnailUtils . isImageThumbnailable ( ' jpg ' , validSize ) ) . toBe ( true ) ;
27+ expect ( ThumbnailUtils . isImageThumbnailable ( ' png ' , validSize ) ) . toBe ( true ) ;
28+ expect ( ThumbnailUtils . isImageThumbnailable ( '\twebp\n' , validSize ) ) . toBe ( true ) ;
11029 } ) ;
11130
11231 test ( 'when a non-thumbnailable image format is given, then false is returned' , ( ) => {
113- expect ( ThumbnailUtils . isImageThumbnailable ( 'bmp' ) ) . toBe ( false ) ;
114- expect ( ThumbnailUtils . isImageThumbnailable ( 'heic' ) ) . toBe ( false ) ;
115- expect ( ThumbnailUtils . isImageThumbnailable ( 'raw' ) ) . toBe ( false ) ;
116- expect ( ThumbnailUtils . isImageThumbnailable ( 'cr2' ) ) . toBe ( false ) ;
117- expect ( ThumbnailUtils . isImageThumbnailable ( 'nef' ) ) . toBe ( false ) ;
118- expect ( ThumbnailUtils . isImageThumbnailable ( 'eps' ) ) . toBe ( false ) ;
32+ expect ( ThumbnailUtils . isImageThumbnailable ( 'bmp' , validSize ) ) . toBe ( false ) ;
33+ expect ( ThumbnailUtils . isImageThumbnailable ( 'heic' , validSize ) ) . toBe ( false ) ;
34+ expect ( ThumbnailUtils . isImageThumbnailable ( 'raw' , validSize ) ) . toBe ( false ) ;
35+ expect ( ThumbnailUtils . isImageThumbnailable ( 'cr2' , validSize ) ) . toBe ( false ) ;
36+ expect ( ThumbnailUtils . isImageThumbnailable ( 'nef' , validSize ) ) . toBe ( false ) ;
37+ expect ( ThumbnailUtils . isImageThumbnailable ( 'eps' , validSize ) ) . toBe ( false ) ;
11938 } ) ;
12039
12140 test ( 'when a non-image extension is given, then false is returned' , ( ) => {
122- expect ( ThumbnailUtils . isImageThumbnailable ( 'pdf' ) ) . toBe ( false ) ;
123- expect ( ThumbnailUtils . isImageThumbnailable ( 'doc' ) ) . toBe ( false ) ;
124- expect ( ThumbnailUtils . isImageThumbnailable ( 'txt' ) ) . toBe ( false ) ;
125- expect ( ThumbnailUtils . isImageThumbnailable ( 'mp4' ) ) . toBe ( false ) ;
126- expect ( ThumbnailUtils . isImageThumbnailable ( 'mp3' ) ) . toBe ( false ) ;
41+ expect ( ThumbnailUtils . isImageThumbnailable ( 'pdf' , validSize ) ) . toBe ( false ) ;
42+ expect ( ThumbnailUtils . isImageThumbnailable ( 'doc' , validSize ) ) . toBe ( false ) ;
43+ expect ( ThumbnailUtils . isImageThumbnailable ( 'txt' , validSize ) ) . toBe ( false ) ;
44+ expect ( ThumbnailUtils . isImageThumbnailable ( 'mp4' , validSize ) ) . toBe ( false ) ;
45+ expect ( ThumbnailUtils . isImageThumbnailable ( 'mp3' , validSize ) ) . toBe ( false ) ;
12746 } ) ;
12847
12948 test ( 'when an empty or blank string is given, then false is returned' , ( ) => {
130- expect ( ThumbnailUtils . isImageThumbnailable ( '' ) ) . toBe ( false ) ;
131- expect ( ThumbnailUtils . isImageThumbnailable ( ' ' ) ) . toBe ( false ) ;
132- expect ( ThumbnailUtils . isImageThumbnailable ( '\t\n' ) ) . toBe ( false ) ;
49+ expect ( ThumbnailUtils . isImageThumbnailable ( '' , validSize ) ) . toBe ( false ) ;
50+ expect ( ThumbnailUtils . isImageThumbnailable ( ' ' , validSize ) ) . toBe ( false ) ;
51+ expect ( ThumbnailUtils . isImageThumbnailable ( '\t\n' , validSize ) ) . toBe ( false ) ;
13352 } ) ;
13453
13554 test ( 'when an invalid extension is given, then false is returned' , ( ) => {
136- expect ( ThumbnailUtils . isImageThumbnailable ( 'jpgg' ) ) . toBe ( false ) ;
137- expect ( ThumbnailUtils . isImageThumbnailable ( 'unknown' ) ) . toBe ( false ) ;
55+ expect ( ThumbnailUtils . isImageThumbnailable ( 'jpgg' , validSize ) ) . toBe ( false ) ;
56+ expect ( ThumbnailUtils . isImageThumbnailable ( 'unknown' , validSize ) ) . toBe ( false ) ;
57+ } ) ;
58+
59+ test ( 'when the size is zero or negative, then false is returned' , ( ) => {
60+ expect ( ThumbnailUtils . isImageThumbnailable ( 'jpg' , 0 ) ) . toBe ( false ) ;
61+ expect ( ThumbnailUtils . isImageThumbnailable ( 'jpg' , - 1 ) ) . toBe ( false ) ;
62+ } ) ;
63+
64+ test ( 'when the size exceeds the max thumbnailable size, then false is returned' , ( ) => {
65+ expect ( ThumbnailUtils . isImageThumbnailable ( 'jpg' , ThumbnailUtils . MAX_IMAGE_THUMBNAILABLE_SIZE_IN_BYTES + 1 ) ) . toBe (
66+ false ,
67+ ) ;
13868 } ) ;
13969 } ) ;
14070} ) ;
0 commit comments