@wordpress/media-editor 0.17.2-next.v.202609031004.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 (90) 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-types/components/media-editor/index.d.ts.map +1 -1
  47. package/build-types/components/media-editor-canvas/index.d.ts +7 -1
  48. package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
  49. package/build-types/components/media-editor-crop-panel/index.d.ts +4 -1
  50. package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
  51. package/build-types/components/media-editor-fine-rotation/index.d.ts +7 -1
  52. package/build-types/components/media-editor-fine-rotation/index.d.ts.map +1 -1
  53. package/build-types/components/media-editor-image-controls/index.d.ts +7 -1
  54. package/build-types/components/media-editor-image-controls/index.d.ts.map +1 -1
  55. package/build-types/components/rotation-ruler/index.d.ts.map +1 -1
  56. package/build-types/components/rotation-ruler/use-ruler-drag.d.ts.map +1 -1
  57. package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -1
  58. package/build-types/image-editor/react/components/cropper.d.ts +7 -0
  59. package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
  60. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -1
  61. package/build-types/image-editor/react/hooks/use-interaction.d.ts +5 -0
  62. package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +1 -1
  63. package/global.d.ts +1 -0
  64. package/package.json +22 -22
  65. package/src/components/media-editor/index.tsx +42 -5
  66. package/src/components/media-editor/test/use-crop-options.jsdom.test.tsx +1 -0
  67. package/src/components/media-editor-canvas/index.tsx +8 -0
  68. package/src/components/media-editor-crop-panel/index.tsx +6 -1
  69. package/src/components/media-editor-crop-panel/test/index.jsdom.test.tsx +5 -2
  70. package/src/components/media-editor-fine-rotation/index.tsx +8 -0
  71. package/src/components/media-editor-image-controls/index.tsx +22 -3
  72. package/src/components/media-editor-image-controls/test/index.jsdom.test.tsx +37 -0
  73. package/src/components/media-editor-modal/test/index.jsdom.test.tsx +65 -35
  74. package/src/components/rotation-ruler/index.tsx +1 -0
  75. package/src/components/rotation-ruler/test/index.jsdom.test.tsx +42 -4
  76. package/src/components/rotation-ruler/use-ruler-drag.ts +10 -2
  77. package/src/image-editor/core/export/test/canvas-renderer.jsdom.test.ts +76 -63
  78. package/src/image-editor/core/interaction-controller.ts +8 -0
  79. package/src/image-editor/react/components/cropper.tsx +12 -1
  80. package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +22 -6
  81. package/src/image-editor/react/components/stencils/test/rectangle-stencil.jsdom.test.tsx +54 -36
  82. package/src/image-editor/react/components/test/cropper.jsdom.test.tsx +304 -0
  83. package/src/image-editor/react/hooks/test/use-aria-announcer.jsdom.test.ts +57 -44
  84. package/src/image-editor/react/hooks/test/use-cropper-reducer.jsdom.test.ts +1 -0
  85. package/src/image-editor/react/hooks/test/use-interaction.jsdom.test.ts +17 -16
  86. package/src/image-editor/react/hooks/test/use-transform-style.jsdom.test.ts +1 -0
  87. package/src/image-editor/react/hooks/use-interaction.ts +47 -0
  88. package/src/state/test/use-media-editor-state.jsdom.test.ts +1 -0
  89. package/tsconfig.build.tsbuildinfo +1 -1
  90. package/tsconfig.json +6 -1
@@ -1,4 +1,6 @@
1
+ import { describe, expect, it } from 'vitest';
1
2
  import { fireEvent, render, screen, waitFor } from '@testing-library/react';
3
+ import { useState } from '@wordpress/element';
2
4
  import MediaEditorImageControls from '..';
3
5
  import type { MediaEditorImageControlsProps } from '..';
4
6
  import { MediaEditorStateProvider, useMediaEditor } from '../../../state';
@@ -130,6 +132,41 @@ describe( 'MediaEditorImageControls', () => {
130
132
  );
131
133
  } );
132
134
 
135
+ it( 'ignores an aspect ratio chosen from a menu left open when disabled', async () => {
136
+ function Harness() {
137
+ const [ locked, setLocked ] = useState( false );
138
+ return (
139
+ <MediaEditorStateProvider>
140
+ <MediaEditorImageControls
141
+ showAspectRatioControl
142
+ disabled={ locked }
143
+ />
144
+ <button onClick={ () => setLocked( true ) }>lock</button>
145
+ <CurrentState />
146
+ </MediaEditorStateProvider>
147
+ );
148
+ }
149
+ render( <Harness /> );
150
+
151
+ fireEvent.click(
152
+ screen.getByRole( 'button', { name: 'Aspect ratio' } )
153
+ );
154
+ const before = screen.getByTestId( 'current-aspect-ratio' ).textContent;
155
+
156
+ // The popover stays mounted when the toggle becomes disabled, so
157
+ // the items have to refuse the change themselves.
158
+ fireEvent.click( screen.getByRole( 'button', { name: 'lock' } ) );
159
+ fireEvent.click(
160
+ screen.getByRole( 'menuitemradio', { name: 'Square (1:1)' } )
161
+ );
162
+
163
+ await waitFor( () =>
164
+ expect(
165
+ screen.getByTestId( 'current-aspect-ratio' )
166
+ ).toHaveTextContent( before ?? '' )
167
+ );
168
+ } );
169
+
133
170
  it( 'omits the aspect ratio dropdown from the labelled panel layout', () => {
134
171
  setup( {
135
172
  withLabels: true,
@@ -1,9 +1,16 @@
1
+ import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest';
1
2
  import { fireEvent, render, screen } from '@testing-library/react';
3
+ import type { ReactNode } from 'react';
2
4
  import { useDispatch, useSelect } from '@wordpress/data';
3
5
  import { store as noticesStore } from '@wordpress/notices';
4
6
  import { MediaEditorModal } from '../index';
5
7
 
6
- let mockSaveResult = {
8
+ let mockSaveResult: {
9
+ id: number;
10
+ url: string;
11
+ media: { id: number; source_url: string };
12
+ previous?: { id: number; url: string };
13
+ } = {
7
14
  id: 11,
8
15
  url: 'edited.jpg',
9
16
  media: { id: 11, source_url: 'edited.jpg' },
@@ -12,50 +19,73 @@ let mockSaveResult = {
12
19
  url: 'original.jpg',
13
20
  },
14
21
  };
15
- const mockOnUpdate = jest.fn();
16
- const mockOnClose = jest.fn();
17
- const mockCloseMediaEditorModal = jest.fn();
18
- const mockCreateSuccessNotice = jest.fn();
19
-
20
- jest.mock( '@wordpress/data', () => ( {
21
- useDispatch: jest.fn(),
22
- useSelect: jest.fn(),
23
- } ) );
24
-
25
- jest.mock( '@wordpress/components', () => ( {
26
- Modal: ( { children } ) => children,
27
- } ) );
28
-
29
- jest.mock( '@wordpress/keyboard-shortcuts', () => ( {
30
- ShortcutProvider: ( { children } ) => children,
31
- } ) );
32
-
33
- jest.mock( '@wordpress/notices', () => ( {
34
- store: { name: 'notices' },
35
- } ) );
36
-
37
- jest.mock( '../../../store', () => ( {
38
- store: { name: 'media-editor' },
39
- } ) );
40
-
41
- jest.mock( '../../media-editor', () => {
42
- const { createElement } = jest.requireActual( '@wordpress/element' );
22
+ const mockOnUpdate = vi.fn();
23
+ const mockOnClose = vi.fn();
24
+ const mockCloseMediaEditorModal = vi.fn();
25
+ const mockCreateSuccessNotice = vi.fn();
26
+
27
+ vi.mock(
28
+ import( '@wordpress/data' ),
29
+ () =>
30
+ ( {
31
+ useDispatch: vi.fn(),
32
+ useSelect: vi.fn(),
33
+ } ) as unknown as typeof import('@wordpress/data')
34
+ );
35
+
36
+ vi.mock(
37
+ import( '@wordpress/components' ),
38
+ () =>
39
+ ( {
40
+ Modal: ( { children }: { children: ReactNode } ) => children,
41
+ } ) as unknown as typeof import('@wordpress/components')
42
+ );
43
+
44
+ vi.mock(
45
+ import( '@wordpress/keyboard-shortcuts' ),
46
+ () =>
47
+ ( {
48
+ ShortcutProvider: ( { children }: { children: ReactNode } ) =>
49
+ children,
50
+ } ) as unknown as typeof import('@wordpress/keyboard-shortcuts')
51
+ );
52
+
53
+ vi.mock(
54
+ import( '@wordpress/notices' ),
55
+ () =>
56
+ ( {
57
+ store: { name: 'notices' },
58
+ } ) as unknown as typeof import('@wordpress/notices')
59
+ );
60
+
61
+ vi.mock(
62
+ import( '../../../store' ),
63
+ () =>
64
+ ( {
65
+ store: { name: 'media-editor' },
66
+ } ) as unknown as typeof import('../../../store')
67
+ );
68
+
69
+ vi.mock( import( '../../media-editor' ), async () => {
70
+ const { createElement } =
71
+ await vi.importActual< typeof import('@wordpress/element') >(
72
+ '@wordpress/element'
73
+ );
43
74
 
44
75
  return {
45
- __esModule: true,
46
- default: jest.fn( ( props ) =>
76
+ default: vi.fn( ( props ) =>
47
77
  createElement(
48
78
  'button',
49
79
  { onClick: () => props.onSaved( mockSaveResult ) },
50
80
  'Save result'
51
81
  )
52
82
  ),
53
- };
83
+ } as unknown as typeof import('../../media-editor');
54
84
  } );
55
85
 
56
86
  describe( 'MediaEditorModal', () => {
57
87
  beforeEach( () => {
58
- jest.clearAllMocks();
88
+ vi.clearAllMocks();
59
89
  mockSaveResult = {
60
90
  id: 11,
61
91
  url: 'edited.jpg',
@@ -66,7 +96,7 @@ describe( 'MediaEditorModal', () => {
66
96
  },
67
97
  };
68
98
 
69
- ( useSelect as jest.Mock ).mockImplementation( ( mapSelect ) =>
99
+ ( useSelect as Mock ).mockImplementation( ( mapSelect ) =>
70
100
  mapSelect( () => ( {
71
101
  isOpen: () => true,
72
102
  getId: () => 10,
@@ -74,7 +104,7 @@ describe( 'MediaEditorModal', () => {
74
104
  getOnClose: () => mockOnClose,
75
105
  } ) )
76
106
  );
77
- ( useDispatch as jest.Mock ).mockImplementation( ( store ) =>
107
+ ( useDispatch as Mock ).mockImplementation( ( store ) =>
78
108
  store === noticesStore
79
109
  ? { createSuccessNotice: mockCreateSuccessNotice }
80
110
  : { closeMediaEditorModal: mockCloseMediaEditorModal }
@@ -194,6 +194,7 @@ export default function RotationRuler( props: RotationRulerProps ) {
194
194
  <div
195
195
  className={ clsx( 'rotation-ruler', className ) }
196
196
  role="presentation"
197
+ data-testid="rotation-ruler"
197
198
  data-disabled={ disabled || undefined }
198
199
  { ...dragHandlers }
199
200
  >
@@ -1,8 +1,11 @@
1
- import { render, screen } from '@testing-library/react';
1
+ import { describe, expect, it, vi } from 'vitest';
2
+ import { fireEvent, render, screen } from '@testing-library/react';
2
3
  import userEvent from '@testing-library/user-event';
3
4
  import { pxToValueDelta, clampValue, quantize } from '../use-ruler-drag';
4
5
  import RotationRuler from '../index';
5
6
 
7
+ globalThis.wpVitest.mockPointerEvent();
8
+
6
9
  describe( 'rotation-ruler math', () => {
7
10
  describe( 'pxToValueDelta', () => {
8
11
  it( 'converts pointer delta to a value delta using pixelsPerStep × step', () => {
@@ -53,7 +56,7 @@ describe( 'RotationRuler', () => {
53
56
 
54
57
  it( 'fires onChange with value + step on ArrowRight', async () => {
55
58
  const user = userEvent.setup();
56
- const onChange = jest.fn();
59
+ const onChange = vi.fn();
57
60
  render(
58
61
  <RotationRuler
59
62
  value={ 0 }
@@ -70,7 +73,7 @@ describe( 'RotationRuler', () => {
70
73
 
71
74
  it( 'fires onChange with value + step / 2 on Shift+ArrowRight', async () => {
72
75
  const user = userEvent.setup();
73
- const onChange = jest.fn();
76
+ const onChange = vi.fn();
74
77
  render(
75
78
  <RotationRuler
76
79
  value={ 0 }
@@ -84,9 +87,44 @@ describe( 'RotationRuler', () => {
84
87
  expect( onChange ).toHaveBeenCalledTimes( 1 );
85
88
  } );
86
89
 
90
+ it( 'stops an in-flight drag when it becomes disabled', () => {
91
+ const onChange = vi.fn();
92
+ const props = {
93
+ value: 0,
94
+ onChange,
95
+ label: 'Fine rotation',
96
+ };
97
+ const { rerender } = render( <RotationRuler { ...props } /> );
98
+ const strip = screen.getByTestId( 'rotation-ruler' );
99
+
100
+ if ( ! strip.hasPointerCapture ) {
101
+ strip.hasPointerCapture = () => false;
102
+ }
103
+ if ( ! strip.setPointerCapture ) {
104
+ strip.setPointerCapture = () => {};
105
+ }
106
+ if ( ! strip.releasePointerCapture ) {
107
+ strip.releasePointerCapture = () => {};
108
+ }
109
+
110
+ fireEvent.pointerDown( strip, {
111
+ button: 0,
112
+ clientX: 0,
113
+ pointerId: 1,
114
+ } );
115
+ fireEvent.pointerMove( strip, { clientX: 40, pointerId: 1 } );
116
+ expect( onChange ).toHaveBeenCalled();
117
+
118
+ rerender( <RotationRuler { ...props } disabled /> );
119
+ onChange.mockClear();
120
+
121
+ fireEvent.pointerMove( strip, { clientX: 90, pointerId: 1 } );
122
+ expect( onChange ).not.toHaveBeenCalled();
123
+ } );
124
+
87
125
  it( 'does not fire onChange when disabled', async () => {
88
126
  const user = userEvent.setup();
89
- const onChange = jest.fn();
127
+ const onChange = vi.fn();
90
128
  render(
91
129
  <RotationRuler
92
130
  value={ 0 }
@@ -152,6 +152,14 @@ export function useRulerDrag(
152
152
  // listeners or stale pointer capture.
153
153
  useEffect( () => endDrag, [ endDrag ] );
154
154
 
155
+ // Becoming disabled mid-drag ends it, so a rotation cannot keep
156
+ // changing after a save has read it.
157
+ useEffect( () => {
158
+ if ( disabled ) {
159
+ endDrag();
160
+ }
161
+ }, [ disabled, endDrag ] );
162
+
155
163
  const onPointerDown = useCallback(
156
164
  ( event: React.PointerEvent< HTMLElement > ) => {
157
165
  if ( disabled || event.button !== 0 ) {
@@ -184,7 +192,7 @@ export function useRulerDrag(
184
192
  const onPointerMove = useCallback(
185
193
  ( event: React.PointerEvent< HTMLElement > ) => {
186
194
  const state = latestRef.current;
187
- if ( ! state.dragging ) {
195
+ if ( disabled || ! state.dragging ) {
188
196
  return;
189
197
  }
190
198
  // Shift toggles fine mode mid-drag. The drag math computes
@@ -215,7 +223,7 @@ export function useRulerDrag(
215
223
  onChange( next );
216
224
  }
217
225
  },
218
- [ onChange, min, max, step, pixelsPerStep ]
226
+ [ disabled, onChange, min, max, step, pixelsPerStep ]
219
227
  );
220
228
 
221
229
  return {
@@ -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', () => {