@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.
- package/CHANGELOG.md +1 -1
- package/build/components/media-editor/index.cjs +43 -11
- package/build/components/media-editor/index.cjs.map +2 -2
- package/build/components/media-editor-canvas/index.cjs +4 -2
- package/build/components/media-editor-canvas/index.cjs.map +2 -2
- package/build/components/media-editor-crop-panel/index.cjs +4 -2
- package/build/components/media-editor-crop-panel/index.cjs.map +2 -2
- package/build/components/media-editor-fine-rotation/index.cjs +4 -2
- package/build/components/media-editor-fine-rotation/index.cjs.map +2 -2
- package/build/components/media-editor-image-controls/index.cjs +17 -4
- package/build/components/media-editor-image-controls/index.cjs.map +2 -2
- package/build/components/rotation-ruler/index.cjs +1 -0
- package/build/components/rotation-ruler/index.cjs.map +2 -2
- package/build/components/rotation-ruler/use-ruler-drag.cjs +7 -2
- package/build/components/rotation-ruler/use-ruler-drag.cjs.map +2 -2
- package/build/image-editor/core/interaction-controller.cjs +3 -0
- package/build/image-editor/core/interaction-controller.cjs.map +2 -2
- package/build/image-editor/react/components/cropper.cjs +5 -3
- package/build/image-editor/react/components/cropper.cjs.map +2 -2
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +12 -5
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-interaction.cjs +31 -0
- package/build/image-editor/react/hooks/use-interaction.cjs.map +2 -2
- package/build-module/components/media-editor/index.mjs +43 -11
- package/build-module/components/media-editor/index.mjs.map +2 -2
- package/build-module/components/media-editor-canvas/index.mjs +4 -2
- package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
- package/build-module/components/media-editor-crop-panel/index.mjs +4 -2
- package/build-module/components/media-editor-crop-panel/index.mjs.map +2 -2
- package/build-module/components/media-editor-fine-rotation/index.mjs +4 -2
- package/build-module/components/media-editor-fine-rotation/index.mjs.map +2 -2
- package/build-module/components/media-editor-image-controls/index.mjs +17 -4
- package/build-module/components/media-editor-image-controls/index.mjs.map +2 -2
- package/build-module/components/rotation-ruler/index.mjs +1 -0
- package/build-module/components/rotation-ruler/index.mjs.map +2 -2
- package/build-module/components/rotation-ruler/use-ruler-drag.mjs +7 -2
- package/build-module/components/rotation-ruler/use-ruler-drag.mjs.map +2 -2
- package/build-module/image-editor/core/interaction-controller.mjs +3 -0
- package/build-module/image-editor/core/interaction-controller.mjs.map +2 -2
- package/build-module/image-editor/react/components/cropper.mjs +5 -3
- package/build-module/image-editor/react/components/cropper.mjs.map +2 -2
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +12 -5
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/use-interaction.mjs +31 -0
- package/build-module/image-editor/react/hooks/use-interaction.mjs.map +2 -2
- package/build-types/components/media-editor/index.d.ts.map +1 -1
- package/build-types/components/media-editor-canvas/index.d.ts +7 -1
- package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
- package/build-types/components/media-editor-crop-panel/index.d.ts +4 -1
- package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
- package/build-types/components/media-editor-fine-rotation/index.d.ts +7 -1
- package/build-types/components/media-editor-fine-rotation/index.d.ts.map +1 -1
- package/build-types/components/media-editor-image-controls/index.d.ts +7 -1
- package/build-types/components/media-editor-image-controls/index.d.ts.map +1 -1
- package/build-types/components/rotation-ruler/index.d.ts.map +1 -1
- package/build-types/components/rotation-ruler/use-ruler-drag.d.ts.map +1 -1
- package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -1
- package/build-types/image-editor/react/components/cropper.d.ts +7 -0
- package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/use-interaction.d.ts +5 -0
- package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +1 -1
- package/global.d.ts +1 -0
- package/package.json +22 -22
- package/src/components/media-editor/index.tsx +42 -5
- package/src/components/media-editor/test/use-crop-options.jsdom.test.tsx +1 -0
- package/src/components/media-editor-canvas/index.tsx +8 -0
- package/src/components/media-editor-crop-panel/index.tsx +6 -1
- package/src/components/media-editor-crop-panel/test/index.jsdom.test.tsx +5 -2
- package/src/components/media-editor-fine-rotation/index.tsx +8 -0
- package/src/components/media-editor-image-controls/index.tsx +22 -3
- package/src/components/media-editor-image-controls/test/index.jsdom.test.tsx +37 -0
- package/src/components/media-editor-modal/test/index.jsdom.test.tsx +65 -35
- package/src/components/rotation-ruler/index.tsx +1 -0
- package/src/components/rotation-ruler/test/index.jsdom.test.tsx +42 -4
- package/src/components/rotation-ruler/use-ruler-drag.ts +10 -2
- package/src/image-editor/core/export/test/canvas-renderer.jsdom.test.ts +76 -63
- package/src/image-editor/core/interaction-controller.ts +8 -0
- package/src/image-editor/react/components/cropper.tsx +12 -1
- package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +22 -6
- package/src/image-editor/react/components/stencils/test/rectangle-stencil.jsdom.test.tsx +54 -36
- package/src/image-editor/react/components/test/cropper.jsdom.test.tsx +304 -0
- package/src/image-editor/react/hooks/test/use-aria-announcer.jsdom.test.ts +57 -44
- package/src/image-editor/react/hooks/test/use-cropper-reducer.jsdom.test.ts +1 -0
- package/src/image-editor/react/hooks/test/use-interaction.jsdom.test.ts +17 -16
- package/src/image-editor/react/hooks/test/use-transform-style.jsdom.test.ts +1 -0
- package/src/image-editor/react/hooks/use-interaction.ts +47 -0
- package/src/state/test/use-media-editor-state.jsdom.test.ts +1 -0
- package/tsconfig.build.tsbuildinfo +1 -1
- 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 =
|
|
16
|
-
const mockOnClose =
|
|
17
|
-
const mockCloseMediaEditorModal =
|
|
18
|
-
const mockCreateSuccessNotice =
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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
|
|
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 {
|
|
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 =
|
|
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 =
|
|
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 =
|
|
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:
|
|
18
|
-
translate:
|
|
19
|
-
rotate:
|
|
20
|
-
scale:
|
|
21
|
-
drawImage:
|
|
22
|
-
getImageData:
|
|
23
|
-
putImageData:
|
|
24
|
-
beginPath:
|
|
25
|
-
closePath:
|
|
26
|
-
moveTo:
|
|
27
|
-
lineTo:
|
|
28
|
-
clip:
|
|
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:
|
|
42
|
-
toBlob:
|
|
43
|
-
toDataURL:
|
|
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:
|
|
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 =
|
|
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:
|
|
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 =
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
215
|
-
|
|
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
|
-
|
|
256
|
-
|
|
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:
|
|
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:
|
|
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:
|
|
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:
|
|
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:
|
|
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:
|
|
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 =
|
|
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
|
-
|
|
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 =
|
|
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
|
-
|
|
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:
|
|
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 =
|
|
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 =
|
|
477
|
+
global.Image = vi.fn( function Image() {
|
|
463
478
|
const img: {
|
|
464
|
-
addEventListener:
|
|
479
|
+
addEventListener: Mock;
|
|
465
480
|
crossOrigin: string;
|
|
466
481
|
src: string;
|
|
467
482
|
naturalWidth: number;
|
|
468
483
|
naturalHeight: number;
|
|
469
484
|
} = {
|
|
470
|
-
addEventListener:
|
|
471
|
-
( event
|
|
472
|
-
|
|
473
|
-
|
|
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:
|
|
491
|
-
setTransform:
|
|
492
|
-
drawImage:
|
|
503
|
+
getContext: vi.fn( () => ( {
|
|
504
|
+
setTransform: vi.fn(),
|
|
505
|
+
drawImage: vi.fn(),
|
|
493
506
|
} ) ),
|
|
494
|
-
toBlob:
|
|
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
|
-
|
|
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
|
-
|
|
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:
|
|
539
|
+
getContext: vi.fn( () => null ),
|
|
527
540
|
};
|
|
528
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
763
|
+
vi.restoreAllMocks();
|
|
751
764
|
} );
|
|
752
765
|
|
|
753
766
|
it( 'should create a canvas with correct dimensions and call setTransform', () => {
|