@wordpress/media-editor 0.17.2-next.v.202608281122.0 → 0.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/build/components/media-editor/index.cjs +43 -11
  3. package/build/components/media-editor/index.cjs.map +2 -2
  4. package/build/components/media-editor-canvas/index.cjs +4 -2
  5. package/build/components/media-editor-canvas/index.cjs.map +2 -2
  6. package/build/components/media-editor-crop-panel/index.cjs +4 -2
  7. package/build/components/media-editor-crop-panel/index.cjs.map +2 -2
  8. package/build/components/media-editor-fine-rotation/index.cjs +4 -2
  9. package/build/components/media-editor-fine-rotation/index.cjs.map +2 -2
  10. package/build/components/media-editor-image-controls/index.cjs +17 -4
  11. package/build/components/media-editor-image-controls/index.cjs.map +2 -2
  12. package/build/components/rotation-ruler/index.cjs +1 -0
  13. package/build/components/rotation-ruler/index.cjs.map +2 -2
  14. package/build/components/rotation-ruler/use-ruler-drag.cjs +7 -2
  15. package/build/components/rotation-ruler/use-ruler-drag.cjs.map +2 -2
  16. package/build/image-editor/core/interaction-controller.cjs +3 -0
  17. package/build/image-editor/core/interaction-controller.cjs.map +2 -2
  18. package/build/image-editor/react/components/cropper.cjs +5 -3
  19. package/build/image-editor/react/components/cropper.cjs.map +2 -2
  20. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +12 -5
  21. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +2 -2
  22. package/build/image-editor/react/hooks/use-interaction.cjs +31 -0
  23. package/build/image-editor/react/hooks/use-interaction.cjs.map +2 -2
  24. package/build-module/components/media-editor/index.mjs +43 -11
  25. package/build-module/components/media-editor/index.mjs.map +2 -2
  26. package/build-module/components/media-editor-canvas/index.mjs +4 -2
  27. package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
  28. package/build-module/components/media-editor-crop-panel/index.mjs +4 -2
  29. package/build-module/components/media-editor-crop-panel/index.mjs.map +2 -2
  30. package/build-module/components/media-editor-fine-rotation/index.mjs +4 -2
  31. package/build-module/components/media-editor-fine-rotation/index.mjs.map +2 -2
  32. package/build-module/components/media-editor-image-controls/index.mjs +17 -4
  33. package/build-module/components/media-editor-image-controls/index.mjs.map +2 -2
  34. package/build-module/components/rotation-ruler/index.mjs +1 -0
  35. package/build-module/components/rotation-ruler/index.mjs.map +2 -2
  36. package/build-module/components/rotation-ruler/use-ruler-drag.mjs +7 -2
  37. package/build-module/components/rotation-ruler/use-ruler-drag.mjs.map +2 -2
  38. package/build-module/image-editor/core/interaction-controller.mjs +3 -0
  39. package/build-module/image-editor/core/interaction-controller.mjs.map +2 -2
  40. package/build-module/image-editor/react/components/cropper.mjs +5 -3
  41. package/build-module/image-editor/react/components/cropper.mjs.map +2 -2
  42. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +12 -5
  43. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +2 -2
  44. package/build-module/image-editor/react/hooks/use-interaction.mjs +31 -0
  45. package/build-module/image-editor/react/hooks/use-interaction.mjs.map +2 -2
  46. package/build-style/style-rtl.css +5 -12
  47. package/build-style/style.css +5 -12
  48. package/build-types/components/media-editor/index.d.ts.map +1 -1
  49. package/build-types/components/media-editor-canvas/index.d.ts +7 -1
  50. package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
  51. package/build-types/components/media-editor-crop-panel/index.d.ts +4 -1
  52. package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
  53. package/build-types/components/media-editor-fine-rotation/index.d.ts +7 -1
  54. package/build-types/components/media-editor-fine-rotation/index.d.ts.map +1 -1
  55. package/build-types/components/media-editor-image-controls/index.d.ts +7 -1
  56. package/build-types/components/media-editor-image-controls/index.d.ts.map +1 -1
  57. package/build-types/components/rotation-ruler/index.d.ts.map +1 -1
  58. package/build-types/components/rotation-ruler/use-ruler-drag.d.ts.map +1 -1
  59. package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -1
  60. package/build-types/image-editor/react/components/cropper.d.ts +7 -0
  61. package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
  62. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -1
  63. package/build-types/image-editor/react/hooks/use-interaction.d.ts +5 -0
  64. package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +1 -1
  65. package/global.d.ts +1 -0
  66. package/package.json +23 -22
  67. package/src/components/media-editor/index.tsx +42 -5
  68. package/src/components/media-editor/test/{use-crop-options.tsx → use-crop-options.jsdom.test.tsx} +1 -0
  69. package/src/components/media-editor-canvas/index.tsx +8 -0
  70. package/src/components/media-editor-crop-panel/index.tsx +6 -1
  71. package/src/components/media-editor-crop-panel/test/{index.tsx → index.jsdom.test.tsx} +5 -2
  72. package/src/components/media-editor-fine-rotation/index.tsx +8 -0
  73. package/src/components/media-editor-image-controls/index.tsx +22 -3
  74. package/src/components/media-editor-image-controls/test/{index.tsx → index.jsdom.test.tsx} +37 -0
  75. package/src/components/media-editor-modal/test/build-modifiers.test.ts +24 -14
  76. package/src/components/media-editor-modal/test/{index.tsx → index.jsdom.test.tsx} +65 -35
  77. package/src/components/rotation-ruler/index.tsx +1 -0
  78. package/src/components/rotation-ruler/style.scss +1 -1
  79. package/src/components/rotation-ruler/test/{index.tsx → index.jsdom.test.tsx} +42 -4
  80. package/src/components/rotation-ruler/use-ruler-drag.ts +10 -2
  81. package/src/image-editor/core/export/test/{canvas-renderer.ts → canvas-renderer.jsdom.test.ts} +76 -63
  82. package/src/image-editor/core/interaction-controller.ts +8 -0
  83. package/src/image-editor/core/math/test/sanitize.ts +1 -0
  84. package/src/image-editor/core/test/camera.ts +2 -3
  85. package/src/image-editor/core/test/interaction-controller.ts +50 -40
  86. package/src/image-editor/core/test/preview-export-parity.ts +31 -23
  87. package/src/image-editor/core/test/render-camera-bridge.ts +1 -0
  88. package/src/image-editor/core/test/state.ts +1 -0
  89. package/src/image-editor/core/test/stencil-math.ts +1 -0
  90. package/src/image-editor/core/transforms/test/pipeline.ts +1 -0
  91. package/src/image-editor/react/components/cropper.tsx +12 -1
  92. package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +22 -6
  93. package/src/image-editor/react/components/stencils/test/{rectangle-stencil.tsx → rectangle-stencil.jsdom.test.tsx} +54 -36
  94. package/src/image-editor/react/components/test/{cropper.tsx → cropper.jsdom.test.tsx} +304 -0
  95. package/src/image-editor/react/hooks/test/{use-aria-announcer.ts → use-aria-announcer.jsdom.test.ts} +57 -44
  96. package/src/image-editor/react/hooks/test/{use-cropper-reducer.ts → use-cropper-reducer.jsdom.test.ts} +1 -0
  97. package/src/image-editor/react/hooks/test/{use-interaction.ts → use-interaction.jsdom.test.ts} +17 -16
  98. package/src/image-editor/react/hooks/test/{use-transform-style.ts → use-transform-style.jsdom.test.ts} +1 -0
  99. package/src/image-editor/react/hooks/use-interaction.ts +47 -0
  100. package/src/state/test/{use-media-editor-state.ts → use-media-editor-state.jsdom.test.ts} +1 -0
  101. package/src/store/test/reducer.ts +9 -8
  102. package/tsconfig.build.tsbuildinfo +1 -1
  103. package/tsconfig.json +8 -3
@@ -1,3 +1,12 @@
1
+ import {
2
+ afterEach,
3
+ beforeEach,
4
+ describe,
5
+ expect,
6
+ it,
7
+ vi,
8
+ type Mock,
9
+ } from 'vitest';
1
10
  import type { CropperState } from '../../types';
2
11
  import { DEFAULT_STATE } from '../../constants';
3
12
  import {
@@ -14,18 +23,18 @@ import {
14
23
  */
15
24
  function createMockContext() {
16
25
  return {
17
- setTransform: jest.fn(),
18
- translate: jest.fn(),
19
- rotate: jest.fn(),
20
- scale: jest.fn(),
21
- drawImage: jest.fn(),
22
- getImageData: jest.fn(),
23
- putImageData: jest.fn(),
24
- beginPath: jest.fn(),
25
- closePath: jest.fn(),
26
- moveTo: jest.fn(),
27
- lineTo: jest.fn(),
28
- clip: jest.fn(),
26
+ setTransform: vi.fn(),
27
+ translate: vi.fn(),
28
+ rotate: vi.fn(),
29
+ scale: vi.fn(),
30
+ drawImage: vi.fn(),
31
+ getImageData: vi.fn(),
32
+ putImageData: vi.fn(),
33
+ beginPath: vi.fn(),
34
+ closePath: vi.fn(),
35
+ moveTo: vi.fn(),
36
+ lineTo: vi.fn(),
37
+ clip: vi.fn(),
29
38
  };
30
39
  }
31
40
 
@@ -38,9 +47,9 @@ function createMockCanvas( ctx: ReturnType< typeof createMockContext > ) {
38
47
  const canvas = {
39
48
  width: 0,
40
49
  height: 0,
41
- getContext: jest.fn().mockReturnValue( ctx ),
42
- toBlob: jest.fn(),
43
- toDataURL: jest.fn().mockReturnValue( 'data:image/png;base64,abc' ),
50
+ getContext: vi.fn().mockReturnValue( ctx ),
51
+ toBlob: vi.fn(),
52
+ toDataURL: vi.fn().mockReturnValue( 'data:image/png;base64,abc' ),
44
53
  };
45
54
  return canvas as unknown as HTMLCanvasElement;
46
55
  }
@@ -68,7 +77,7 @@ function createTestState( overrides?: Partial< CropperState > ): CropperState {
68
77
  describe( 'loadImage', () => {
69
78
  it( 'should create an image element with crossOrigin set', async () => {
70
79
  const mockImage = {
71
- addEventListener: jest.fn(),
80
+ addEventListener: vi.fn(),
72
81
  set crossOrigin( value: string ) {
73
82
  this._crossOrigin = value;
74
83
  },
@@ -94,7 +103,9 @@ describe( 'loadImage', () => {
94
103
 
95
104
  // Replace global Image constructor.
96
105
  const originalImage = global.Image;
97
- global.Image = jest.fn( () => mockImage ) as unknown as typeof Image;
106
+ global.Image = vi.fn( function Image() {
107
+ return mockImage;
108
+ } ) as unknown as typeof Image;
98
109
 
99
110
  try {
100
111
  const result = await loadImage( 'https://example.com/test.jpg' );
@@ -117,7 +128,7 @@ describe( 'loadImage', () => {
117
128
  it( 'should reject when the image fails to load', async () => {
118
129
  const loadError = new Error( 'Network error' );
119
130
  const mockImage = {
120
- addEventListener: jest.fn(),
131
+ addEventListener: vi.fn(),
121
132
  set crossOrigin( _value: string ) {},
122
133
  set src( _value: string ) {
123
134
  // Simulate error on next tick.
@@ -131,7 +142,9 @@ describe( 'loadImage', () => {
131
142
  };
132
143
 
133
144
  const originalImage = global.Image;
134
- global.Image = jest.fn( () => mockImage ) as unknown as typeof Image;
145
+ global.Image = vi.fn( function Image() {
146
+ return mockImage;
147
+ } ) as unknown as typeof Image;
135
148
 
136
149
  try {
137
150
  await expect(
@@ -152,7 +165,7 @@ describe( 'renderToCanvas', () => {
152
165
  canvasCount = 0;
153
166
 
154
167
  // Mock document.createElement to return our mock canvases.
155
- jest.spyOn( document, 'createElement' ).mockImplementation(
168
+ vi.spyOn( document, 'createElement' ).mockImplementation(
156
169
  ( tag: string ) => {
157
170
  if ( tag === 'canvas' ) {
158
171
  canvasCount++;
@@ -166,7 +179,7 @@ describe( 'renderToCanvas', () => {
166
179
  } );
167
180
 
168
181
  afterEach( () => {
169
- jest.restoreAllMocks();
182
+ vi.restoreAllMocks();
170
183
  } );
171
184
 
172
185
  it( 'should call setTransform and drawImage', () => {
@@ -211,8 +224,8 @@ describe( 'renderToCanvas', () => {
211
224
  } );
212
225
  const mockCtxNoFlip = createMockContext();
213
226
  const canvasCountRef = { value: 0 };
214
- jest.restoreAllMocks();
215
- jest.spyOn( document, 'createElement' ).mockImplementation(
227
+ vi.restoreAllMocks();
228
+ vi.spyOn( document, 'createElement' ).mockImplementation(
216
229
  ( tag: string ) => {
217
230
  if ( tag === 'canvas' ) {
218
231
  canvasCountRef.value++;
@@ -252,8 +265,8 @@ describe( 'renderToCanvas', () => {
252
265
  const mockCtxRotated = createMockContext();
253
266
  const mockCtxNormal = createMockContext();
254
267
  const canvasCountRef = { value: 0 };
255
- jest.restoreAllMocks();
256
- jest.spyOn( document, 'createElement' ).mockImplementation(
268
+ vi.restoreAllMocks();
269
+ vi.spyOn( document, 'createElement' ).mockImplementation(
257
270
  ( tag: string ) => {
258
271
  if ( tag === 'canvas' ) {
259
272
  canvasCountRef.value++;
@@ -287,7 +300,7 @@ describe( 'canvasToBlob', () => {
287
300
  it( 'should call canvas.toBlob with the correct arguments', async () => {
288
301
  const mockBlob = new Blob( [ 'test' ], { type: 'image/png' } );
289
302
  const mockCanvas = {
290
- toBlob: jest.fn( ( callback: BlobCallback ) => {
303
+ toBlob: vi.fn( ( callback: BlobCallback ) => {
291
304
  callback( mockBlob );
292
305
  } ),
293
306
  } as unknown as HTMLCanvasElement;
@@ -305,7 +318,7 @@ describe( 'canvasToBlob', () => {
305
318
  it( 'should use default mime type and quality when not specified', async () => {
306
319
  const mockBlob = new Blob( [ 'test' ], { type: 'image/png' } );
307
320
  const mockCanvas = {
308
- toBlob: jest.fn( ( callback: BlobCallback ) => {
321
+ toBlob: vi.fn( ( callback: BlobCallback ) => {
309
322
  callback( mockBlob );
310
323
  } ),
311
324
  } as unknown as HTMLCanvasElement;
@@ -321,7 +334,7 @@ describe( 'canvasToBlob', () => {
321
334
 
322
335
  it( 'should reject when toBlob returns null', async () => {
323
336
  const mockCanvas = {
324
- toBlob: jest.fn( ( callback: BlobCallback ) => {
337
+ toBlob: vi.fn( ( callback: BlobCallback ) => {
325
338
  callback( null );
326
339
  } ),
327
340
  } as unknown as HTMLCanvasElement;
@@ -335,9 +348,7 @@ describe( 'canvasToBlob', () => {
335
348
  describe( 'canvasToDataURL', () => {
336
349
  it( 'should call canvas.toDataURL with correct arguments', () => {
337
350
  const mockCanvas = {
338
- toDataURL: jest
339
- .fn()
340
- .mockReturnValue( 'data:image/jpeg;base64,abc' ),
351
+ toDataURL: vi.fn().mockReturnValue( 'data:image/jpeg;base64,abc' ),
341
352
  } as unknown as HTMLCanvasElement;
342
353
 
343
354
  const result = canvasToDataURL( mockCanvas, 'image/jpeg', 0.85 );
@@ -351,7 +362,7 @@ describe( 'canvasToDataURL', () => {
351
362
 
352
363
  it( 'should use default mime type and quality when not specified', () => {
353
364
  const mockCanvas = {
354
- toDataURL: jest.fn().mockReturnValue( 'data:image/png;base64,xyz' ),
365
+ toDataURL: vi.fn().mockReturnValue( 'data:image/png;base64,xyz' ),
355
366
  } as unknown as HTMLCanvasElement;
356
367
 
357
368
  canvasToDataURL( mockCanvas );
@@ -373,7 +384,7 @@ describe( 'exportCroppedImage', () => {
373
384
  const mockImage = {
374
385
  naturalWidth: 800,
375
386
  naturalHeight: 600,
376
- addEventListener: jest.fn(),
387
+ addEventListener: vi.fn(),
377
388
  _crossOrigin: '',
378
389
  set crossOrigin( value: string ) {
379
390
  this._crossOrigin = value;
@@ -395,16 +406,18 @@ describe( 'exportCroppedImage', () => {
395
406
  return this._src;
396
407
  },
397
408
  };
398
- global.Image = jest.fn( () => mockImage ) as unknown as typeof Image;
409
+ global.Image = vi.fn( function Image() {
410
+ return mockImage;
411
+ } ) as unknown as typeof Image;
399
412
 
400
413
  // Mock document.createElement for renderToCanvas.
401
414
  const mockBlob = new Blob( [ 'test' ], { type: 'image/png' } );
402
415
  const mockCtx = createMockContext();
403
- jest.spyOn( document, 'createElement' ).mockImplementation(
416
+ vi.spyOn( document, 'createElement' ).mockImplementation(
404
417
  ( tag: string ) => {
405
418
  if ( tag === 'canvas' ) {
406
419
  const canvas = createMockCanvas( mockCtx );
407
- ( canvas as any ).toBlob = jest.fn(
420
+ ( canvas as any ).toBlob = vi.fn(
408
421
  ( callback: BlobCallback ) => {
409
422
  callback( mockBlob );
410
423
  }
@@ -418,7 +431,7 @@ describe( 'exportCroppedImage', () => {
418
431
 
419
432
  afterEach( () => {
420
433
  global.Image = originalImage;
421
- jest.restoreAllMocks();
434
+ vi.restoreAllMocks();
422
435
  } );
423
436
 
424
437
  it( 'should chain loadImage, renderToCanvas, and canvasToBlob', async () => {
@@ -436,7 +449,7 @@ describe( 'exportCroppedImage', () => {
436
449
  it( 'throws when image fails to load', async () => {
437
450
  // Override Image to simulate a load error.
438
451
  const errorImage = {
439
- addEventListener: jest.fn(),
452
+ addEventListener: vi.fn(),
440
453
  set crossOrigin( _value: string ) {},
441
454
  set src( _value: string ) {
442
455
  const errorHandler = this.addEventListener.mock.calls.find(
@@ -447,7 +460,9 @@ describe( 'exportCroppedImage', () => {
447
460
  }
448
461
  },
449
462
  };
450
- global.Image = jest.fn( () => errorImage ) as unknown as typeof Image;
463
+ global.Image = vi.fn( function Image() {
464
+ return errorImage;
465
+ } ) as unknown as typeof Image;
451
466
 
452
467
  const state = createTestState();
453
468
  await expect(
@@ -459,22 +474,20 @@ describe( 'exportCroppedImage', () => {
459
474
  // Mock Image to resolve (simulating a successful load even from
460
475
  // a tainted source — the browser doesn't flag the load itself,
461
476
  // only the later toBlob call).
462
- global.Image = jest.fn( () => {
477
+ global.Image = vi.fn( function Image() {
463
478
  const img: {
464
- addEventListener: jest.Mock;
479
+ addEventListener: Mock;
465
480
  crossOrigin: string;
466
481
  src: string;
467
482
  naturalWidth: number;
468
483
  naturalHeight: number;
469
484
  } = {
470
- addEventListener: jest.fn(
471
- ( event: string, fn: () => void ) => {
472
- if ( event === 'load' ) {
473
- // Fire load synchronously after src is set.
474
- queueMicrotask( fn );
475
- }
485
+ addEventListener: vi.fn( ( event: string, fn: () => void ) => {
486
+ if ( event === 'load' ) {
487
+ // Fire load synchronously after src is set.
488
+ queueMicrotask( fn );
476
489
  }
477
- ),
490
+ } ),
478
491
  crossOrigin: '',
479
492
  src: '',
480
493
  naturalWidth: 800,
@@ -487,18 +500,18 @@ describe( 'exportCroppedImage', () => {
487
500
  const corsCanvas = {
488
501
  width: 0,
489
502
  height: 0,
490
- getContext: jest.fn( () => ( {
491
- setTransform: jest.fn(),
492
- drawImage: jest.fn(),
503
+ getContext: vi.fn( () => ( {
504
+ setTransform: vi.fn(),
505
+ drawImage: vi.fn(),
493
506
  } ) ),
494
- toBlob: jest.fn( () => {
507
+ toBlob: vi.fn( () => {
495
508
  throw new DOMException(
496
509
  'Tainted canvases may not be exported',
497
510
  'SecurityError'
498
511
  );
499
512
  } ),
500
513
  };
501
- jest.spyOn( document, 'createElement' ).mockImplementation( ( (
514
+ vi.spyOn( document, 'createElement' ).mockImplementation( ( (
502
515
  tag: string
503
516
  ) => {
504
517
  if ( tag === 'canvas' ) {
@@ -515,7 +528,7 @@ describe( 'exportCroppedImage', () => {
515
528
  )
516
529
  ).rejects.toMatchObject( { name: 'SecurityError' } );
517
530
 
518
- jest.restoreAllMocks();
531
+ vi.restoreAllMocks();
519
532
  } );
520
533
 
521
534
  it( 'throws descriptive error when canvas context is unavailable', () => {
@@ -523,9 +536,9 @@ describe( 'exportCroppedImage', () => {
523
536
  const noCtxCanvas = {
524
537
  width: 0,
525
538
  height: 0,
526
- getContext: jest.fn( () => null ),
539
+ getContext: vi.fn( () => null ),
527
540
  };
528
- jest.spyOn( document, 'createElement' ).mockImplementation( ( (
541
+ vi.spyOn( document, 'createElement' ).mockImplementation( ( (
529
542
  tag: string
530
543
  ) => {
531
544
  if ( tag === 'canvas' ) {
@@ -544,7 +557,7 @@ describe( 'exportCroppedImage', () => {
544
557
  /2D context/i
545
558
  );
546
559
 
547
- jest.restoreAllMocks();
560
+ vi.restoreAllMocks();
548
561
  } );
549
562
  } );
550
563
 
@@ -553,7 +566,7 @@ describe( 'renderToCanvas — export matrix verification', () => {
553
566
 
554
567
  function setupMockCanvas() {
555
568
  mockCtx = createMockContext();
556
- jest.spyOn( document, 'createElement' ).mockImplementation(
569
+ vi.spyOn( document, 'createElement' ).mockImplementation(
557
570
  ( tag: string ) => {
558
571
  if ( tag === 'canvas' ) {
559
572
  return createMockCanvas(
@@ -566,7 +579,7 @@ describe( 'renderToCanvas — export matrix verification', () => {
566
579
  }
567
580
 
568
581
  afterEach( () => {
569
- jest.restoreAllMocks();
582
+ vi.restoreAllMocks();
570
583
  } );
571
584
 
572
585
  it( 'identity state (no rotation, no zoom, full crop) should produce a 1:1 mapping', () => {
@@ -652,7 +665,7 @@ describe( 'renderToCanvas — export matrix verification', () => {
652
665
  const [ a1, , , d1 ] = mockCtx.setTransform.mock.calls[ 0 ];
653
666
 
654
667
  // Reset for zoom=2.
655
- jest.restoreAllMocks();
668
+ vi.restoreAllMocks();
656
669
  setupMockCanvas();
657
670
  const stateZ2 = createTestState( {
658
671
  rotation: 0,
@@ -688,7 +701,7 @@ describe( 'renderToCanvas — export matrix verification', () => {
688
701
  const [ aNoFlip ] = mockCtx.setTransform.mock.calls[ 0 ];
689
702
 
690
703
  // Reset for horizontal flip.
691
- jest.restoreAllMocks();
704
+ vi.restoreAllMocks();
692
705
  setupMockCanvas();
693
706
  const stateFlip = createTestState( {
694
707
  rotation: 0,
@@ -716,7 +729,7 @@ describe( 'applyToCanvas — export matrix verification', () => {
716
729
  mockCtx = createMockContext();
717
730
  canvasWidths = [];
718
731
  canvasHeights = [];
719
- jest.spyOn( document, 'createElement' ).mockImplementation(
732
+ vi.spyOn( document, 'createElement' ).mockImplementation(
720
733
  ( tag: string ) => {
721
734
  if ( tag === 'canvas' ) {
722
735
  const canvas = createMockCanvas( mockCtx );
@@ -747,7 +760,7 @@ describe( 'applyToCanvas — export matrix verification', () => {
747
760
  }
748
761
 
749
762
  afterEach( () => {
750
- jest.restoreAllMocks();
763
+ vi.restoreAllMocks();
751
764
  } );
752
765
 
753
766
  it( 'should create a canvas with correct dimensions and call setTransform', () => {
@@ -956,5 +956,13 @@ export class InteractionController {
956
956
  this.drag = null;
957
957
  this.touch = null;
958
958
  this.lastTap = null;
959
+ // Reset the gesture bookkeeping too. The timers above are cancelled
960
+ // rather than run, so these flags would otherwise stay set: a stale
961
+ // `wheelGestureActive` suppresses the next wheel gesture's start,
962
+ // and `setStatus` dedupes against `isDragging` / `isZooming`, so a
963
+ // stale value swallows the next real change.
964
+ this.wheelGestureActive = false;
965
+ this.isDragging = false;
966
+ this.isZooming = false;
959
967
  }
960
968
  }
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest';
1
2
  import {
2
3
  isValidSize,
3
4
  safeBoundedNumber,
@@ -1,3 +1,5 @@
1
+ import { mat2d as m2d, vec2 } from 'gl-matrix';
2
+ import { describe, expect, it } from 'vitest';
1
3
  import {
2
4
  createCamera,
3
5
  worldToScreen,
@@ -302,7 +304,6 @@ describe( 'createExportCamera', () => {
302
304
  const state = makeState();
303
305
  const outputSize = { width: 400, height: 225 };
304
306
  const camera = createExportCamera( state, IMAGE, outputSize );
305
- const { vec2 } = require( 'gl-matrix' );
306
307
  const out = vec2.create();
307
308
  vec2.transformMat2d(
308
309
  out,
@@ -317,7 +318,6 @@ describe( 'createExportCamera', () => {
317
318
  const state = makeState();
318
319
  const outputSize = { width: IMAGE.width, height: IMAGE.height };
319
320
  const camera = createExportCamera( state, IMAGE, outputSize );
320
- const { vec2 } = require( 'gl-matrix' );
321
321
  const topLeft = vec2.create();
322
322
  vec2.transformMat2d( topLeft, [ 0, 0 ], camera );
323
323
  expect( topLeft[ 0 ] ).toBeCloseTo( 0, 0 );
@@ -381,7 +381,6 @@ describe( 'createExportCamera', () => {
381
381
  // output canvas.
382
382
  const outputSize = { width: 400, height: 300 };
383
383
  const exportCamera = createExportCamera( state, IMAGE, outputSize );
384
- const { mat2d: m2d, vec2 } = require( 'gl-matrix' );
385
384
  const inv = m2d.create();
386
385
  m2d.invert( inv, exportCamera );
387
386
  const exportPxVec = vec2.create();
@@ -1,3 +1,13 @@
1
+ import {
2
+ afterAll,
3
+ beforeAll,
4
+ beforeEach,
5
+ describe,
6
+ expect,
7
+ it,
8
+ vi,
9
+ type Mocked,
10
+ } from 'vitest';
1
11
  import {
2
12
  InteractionController,
3
13
  type CropperInteractionActions,
@@ -52,7 +62,7 @@ function createPointerEvent(
52
62
  clientX: 0,
53
63
  clientY: 0,
54
64
  pointerId: 1,
55
- preventDefault: jest.fn(),
65
+ preventDefault: vi.fn(),
56
66
  ...overrides,
57
67
  } as unknown as PointerEvent;
58
68
  }
@@ -67,16 +77,16 @@ function createMockElement(): HTMLElement & {
67
77
  } {
68
78
  const listeners: Record< string, EventListener[] > = {};
69
79
  return {
70
- focus: jest.fn(),
71
- setPointerCapture: jest.fn(),
72
- releasePointerCapture: jest.fn(),
73
- addEventListener: jest.fn( ( type: string, fn: EventListener ) => {
80
+ focus: vi.fn(),
81
+ setPointerCapture: vi.fn(),
82
+ releasePointerCapture: vi.fn(),
83
+ addEventListener: vi.fn( ( type: string, fn: EventListener ) => {
74
84
  if ( ! listeners[ type ] ) {
75
85
  listeners[ type ] = [];
76
86
  }
77
87
  listeners[ type ].push( fn );
78
88
  } ),
79
- removeEventListener: jest.fn( ( type: string, fn: EventListener ) => {
89
+ removeEventListener: vi.fn( ( type: string, fn: EventListener ) => {
80
90
  if ( listeners[ type ] ) {
81
91
  listeners[ type ] = listeners[ type ].filter(
82
92
  ( l ) => l !== fn
@@ -106,7 +116,7 @@ function createWheelEvent(
106
116
  overrides: Partial< WheelEvent > & { currentTarget?: unknown } = {}
107
117
  ): WheelEvent {
108
118
  return {
109
- preventDefault: jest.fn(),
119
+ preventDefault: vi.fn(),
110
120
  deltaY: 0,
111
121
  clientX: 0,
112
122
  clientY: 0,
@@ -127,7 +137,7 @@ function createKeyboardEvent(
127
137
  ): KeyboardEvent {
128
138
  return {
129
139
  key,
130
- preventDefault: jest.fn(),
140
+ preventDefault: vi.fn(),
131
141
  ...overrides,
132
142
  } as unknown as KeyboardEvent;
133
143
  }
@@ -144,7 +154,7 @@ function createTouchEvent(
144
154
  overrides: Partial< TouchEvent > = {}
145
155
  ): TouchEvent {
146
156
  return {
147
- preventDefault: jest.fn(),
157
+ preventDefault: vi.fn(),
148
158
  touches,
149
159
  ...overrides,
150
160
  } as unknown as TouchEvent;
@@ -166,7 +176,7 @@ function createContainerRect( overrides: Partial< DOMRect > = {} ): DOMRect {
166
176
  bottom: 300,
167
177
  x: 0,
168
178
  y: 0,
169
- toJSON: jest.fn(),
179
+ toJSON: vi.fn(),
170
180
  ...overrides,
171
181
  } as DOMRect;
172
182
  }
@@ -182,13 +192,13 @@ function createMockDocument(): Document & {
182
192
  } {
183
193
  const listeners: Record< string, EventListener[] > = {};
184
194
  return {
185
- addEventListener: jest.fn( ( type: string, fn: EventListener ) => {
195
+ addEventListener: vi.fn( ( type: string, fn: EventListener ) => {
186
196
  if ( ! listeners[ type ] ) {
187
197
  listeners[ type ] = [];
188
198
  }
189
199
  listeners[ type ].push( fn );
190
200
  } ),
191
- removeEventListener: jest.fn( ( type: string, fn: EventListener ) => {
201
+ removeEventListener: vi.fn( ( type: string, fn: EventListener ) => {
192
202
  if ( listeners[ type ] ) {
193
203
  listeners[ type ] = listeners[ type ].filter(
194
204
  ( l ) => l !== fn
@@ -208,7 +218,7 @@ function createMockDocument(): Document & {
208
218
  describe( 'InteractionController', () => {
209
219
  const containerSize: Size = { width: 500, height: 300 };
210
220
  const imageSize: Size = { width: 500, height: 300 };
211
- let actionMocks: jest.Mocked< CropperInteractionActions >;
221
+ let actionMocks: Mocked< CropperInteractionActions >;
212
222
 
213
223
  // Store original requestAnimationFrame so we can restore it.
214
224
  const originalRAF = globalThis.requestAnimationFrame;
@@ -220,7 +230,7 @@ describe( 'InteractionController', () => {
220
230
  cb( 0 );
221
231
  return 0;
222
232
  };
223
- globalThis.cancelAnimationFrame = jest.fn();
233
+ globalThis.cancelAnimationFrame = vi.fn();
224
234
  } );
225
235
 
226
236
  afterAll( () => {
@@ -230,11 +240,11 @@ describe( 'InteractionController', () => {
230
240
 
231
241
  beforeEach( () => {
232
242
  actionMocks = {
233
- setPan: jest.fn(),
234
- setZoom: jest.fn(),
235
- setZoomAtPoint: jest.fn(),
236
- snapRotate90: jest.fn(),
237
- toggleFlip: jest.fn(),
243
+ setPan: vi.fn(),
244
+ setZoom: vi.fn(),
245
+ setZoomAtPoint: vi.fn(),
246
+ snapRotate90: vi.fn(),
247
+ toggleFlip: vi.fn(),
238
248
  };
239
249
  } );
240
250
 
@@ -290,8 +300,8 @@ describe( 'InteractionController', () => {
290
300
 
291
301
  it( 'ignores touch pointerdown so touch gestures own touch input', () => {
292
302
  const state = makeState( { zoom: 2 } );
293
- const onGestureStart = jest.fn();
294
- const onStatusChange = jest.fn();
303
+ const onGestureStart = vi.fn();
304
+ const onStatusChange = vi.fn();
295
305
  const { controller } = createController( state, {
296
306
  onGestureStart,
297
307
  onStatusChange,
@@ -333,7 +343,7 @@ describe( 'InteractionController', () => {
333
343
  // Simulate pointerup.
334
344
  el._fire( 'pointerup', createPointerEvent() );
335
345
 
336
- jest.clearAllMocks();
346
+ vi.clearAllMocks();
337
347
 
338
348
  // Another pointermove should not dispatch because the listener
339
349
  // was removed after pointerup.
@@ -350,8 +360,8 @@ describe( 'InteractionController', () => {
350
360
 
351
361
  it( 'calls onGestureStart on pointerdown and onGestureEnd on pointerup', () => {
352
362
  const state = makeState( { zoom: 2 } );
353
- const onGestureStart = jest.fn();
354
- const onGestureEnd = jest.fn();
363
+ const onGestureStart = vi.fn();
364
+ const onGestureEnd = vi.fn();
355
365
  const { controller } = createController( state, {
356
366
  onGestureStart,
357
367
  onGestureEnd,
@@ -380,7 +390,7 @@ describe( 'InteractionController', () => {
380
390
 
381
391
  it( 'reports isDragging via onStatusChange', () => {
382
392
  const state = makeState( { zoom: 2 } );
383
- const onStatusChange = jest.fn();
393
+ const onStatusChange = vi.fn();
384
394
  const { controller } = createController( state, {
385
395
  onStatusChange,
386
396
  } );
@@ -505,7 +515,7 @@ describe( 'InteractionController', () => {
505
515
  el
506
516
  );
507
517
 
508
- jest.clearAllMocks();
518
+ vi.clearAllMocks();
509
519
 
510
520
  const wheelEvent = createWheelEvent( {
511
521
  deltaY: -100,
@@ -521,12 +531,12 @@ describe( 'InteractionController', () => {
521
531
  } );
522
532
 
523
533
  it( 'calls onGestureStart on first wheel, onGestureEnd after debounce', () => {
524
- jest.useFakeTimers( {
525
- doNotFake: [ 'requestAnimationFrame', 'cancelAnimationFrame' ],
534
+ vi.useFakeTimers( {
535
+ toFake: [ 'setTimeout', 'clearTimeout' ],
526
536
  } );
527
537
  const state = makeState( { zoom: 2 } );
528
- const onGestureStart = jest.fn();
529
- const onGestureEnd = jest.fn();
538
+ const onGestureStart = vi.fn();
539
+ const onGestureEnd = vi.fn();
530
540
  const { controller } = createController( state, {
531
541
  onGestureStart,
532
542
  onGestureEnd,
@@ -546,11 +556,11 @@ describe( 'InteractionController', () => {
546
556
  expect( onGestureStart ).toHaveBeenCalledTimes( 1 );
547
557
 
548
558
  // Advance past the 300ms debounce.
549
- jest.advanceTimersByTime( 350 );
559
+ vi.advanceTimersByTime( 350 );
550
560
 
551
561
  expect( onGestureEnd ).toHaveBeenCalledTimes( 1 );
552
562
 
553
- jest.useRealTimers();
563
+ vi.useRealTimers();
554
564
  } );
555
565
  } );
556
566
 
@@ -848,8 +858,8 @@ describe( 'InteractionController', () => {
848
858
 
849
859
  it( 'calls onGestureStart/onGestureEnd for single-finger pan', () => {
850
860
  const state = makeState( { zoom: 2 } );
851
- const onGestureStart = jest.fn();
852
- const onGestureEnd = jest.fn();
861
+ const onGestureStart = vi.fn();
862
+ const onGestureEnd = vi.fn();
853
863
  const { controller } = createController( state, {
854
864
  onGestureStart,
855
865
  onGestureEnd,
@@ -873,7 +883,7 @@ describe( 'InteractionController', () => {
873
883
 
874
884
  it( 'reports isDragging on first single-finger move', () => {
875
885
  const state = makeState( { zoom: 2 } );
876
- const onStatusChange = jest.fn();
886
+ const onStatusChange = vi.fn();
877
887
  const { controller } = createController( state, {
878
888
  onStatusChange,
879
889
  } );
@@ -987,8 +997,8 @@ describe( 'InteractionController', () => {
987
997
 
988
998
  it( 'calls onGestureStart for pinch, onGestureEnd on touchend', () => {
989
999
  const state = makeState( { zoom: 1 } );
990
- const onGestureStart = jest.fn();
991
- const onGestureEnd = jest.fn();
1000
+ const onGestureStart = vi.fn();
1001
+ const onGestureEnd = vi.fn();
992
1002
  const { controller } = createController( state, {
993
1003
  onGestureStart,
994
1004
  onGestureEnd,
@@ -1031,7 +1041,7 @@ describe( 'InteractionController', () => {
1031
1041
  createTouchEvent( [ { clientX: 210, clientY: 155 } ] )
1032
1042
  );
1033
1043
  expect( actionMocks.setPan ).toHaveBeenCalled();
1034
- jest.clearAllMocks();
1044
+ vi.clearAllMocks();
1035
1045
 
1036
1046
  // Second finger arrives via touchstart.
1037
1047
  controller.handleTouchStart(
@@ -1122,7 +1132,7 @@ describe( 'InteractionController', () => {
1122
1132
  ] )
1123
1133
  );
1124
1134
 
1125
- jest.clearAllMocks();
1135
+ vi.clearAllMocks();
1126
1136
 
1127
1137
  // One finger lifts — move with 1 touch should NOT pan
1128
1138
  // because didPinch is true.
@@ -1225,7 +1235,7 @@ describe( 'InteractionController', () => {
1225
1235
  createPointerEvent( { clientX: 100, clientY: 100 } ),
1226
1236
  el
1227
1237
  );
1228
- jest.clearAllMocks();
1238
+ vi.clearAllMocks();
1229
1239
 
1230
1240
  controller.destroy();
1231
1241