11import {
2+ calculateCropDimensions ,
23 handleResizeImage ,
4+ isPointInCropRect ,
35 processImageFile ,
46 resizeImage ,
57 updateHeight ,
@@ -15,11 +17,19 @@ describe("Image Processing Functions", () => {
1517 canvasMock = document . createElement ( "canvas" ) ;
1618 ctxMock = {
1719 drawImage : jest . fn ( ) ,
18- toDataURL : jest . fn ( ) . mockReturnValue ( "data:image/png;base64,MOCK_DATA" ) ,
1920 } as unknown as CanvasRenderingContext2D ;
2021
2122 jest . spyOn ( document , "createElement" ) . mockReturnValue ( canvasMock ) ;
2223 jest . spyOn ( canvasMock , "getContext" ) . mockReturnValue ( ctxMock ) ;
24+ jest
25+ . spyOn ( canvasMock , "toBlob" )
26+ . mockImplementation ( ( callback : BlobCallback ) => {
27+ callback ( new Blob ( [ "MOCK_DATA" ] , { type : "image/png" } ) ) ;
28+ } ) ;
29+ Object . defineProperty ( URL , "createObjectURL" , {
30+ writable : true ,
31+ value : jest . fn ( ( ) => "blob:mock-url" ) ,
32+ } ) ;
2333
2434 jest . spyOn ( window , "FileReader" ) . mockImplementation (
2535 ( ) =>
@@ -59,7 +69,7 @@ describe("Image Processing Functions", () => {
5969 quality : 1 ,
6070 } ) ;
6171
62- expect ( result ) . toMatch ( / ^ d a t a : i m a g e \/ p n g ; b a s e 6 4 , / ) ;
72+ expect ( result ) . toMatch ( / ^ b l o b : / ) ;
6373 expect ( ctxMock . drawImage ) . toHaveBeenCalledWith ( img , 0 , 0 , 500 , 250 ) ;
6474 } ) ;
6575
@@ -74,7 +84,7 @@ describe("Image Processing Functions", () => {
7484 quality : 0.8 ,
7585 } ) ;
7686
77- expect ( result ) . toMatch ( / ^ d a t a : i m a g e \/ j p e g ; b a s e 6 4 , / ) ;
87+ expect ( result ) . toMatch ( / ^ b l o b : / ) ;
7888 expect ( ctxMock . drawImage ) . toHaveBeenCalledWith ( img , 0 , 0 , 500 , 250 ) ;
7989 } ) ;
8090
@@ -97,7 +107,7 @@ describe("Image Processing Functions", () => {
97107 const setOutput = jest . fn ( ) ;
98108
99109 processImageFile ( {
100- file : mockFile ,
110+ source : mockFile ,
101111 format : "jpeg" ,
102112 preserveAspectRatio : true ,
103113 quality : 0.8 ,
@@ -107,9 +117,7 @@ describe("Image Processing Functions", () => {
107117 done : ( ) => {
108118 expect ( setWidth ) . toHaveBeenCalledWith ( 1000 ) ;
109119 expect ( setHeight ) . toHaveBeenCalledWith ( 500 ) ;
110- expect ( setOutput ) . toHaveBeenCalledWith (
111- expect . stringMatching ( / ^ d a t a : i m a g e \/ j p e g ; b a s e 6 4 , / )
112- ) ;
120+ expect ( setOutput ) . toHaveBeenCalledWith ( expect . stringMatching ( / ^ b l o b : / ) ) ;
113121 done ( ) ;
114122 } ,
115123 } ) ;
@@ -121,7 +129,7 @@ describe("Image Processing Functions", () => {
121129 } ) ;
122130 const setWidth = jest . fn ( ) ;
123131
124- updateWidth ( { file : mockFile , height : 200 , setWidth } ) ;
132+ updateWidth ( { source : mockFile , height : 200 , setWidth } ) ;
125133
126134 setTimeout ( ( ) => {
127135 expect ( setWidth ) . toHaveBeenCalledWith ( 400 ) ;
@@ -135,7 +143,7 @@ describe("Image Processing Functions", () => {
135143 } ) ;
136144 const setHeight = jest . fn ( ) ;
137145
138- updateHeight ( { file : mockFile , width : 300 , setHeight } ) ;
146+ updateHeight ( { source : mockFile , width : 300 , setHeight } ) ;
139147
140148 setTimeout ( ( ) => {
141149 expect ( setHeight ) . toHaveBeenCalledWith ( 150 ) ;
@@ -150,7 +158,7 @@ describe("Image Processing Functions", () => {
150158 const setOutput = jest . fn ( ) ;
151159
152160 handleResizeImage ( {
153- file : mockFile ,
161+ source : mockFile ,
154162 format : "jpeg" ,
155163 height : 400 ,
156164 width : 600 ,
@@ -160,10 +168,128 @@ describe("Image Processing Functions", () => {
160168 } ) ;
161169
162170 setTimeout ( ( ) => {
163- expect ( setOutput ) . toHaveBeenCalledWith (
164- expect . stringMatching ( / ^ d a t a : i m a g e \/ j p e g ; b a s e 6 4 , / )
165- ) ;
171+ expect ( setOutput ) . toHaveBeenCalledWith ( expect . stringMatching ( / ^ b l o b : / ) ) ;
166172 done ( ) ;
167173 } , 0 ) ;
168174 } ) ;
175+
176+ it ( "should calculate the crop dimensions correctly" , ( ) => {
177+ const imgMock = {
178+ naturalWidth : 1000 ,
179+ naturalHeight : 500 ,
180+ width : 1000 ,
181+ height : 500 ,
182+ } as HTMLImageElement ;
183+
184+ const currentImageRefMock = {
185+ clientWidth : 500 ,
186+ clientHeight : 250 ,
187+ getBoundingClientRect : jest . fn ( ( ) => ( {
188+ width : 500 ,
189+ height : 250 ,
190+ } ) ) ,
191+ } as unknown as HTMLImageElement ;
192+
193+ const cropRect = { x : 50 , y : 50 , width : 100 , height : 50 } ;
194+
195+ const result = calculateCropDimensions (
196+ imgMock ,
197+ currentImageRefMock ,
198+ cropRect
199+ ) ;
200+
201+ expect ( result ) . toEqual ( {
202+ x : 100 ,
203+ y : 100 ,
204+ width : 200 ,
205+ height : 100 ,
206+ } ) ;
207+ } ) ;
208+
209+ it ( "should handle negative width and height values in cropRect" , ( ) => {
210+ const imgMock = {
211+ naturalWidth : 1000 ,
212+ naturalHeight : 500 ,
213+ width : 1000 ,
214+ height : 500 ,
215+ } as HTMLImageElement ;
216+
217+ const currentImageRefMock = {
218+ clientWidth : 500 ,
219+ clientHeight : 250 ,
220+ getBoundingClientRect : jest . fn ( ( ) => ( {
221+ width : 500 ,
222+ height : 250 ,
223+ } ) ) ,
224+ } as unknown as HTMLImageElement ;
225+
226+ const cropRect = { x : 150 , y : 150 , width : - 100 , height : - 50 } ;
227+
228+ const result = calculateCropDimensions (
229+ imgMock ,
230+ currentImageRefMock ,
231+ cropRect
232+ ) ;
233+
234+ expect ( result ) . toEqual ( {
235+ x : 100 ,
236+ y : 200 ,
237+ width : 200 ,
238+ height : 100 ,
239+ } ) ;
240+ } ) ;
241+
242+ it ( "should clamp crop dimensions to image boundaries" , ( ) => {
243+ const imgMock = {
244+ naturalWidth : 1000 ,
245+ naturalHeight : 500 ,
246+ width : 1000 ,
247+ height : 500 ,
248+ } as HTMLImageElement ;
249+
250+ const currentImageRefMock = {
251+ clientWidth : 500 ,
252+ clientHeight : 250 ,
253+ getBoundingClientRect : jest . fn ( ( ) => ( {
254+ width : 500 ,
255+ height : 250 ,
256+ } ) ) ,
257+ } as unknown as HTMLImageElement ;
258+
259+ const cropRect = { x : - 10 , y : - 20 , width : 600 , height : 400 } ;
260+
261+ const result = calculateCropDimensions (
262+ imgMock ,
263+ currentImageRefMock ,
264+ cropRect
265+ ) ;
266+
267+ expect ( result ) . toEqual ( {
268+ x : 0 ,
269+ y : 0 ,
270+ width : 1000 ,
271+ height : 500 ,
272+ } ) ;
273+ } ) ;
274+
275+ const cropRect = { x : 50 , y : 50 , width : 100 , height : 50 } ;
276+
277+ it ( "should return true for a point inside the crop rectangle" , ( ) => {
278+ const result = isPointInCropRect ( 75 , 75 , cropRect ) ;
279+ expect ( result ) . toBe ( true ) ;
280+ } ) ;
281+
282+ it ( "should return false for a point outside the crop rectangle" , ( ) => {
283+ const result = isPointInCropRect ( 200 , 200 , cropRect ) ;
284+ expect ( result ) . toBe ( false ) ;
285+ } ) ;
286+
287+ it ( "should handle negative width and height in crop rectangle" , ( ) => {
288+ const cropRectNegative = { x : 150 , y : 150 , width : - 100 , height : - 50 } ;
289+ const result = isPointInCropRect ( 75 , 75 , cropRectNegative ) ;
290+ expect ( result ) . toBe ( false ) ;
291+
292+ const resultInside = isPointInCropRect ( 125 , 125 , cropRectNegative ) ;
293+ expect ( resultInside ) . toBe ( true ) ;
294+ } ) ;
169295} ) ;
0 commit comments