@wordpress/media-editor 0.18.0 → 0.19.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 +7 -0
- package/README.md +7 -7
- package/build/components/media-editor/index.cjs.map +2 -2
- package/build/components/media-editor/use-save-media-editor.cjs.map +2 -2
- package/build/components/media-editor-crop-panel/index.cjs.map +3 -3
- package/build/image-editor/core/interaction-controller.cjs.map +2 -2
- package/build/image-editor/core/setter-helpers.cjs.map +2 -2
- package/build/image-editor/react/components/overlays/dimensions-overlay.cjs.map +2 -2
- package/build-module/components/media-editor/index.mjs.map +2 -2
- package/build-module/components/media-editor/use-save-media-editor.mjs.map +2 -2
- package/build-module/components/media-editor-crop-panel/index.mjs +2 -2
- package/build-module/components/media-editor-crop-panel/index.mjs.map +2 -2
- package/build-module/image-editor/core/interaction-controller.mjs.map +2 -2
- package/build-module/image-editor/core/setter-helpers.mjs.map +2 -2
- package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs.map +2 -2
- package/build-style/style-rtl.css +21 -22
- package/build-style/style.css +6 -0
- package/global.d.ts +0 -1
- package/package.json +23 -24
- package/src/components/media-editor/index.tsx +2 -2
- package/src/components/media-editor/use-save-media-editor.ts +5 -5
- package/src/components/media-editor-crop-panel/index.tsx +2 -2
- package/src/components/media-editor-modal/test/index.jsdom.test.tsx +7 -7
- package/src/components/rotation-ruler/stories/index.story.tsx +2 -2
- package/src/components/rotation-ruler/style.scss +9 -0
- package/src/image-editor/core/interaction-controller.ts +2 -2
- package/src/image-editor/core/setter-helpers.ts +1 -1
- package/src/image-editor/react/components/cropper.scss +6 -0
- package/src/image-editor/react/components/overlays/dimensions-overlay.tsx +1 -1
- package/src/image-editor/react/components/stencils/test/{rectangle-stencil.jsdom.test.tsx → rectangle-stencil.browser.test.tsx} +80 -98
- package/src/image-editor/react/components/test/{cropper.jsdom.test.tsx → cropper.browser.test.tsx} +230 -221
- package/src/image-editor/react/hooks/test/use-aria-announcer.jsdom.test.ts +1 -1
- package/src/image-editor/stories/rectangle-crop.story.tsx +24 -23
- package/src/image-editor/stories/{style.css → style.module.css} +8 -37
- package/tsconfig.build.json +1 -1
- package/tsconfig.build.tsbuildinfo +1 -1
- package/tsconfig.json +1 -2
package/src/image-editor/react/components/test/{cropper.jsdom.test.tsx → cropper.browser.test.tsx}
RENAMED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
screen,
|
|
6
|
-
waitFor,
|
|
7
|
-
} from '@testing-library/react';
|
|
1
|
+
import { afterEach, describe, expect, it, vi, type Mock } from 'vitest';
|
|
2
|
+
import { userEvent } from 'vitest/browser';
|
|
3
|
+
import { act, fireEvent, screen, waitFor } from '@testing-library/react';
|
|
4
|
+
import { render } from 'vitest-browser-react';
|
|
8
5
|
import { Cropper } from '../cropper';
|
|
9
6
|
import type { CropperController } from '../../hooks/use-cropper-reducer';
|
|
10
7
|
import { DEFAULT_STATE } from '../../../core/constants';
|
|
11
8
|
import { getSourceRegion } from '../../../core/source-region';
|
|
9
|
+
// Browser Mode needs the package's real styles for layout and transitions.
|
|
10
|
+
// eslint-disable-next-line @wordpress/no-non-module-stylesheet-imports
|
|
11
|
+
import '../../../style.scss';
|
|
12
12
|
|
|
13
13
|
const GRID_TEST_ID = 'cropper-grid';
|
|
14
14
|
const GRID_INTERACTIVE_CLASS =
|
|
@@ -25,81 +25,56 @@ function createController(): CropperController {
|
|
|
25
25
|
naturalHeight: 400,
|
|
26
26
|
},
|
|
27
27
|
},
|
|
28
|
-
setImage:
|
|
29
|
-
setPan:
|
|
30
|
-
setZoom:
|
|
31
|
-
setZoomAtPoint:
|
|
32
|
-
setRotation:
|
|
33
|
-
setFlip:
|
|
34
|
-
toggleFlip:
|
|
35
|
-
snapRotate90:
|
|
36
|
-
setCropRect:
|
|
37
|
-
settleCrop:
|
|
38
|
-
applyOperation:
|
|
39
|
-
reset:
|
|
28
|
+
setImage: vi.fn(),
|
|
29
|
+
setPan: vi.fn(),
|
|
30
|
+
setZoom: vi.fn(),
|
|
31
|
+
setZoomAtPoint: vi.fn(),
|
|
32
|
+
setRotation: vi.fn(),
|
|
33
|
+
setFlip: vi.fn(),
|
|
34
|
+
toggleFlip: vi.fn(),
|
|
35
|
+
snapRotate90: vi.fn(),
|
|
36
|
+
setCropRect: vi.fn(),
|
|
37
|
+
settleCrop: vi.fn(),
|
|
38
|
+
applyOperation: vi.fn(),
|
|
39
|
+
reset: vi.fn(),
|
|
40
40
|
isDirty: false,
|
|
41
|
-
getCroppedImage:
|
|
42
|
-
setVisualSize:
|
|
43
|
-
adjustCropRectForViewport:
|
|
41
|
+
getCroppedImage: vi.fn(),
|
|
42
|
+
setVisualSize: vi.fn(),
|
|
43
|
+
adjustCropRectForViewport: vi.fn(),
|
|
44
44
|
};
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
beforeAll( () => {
|
|
51
|
-
if ( ! HTMLElement.prototype.setPointerCapture ) {
|
|
52
|
-
HTMLElement.prototype.setPointerCapture = jest.fn();
|
|
53
|
-
}
|
|
54
|
-
if ( ! HTMLElement.prototype.releasePointerCapture ) {
|
|
55
|
-
HTMLElement.prototype.releasePointerCapture = jest.fn();
|
|
56
|
-
}
|
|
57
|
-
if ( typeof ( globalThis as any ).PointerEvent === 'undefined' ) {
|
|
58
|
-
( globalThis as any ).PointerEvent = class PointerEvent extends (
|
|
59
|
-
MouseEvent
|
|
60
|
-
) {
|
|
61
|
-
pointerId: number;
|
|
62
|
-
pointerType: string;
|
|
63
|
-
isPrimary: boolean;
|
|
64
|
-
constructor( type: string, init: PointerEventInit = {} ) {
|
|
65
|
-
super( type, init );
|
|
66
|
-
this.pointerId = init.pointerId ?? 0;
|
|
67
|
-
this.pointerType = init.pointerType ?? '';
|
|
68
|
-
this.isPrimary = init.isPrimary ?? false;
|
|
69
|
-
}
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
globalThis.ResizeObserver = class ResizeObserver {
|
|
74
|
-
private callback: ResizeObserverCallback;
|
|
75
|
-
|
|
76
|
-
constructor( callback: ResizeObserverCallback ) {
|
|
77
|
-
this.callback = callback;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
observe() {
|
|
81
|
-
this.callback(
|
|
82
|
-
[
|
|
83
|
-
{
|
|
84
|
-
contentRect: { width: 600, height: 400 },
|
|
85
|
-
} as ResizeObserverEntry,
|
|
86
|
-
],
|
|
87
|
-
this
|
|
88
|
-
);
|
|
89
|
-
}
|
|
47
|
+
function CropperFixture( { children }: { children: React.ReactNode } ) {
|
|
48
|
+
return <div style={ { width: 648, height: 448 } }>{ children }</div>;
|
|
49
|
+
}
|
|
90
50
|
|
|
91
|
-
|
|
51
|
+
async function renderCropper( cropper: React.ReactElement ) {
|
|
52
|
+
const view = await render( <CropperFixture>{ cropper }</CropperFixture> );
|
|
53
|
+
return {
|
|
54
|
+
...view,
|
|
55
|
+
rerender: async ( nextCropper: React.ReactElement ) =>
|
|
56
|
+
await view.rerender(
|
|
57
|
+
<CropperFixture>{ nextCropper }</CropperFixture>
|
|
58
|
+
),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
92
61
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
62
|
+
function hasSettleTransition( element: HTMLElement ) {
|
|
63
|
+
const style = getComputedStyle( element );
|
|
64
|
+
return (
|
|
65
|
+
style.transitionProperty === 'transform' &&
|
|
66
|
+
style.transitionDuration === '0.2s' &&
|
|
67
|
+
style.transitionTimingFunction === 'ease-out'
|
|
68
|
+
);
|
|
69
|
+
}
|
|
96
70
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
71
|
+
afterEach( () => {
|
|
72
|
+
vi.useRealTimers();
|
|
73
|
+
} );
|
|
100
74
|
|
|
101
|
-
|
|
102
|
-
|
|
75
|
+
describe( 'Cropper', () => {
|
|
76
|
+
it( 'does not render the grid when showGrid is false', async () => {
|
|
77
|
+
await renderCropper(
|
|
103
78
|
<Cropper
|
|
104
79
|
src="test.jpg"
|
|
105
80
|
controller={ createController() }
|
|
@@ -112,7 +87,7 @@ describe( 'Cropper', () => {
|
|
|
112
87
|
} );
|
|
113
88
|
|
|
114
89
|
it( 'renders the grid always visible when showGrid is true', async () => {
|
|
115
|
-
|
|
90
|
+
await renderCropper(
|
|
116
91
|
<Cropper
|
|
117
92
|
src="test.jpg"
|
|
118
93
|
controller={ createController() }
|
|
@@ -128,8 +103,8 @@ describe( 'Cropper', () => {
|
|
|
128
103
|
expect( canvas ).not.toHaveClass( SHOW_GRID_CLASS );
|
|
129
104
|
} );
|
|
130
105
|
|
|
131
|
-
it( 'describes and focuses the crop area when requested', () => {
|
|
132
|
-
|
|
106
|
+
it( 'describes and focuses the crop area when requested', async () => {
|
|
107
|
+
await renderCropper(
|
|
133
108
|
<Cropper
|
|
134
109
|
src="test.jpg"
|
|
135
110
|
controller={ createController() }
|
|
@@ -146,7 +121,7 @@ describe( 'Cropper', () => {
|
|
|
146
121
|
} );
|
|
147
122
|
|
|
148
123
|
it( 'does not expose crop area keyboard hints while resize handles are focused', async () => {
|
|
149
|
-
|
|
124
|
+
await renderCropper(
|
|
150
125
|
<Cropper
|
|
151
126
|
src="test.jpg"
|
|
152
127
|
controller={ createController() }
|
|
@@ -161,8 +136,8 @@ describe( 'Cropper', () => {
|
|
|
161
136
|
name: 'Resize from top-left corner',
|
|
162
137
|
} );
|
|
163
138
|
|
|
164
|
-
|
|
165
|
-
|
|
139
|
+
await userEvent.tab();
|
|
140
|
+
expect( handle ).toHaveFocus();
|
|
166
141
|
|
|
167
142
|
expect( canvas ).not.toHaveAccessibleDescription(
|
|
168
143
|
'When this area is focused, use arrow keys to move the image and plus or minus to zoom. Tab to resize handles and controls.'
|
|
@@ -173,7 +148,7 @@ describe( 'Cropper', () => {
|
|
|
173
148
|
} );
|
|
174
149
|
|
|
175
150
|
it( 'clears the keyboard-active class when a pointer drag starts on a handle', async () => {
|
|
176
|
-
|
|
151
|
+
await renderCropper(
|
|
177
152
|
<Cropper
|
|
178
153
|
src="test.jpg"
|
|
179
154
|
controller={ createController() }
|
|
@@ -195,7 +170,7 @@ describe( 'Cropper', () => {
|
|
|
195
170
|
act( () => {
|
|
196
171
|
handle.focus();
|
|
197
172
|
} );
|
|
198
|
-
|
|
173
|
+
await userEvent.keyboard( '{ArrowRight}' );
|
|
199
174
|
expect( canvas ).toHaveClass( focusVisibleClass );
|
|
200
175
|
|
|
201
176
|
// Switching to the mouse on the same handle must drop the
|
|
@@ -213,7 +188,7 @@ describe( 'Cropper', () => {
|
|
|
213
188
|
} );
|
|
214
189
|
|
|
215
190
|
it( 'returns focus to the crop area on Escape from a resize handle', async () => {
|
|
216
|
-
|
|
191
|
+
await renderCropper(
|
|
217
192
|
<Cropper
|
|
218
193
|
src="test.jpg"
|
|
219
194
|
controller={ createController() }
|
|
@@ -231,13 +206,13 @@ describe( 'Cropper', () => {
|
|
|
231
206
|
act( () => {
|
|
232
207
|
handle.focus();
|
|
233
208
|
} );
|
|
234
|
-
|
|
209
|
+
await userEvent.keyboard( '{Escape}' );
|
|
235
210
|
|
|
236
211
|
expect( canvas ).toHaveFocus();
|
|
237
212
|
} );
|
|
238
213
|
|
|
239
214
|
it( 'renders the grid hidden by default in interactive mode', async () => {
|
|
240
|
-
|
|
215
|
+
await renderCropper(
|
|
241
216
|
<Cropper
|
|
242
217
|
src="test.jpg"
|
|
243
218
|
controller={ createController() }
|
|
@@ -254,7 +229,7 @@ describe( 'Cropper', () => {
|
|
|
254
229
|
} );
|
|
255
230
|
|
|
256
231
|
it( 'shows the interactive grid when a placement control is active', async () => {
|
|
257
|
-
|
|
232
|
+
await renderCropper(
|
|
258
233
|
<Cropper
|
|
259
234
|
src="test.jpg"
|
|
260
235
|
controller={ createController() }
|
|
@@ -277,7 +252,7 @@ describe( 'Cropper', () => {
|
|
|
277
252
|
...controller.state,
|
|
278
253
|
cropRect: { x: 0.1, y: 0.2, width: 0.5, height: 0.4 },
|
|
279
254
|
};
|
|
280
|
-
const { rerender } =
|
|
255
|
+
const { rerender } = await renderCropper(
|
|
281
256
|
<Cropper
|
|
282
257
|
src="test.jpg"
|
|
283
258
|
controller={ controller }
|
|
@@ -289,10 +264,10 @@ describe( 'Cropper', () => {
|
|
|
289
264
|
await screen.findByRole( 'button', {
|
|
290
265
|
name: 'Resize from top-left corner',
|
|
291
266
|
} );
|
|
292
|
-
( controller.setCropRect as
|
|
293
|
-
( controller.adjustCropRectForViewport as
|
|
267
|
+
( controller.setCropRect as Mock ).mockClear();
|
|
268
|
+
( controller.adjustCropRectForViewport as Mock ).mockClear();
|
|
294
269
|
|
|
295
|
-
rerender(
|
|
270
|
+
await rerender(
|
|
296
271
|
<Cropper
|
|
297
272
|
src="test.jpg"
|
|
298
273
|
controller={ controller }
|
|
@@ -316,7 +291,7 @@ describe( 'Cropper', () => {
|
|
|
316
291
|
...controller.state,
|
|
317
292
|
cropRect: { x: 0.1, y: 0.2, width: 0.5, height: 0.4 },
|
|
318
293
|
};
|
|
319
|
-
|
|
294
|
+
await renderCropper(
|
|
320
295
|
<Cropper
|
|
321
296
|
src="test.jpg"
|
|
322
297
|
controller={ controller }
|
|
@@ -339,9 +314,7 @@ describe( 'Cropper', () => {
|
|
|
339
314
|
} );
|
|
340
315
|
|
|
341
316
|
it( 'clears settling state when a new resize starts before the settle fallback fires', async () => {
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
render(
|
|
317
|
+
await renderCropper(
|
|
345
318
|
<Cropper
|
|
346
319
|
src="test.jpg"
|
|
347
320
|
controller={ createController() }
|
|
@@ -353,6 +326,7 @@ describe( 'Cropper', () => {
|
|
|
353
326
|
const handle = await screen.findByRole( 'button', {
|
|
354
327
|
name: 'Resize from top-left corner',
|
|
355
328
|
} );
|
|
329
|
+
vi.useFakeTimers();
|
|
356
330
|
// The settle transition and viewport pan live on the stage, not the
|
|
357
331
|
// canvas (which stays fixed so the root background is never exposed).
|
|
358
332
|
const stage = screen.getByTestId( 'cropper-stage' );
|
|
@@ -367,7 +341,7 @@ describe( 'Cropper', () => {
|
|
|
367
341
|
fireEvent.pointerUp( handle, { pointerId: 1 } );
|
|
368
342
|
|
|
369
343
|
// The settle transition should now be active on the stage.
|
|
370
|
-
expect( stage ).
|
|
344
|
+
expect( hasSettleTransition( stage ) ).toBe( true );
|
|
371
345
|
|
|
372
346
|
// Start a new resize before the settle fallback fires.
|
|
373
347
|
fireEvent.pointerDown( handle, {
|
|
@@ -378,26 +352,20 @@ describe( 'Cropper', () => {
|
|
|
378
352
|
} );
|
|
379
353
|
|
|
380
354
|
// Settling must be cleared — no transition on the drag.
|
|
381
|
-
expect( stage ).
|
|
382
|
-
'transition: transform 200ms ease-out'
|
|
383
|
-
);
|
|
355
|
+
expect( hasSettleTransition( stage ) ).toBe( false );
|
|
384
356
|
|
|
385
357
|
// Advance past the old settle fallback; it was cancelled so the stage
|
|
386
358
|
// should still have no transition.
|
|
387
|
-
act( () =>
|
|
388
|
-
expect( stage ).
|
|
389
|
-
'transition: transform 200ms ease-out'
|
|
390
|
-
);
|
|
359
|
+
act( () => vi.advanceTimersByTime( 300 ) );
|
|
360
|
+
expect( hasSettleTransition( stage ) ).toBe( false );
|
|
391
361
|
|
|
392
362
|
fireEvent.pointerUp( handle, { pointerId: 1 } );
|
|
393
363
|
|
|
394
|
-
|
|
364
|
+
vi.useRealTimers();
|
|
395
365
|
} );
|
|
396
366
|
|
|
397
367
|
it( 'keeps settling active until the settle transform transition ends', async () => {
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
render(
|
|
368
|
+
await renderCropper(
|
|
401
369
|
<Cropper
|
|
402
370
|
src="test.jpg"
|
|
403
371
|
controller={ createController() }
|
|
@@ -409,6 +377,7 @@ describe( 'Cropper', () => {
|
|
|
409
377
|
const handle = await screen.findByRole( 'button', {
|
|
410
378
|
name: 'Resize from top-left corner',
|
|
411
379
|
} );
|
|
380
|
+
vi.useFakeTimers();
|
|
412
381
|
const stage = screen.getByTestId( 'cropper-stage' );
|
|
413
382
|
|
|
414
383
|
fireEvent.pointerDown( handle, {
|
|
@@ -419,10 +388,10 @@ describe( 'Cropper', () => {
|
|
|
419
388
|
} );
|
|
420
389
|
fireEvent.pointerUp( handle, { pointerId: 1 } );
|
|
421
390
|
|
|
422
|
-
expect( stage ).
|
|
391
|
+
expect( hasSettleTransition( stage ) ).toBe( true );
|
|
423
392
|
|
|
424
|
-
act( () =>
|
|
425
|
-
expect( stage ).
|
|
393
|
+
act( () => vi.advanceTimersByTime( 200 ) );
|
|
394
|
+
expect( hasSettleTransition( stage ) ).toBe( true );
|
|
426
395
|
|
|
427
396
|
// The transform transition runs on the image and bubbles up to the
|
|
428
397
|
// stage handler, so fire from the image to exercise the real path.
|
|
@@ -435,17 +404,13 @@ describe( 'Cropper', () => {
|
|
|
435
404
|
} );
|
|
436
405
|
fireEvent( image, transitionEndEvent );
|
|
437
406
|
|
|
438
|
-
expect( stage ).
|
|
439
|
-
'transition: transform 200ms ease-out'
|
|
440
|
-
);
|
|
407
|
+
expect( hasSettleTransition( stage ) ).toBe( false );
|
|
441
408
|
|
|
442
|
-
|
|
409
|
+
vi.useRealTimers();
|
|
443
410
|
} );
|
|
444
411
|
|
|
445
412
|
it( 'clears settling via the fallback timer when no transitionend fires', async () => {
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
render(
|
|
413
|
+
await renderCropper(
|
|
449
414
|
<Cropper
|
|
450
415
|
src="test.jpg"
|
|
451
416
|
controller={ createController() }
|
|
@@ -457,6 +422,7 @@ describe( 'Cropper', () => {
|
|
|
457
422
|
const handle = await screen.findByRole( 'button', {
|
|
458
423
|
name: 'Resize from top-left corner',
|
|
459
424
|
} );
|
|
425
|
+
vi.useFakeTimers();
|
|
460
426
|
const stage = screen.getByTestId( 'cropper-stage' );
|
|
461
427
|
|
|
462
428
|
fireEvent.pointerDown( handle, {
|
|
@@ -467,25 +433,21 @@ describe( 'Cropper', () => {
|
|
|
467
433
|
} );
|
|
468
434
|
fireEvent.pointerUp( handle, { pointerId: 1 } );
|
|
469
435
|
|
|
470
|
-
expect( stage ).
|
|
436
|
+
expect( hasSettleTransition( stage ) ).toBe( true );
|
|
471
437
|
|
|
472
438
|
// Without a transitionend, settling persists past the CSS duration...
|
|
473
|
-
act( () =>
|
|
474
|
-
expect( stage ).
|
|
439
|
+
act( () => vi.advanceTimersByTime( 200 ) );
|
|
440
|
+
expect( hasSettleTransition( stage ) ).toBe( true );
|
|
475
441
|
|
|
476
442
|
// ...and is cleared by the fallback timer (CSS duration + 100ms).
|
|
477
|
-
act( () =>
|
|
478
|
-
expect( stage ).
|
|
479
|
-
'transition: transform 200ms ease-out'
|
|
480
|
-
);
|
|
443
|
+
act( () => vi.advanceTimersByTime( 100 ) );
|
|
444
|
+
expect( hasSettleTransition( stage ) ).toBe( false );
|
|
481
445
|
|
|
482
|
-
|
|
446
|
+
vi.useRealTimers();
|
|
483
447
|
} );
|
|
484
448
|
|
|
485
449
|
it( 'ignores non-transform transitionend events while settling', async () => {
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
render(
|
|
450
|
+
await renderCropper(
|
|
489
451
|
<Cropper
|
|
490
452
|
src="test.jpg"
|
|
491
453
|
controller={ createController() }
|
|
@@ -497,6 +459,7 @@ describe( 'Cropper', () => {
|
|
|
497
459
|
const handle = await screen.findByRole( 'button', {
|
|
498
460
|
name: 'Resize from top-left corner',
|
|
499
461
|
} );
|
|
462
|
+
vi.useFakeTimers();
|
|
500
463
|
const stage = screen.getByTestId( 'cropper-stage' );
|
|
501
464
|
|
|
502
465
|
fireEvent.pointerDown( handle, {
|
|
@@ -507,7 +470,7 @@ describe( 'Cropper', () => {
|
|
|
507
470
|
} );
|
|
508
471
|
fireEvent.pointerUp( handle, { pointerId: 1 } );
|
|
509
472
|
|
|
510
|
-
expect( stage ).
|
|
473
|
+
expect( hasSettleTransition( stage ) ).toBe( true );
|
|
511
474
|
|
|
512
475
|
// A stencil left/top/width/height transitionend must not clear settling.
|
|
513
476
|
const stencilTransitionEnd = new Event( 'transitionend', {
|
|
@@ -518,20 +481,18 @@ describe( 'Cropper', () => {
|
|
|
518
481
|
} );
|
|
519
482
|
fireEvent( stage, stencilTransitionEnd );
|
|
520
483
|
|
|
521
|
-
expect( stage ).
|
|
484
|
+
expect( hasSettleTransition( stage ) ).toBe( true );
|
|
522
485
|
|
|
523
|
-
|
|
486
|
+
vi.useRealTimers();
|
|
524
487
|
} );
|
|
525
488
|
|
|
526
489
|
it( 'pans the canvas when a keyboard resize extends the crop past the canvas edge', async () => {
|
|
527
|
-
jest.useFakeTimers();
|
|
528
|
-
|
|
529
490
|
// zoom: 2 so the image bounds extend past [0, 1] in normalized space,
|
|
530
491
|
// allowing the crop to be resized beyond the canvas edge.
|
|
531
492
|
const controller = createController();
|
|
532
493
|
controller.state = { ...controller.state, zoom: 2 };
|
|
533
494
|
|
|
534
|
-
|
|
495
|
+
await renderCropper(
|
|
535
496
|
<Cropper
|
|
536
497
|
src="test.jpg"
|
|
537
498
|
controller={ controller }
|
|
@@ -544,6 +505,7 @@ describe( 'Cropper', () => {
|
|
|
544
505
|
const eHandle = await screen.findByRole( 'button', {
|
|
545
506
|
name: 'Resize from right edge',
|
|
546
507
|
} );
|
|
508
|
+
vi.useFakeTimers();
|
|
547
509
|
const stage = screen.getByTestId( 'cropper-stage' );
|
|
548
510
|
|
|
549
511
|
// cropRect starts at {x:0, y:0, width:1, height:1} (right edge = 1.0).
|
|
@@ -560,12 +522,12 @@ describe( 'Cropper', () => {
|
|
|
560
522
|
expect( Number( match?.[ 1 ] ) ).toBeCloseTo( -2, 4 );
|
|
561
523
|
expect( Number( match?.[ 2 ] ) ).toBeCloseTo( 0, 5 );
|
|
562
524
|
|
|
563
|
-
|
|
525
|
+
vi.useRealTimers();
|
|
564
526
|
} );
|
|
565
527
|
|
|
566
528
|
it( 'ignores wheel zoom while a crop resize is active', async () => {
|
|
567
529
|
const controller = createController();
|
|
568
|
-
|
|
530
|
+
await renderCropper(
|
|
569
531
|
<Cropper
|
|
570
532
|
src="test.jpg"
|
|
571
533
|
controller={ controller }
|
|
@@ -604,7 +566,7 @@ describe( 'Cropper', () => {
|
|
|
604
566
|
|
|
605
567
|
it( 'does not start canvas drag from touch pointer events', async () => {
|
|
606
568
|
const controller = createController();
|
|
607
|
-
|
|
569
|
+
await renderCropper(
|
|
608
570
|
<Cropper
|
|
609
571
|
src="test.jpg"
|
|
610
572
|
controller={ controller }
|
|
@@ -642,7 +604,7 @@ describe( 'Cropper', () => {
|
|
|
642
604
|
|
|
643
605
|
it( 'ignores pointer drags on the canvas while disabled', async () => {
|
|
644
606
|
const controller = createController();
|
|
645
|
-
render(
|
|
607
|
+
await render(
|
|
646
608
|
<Cropper
|
|
647
609
|
src="test.jpg"
|
|
648
610
|
controller={ controller }
|
|
@@ -690,13 +652,13 @@ describe( 'Cropper', () => {
|
|
|
690
652
|
showGrid: 'interactive' as const,
|
|
691
653
|
freeformCrop: true,
|
|
692
654
|
};
|
|
693
|
-
const { rerender } = render( <Cropper { ...props } disabled /> );
|
|
655
|
+
const { rerender } = await render( <Cropper { ...props } disabled /> );
|
|
694
656
|
|
|
695
657
|
await screen.findByRole( 'button', {
|
|
696
658
|
name: 'Resize from top-left corner',
|
|
697
659
|
} );
|
|
698
660
|
|
|
699
|
-
rerender( <Cropper { ...props } /> );
|
|
661
|
+
await rerender( <Cropper { ...props } /> );
|
|
700
662
|
|
|
701
663
|
const canvas = screen.getByRole( 'group', { name: 'Crop area' } );
|
|
702
664
|
fireEvent.pointerDown( canvas, {
|
|
@@ -719,8 +681,8 @@ describe( 'Cropper', () => {
|
|
|
719
681
|
|
|
720
682
|
it( 'ends the gesture when a drag is cancelled by disabling', async () => {
|
|
721
683
|
const controller = createController();
|
|
722
|
-
const onGestureStart =
|
|
723
|
-
const onGestureEnd =
|
|
684
|
+
const onGestureStart = vi.fn();
|
|
685
|
+
const onGestureEnd = vi.fn();
|
|
724
686
|
const props = {
|
|
725
687
|
src: 'test.jpg',
|
|
726
688
|
controller,
|
|
@@ -730,7 +692,7 @@ describe( 'Cropper', () => {
|
|
|
730
692
|
onGestureStart,
|
|
731
693
|
onGestureEnd,
|
|
732
694
|
};
|
|
733
|
-
const { rerender } = render( <Cropper { ...props } /> );
|
|
695
|
+
const { rerender } = await render( <Cropper { ...props } /> );
|
|
734
696
|
|
|
735
697
|
await screen.findByRole( 'button', {
|
|
736
698
|
name: 'Resize from top-left corner',
|
|
@@ -753,7 +715,7 @@ describe( 'Cropper', () => {
|
|
|
753
715
|
} );
|
|
754
716
|
await waitFor( () => expect( onGestureStart ).toHaveBeenCalled() );
|
|
755
717
|
|
|
756
|
-
rerender( <Cropper { ...props } disabled /> );
|
|
718
|
+
await rerender( <Cropper { ...props } disabled /> );
|
|
757
719
|
|
|
758
720
|
// Without this the caller is left believing a gesture is still
|
|
759
721
|
// running, which keeps undo/redo disabled after a failed save.
|
|
@@ -763,7 +725,7 @@ describe( 'Cropper', () => {
|
|
|
763
725
|
} );
|
|
764
726
|
|
|
765
727
|
it( 'does not signal a gesture end when disabling with no gesture running', async () => {
|
|
766
|
-
const onGestureEnd =
|
|
728
|
+
const onGestureEnd = vi.fn();
|
|
767
729
|
const props = {
|
|
768
730
|
src: 'test.jpg',
|
|
769
731
|
controller: createController(),
|
|
@@ -772,20 +734,20 @@ describe( 'Cropper', () => {
|
|
|
772
734
|
freeformCrop: true,
|
|
773
735
|
onGestureEnd,
|
|
774
736
|
};
|
|
775
|
-
const { rerender } = render( <Cropper { ...props } /> );
|
|
737
|
+
const { rerender } = await render( <Cropper { ...props } /> );
|
|
776
738
|
|
|
777
739
|
await screen.findByRole( 'button', {
|
|
778
740
|
name: 'Resize from top-left corner',
|
|
779
741
|
} );
|
|
780
742
|
|
|
781
|
-
rerender( <Cropper { ...props } disabled /> );
|
|
743
|
+
await rerender( <Cropper { ...props } disabled /> );
|
|
782
744
|
|
|
783
745
|
expect( onGestureEnd ).not.toHaveBeenCalled();
|
|
784
746
|
} );
|
|
785
747
|
|
|
786
748
|
it( 'signals a fresh gesture on wheel zoom after a save interrupted one', async () => {
|
|
787
749
|
const controller = createController();
|
|
788
|
-
const onGestureStart =
|
|
750
|
+
const onGestureStart = vi.fn();
|
|
789
751
|
const props = {
|
|
790
752
|
src: 'test.jpg',
|
|
791
753
|
controller,
|
|
@@ -794,7 +756,7 @@ describe( 'Cropper', () => {
|
|
|
794
756
|
freeformCrop: true,
|
|
795
757
|
onGestureStart,
|
|
796
758
|
};
|
|
797
|
-
const { rerender } =
|
|
759
|
+
const { rerender } = await renderCropper( <Cropper { ...props } /> );
|
|
798
760
|
|
|
799
761
|
await screen.findByRole( 'button', {
|
|
800
762
|
name: 'Resize from top-left corner',
|
|
@@ -802,22 +764,22 @@ describe( 'Cropper', () => {
|
|
|
802
764
|
const canvas = screen.getByRole( 'group', { name: 'Crop area' } );
|
|
803
765
|
|
|
804
766
|
// A wheel zoom opens a debounced gesture.
|
|
805
|
-
|
|
767
|
+
await userEvent.wheel( canvas, { delta: { y: -120 } } );
|
|
806
768
|
await waitFor( () => expect( onGestureStart ).toHaveBeenCalled() );
|
|
807
769
|
|
|
808
770
|
// Saving cancels it, then the save fails and tools come back.
|
|
809
|
-
rerender( <Cropper { ...props } disabled /> );
|
|
810
|
-
rerender( <Cropper { ...props } /> );
|
|
771
|
+
await rerender( <Cropper { ...props } disabled /> );
|
|
772
|
+
await rerender( <Cropper { ...props } /> );
|
|
811
773
|
onGestureStart.mockClear();
|
|
812
774
|
|
|
813
775
|
// The next wheel zoom must open a gesture of its own.
|
|
814
|
-
|
|
776
|
+
await userEvent.wheel( canvas, { delta: { y: -120 } } );
|
|
815
777
|
await waitFor( () => expect( onGestureStart ).toHaveBeenCalled() );
|
|
816
778
|
} );
|
|
817
779
|
|
|
818
780
|
it( 'ignores wheel zoom while disabled', async () => {
|
|
819
781
|
const controller = createController();
|
|
820
|
-
|
|
782
|
+
await renderCropper(
|
|
821
783
|
<Cropper
|
|
822
784
|
src="test.jpg"
|
|
823
785
|
controller={ controller }
|
|
@@ -833,7 +795,10 @@ describe( 'Cropper', () => {
|
|
|
833
795
|
} );
|
|
834
796
|
const canvas = screen.getByRole( 'group', { name: 'Crop area' } );
|
|
835
797
|
|
|
836
|
-
|
|
798
|
+
const onWheel = vi.fn();
|
|
799
|
+
canvas.addEventListener( 'wheel', onWheel, { once: true } );
|
|
800
|
+
await userEvent.wheel( canvas, { delta: { y: -120 } } );
|
|
801
|
+
await waitFor( () => expect( onWheel ).toHaveBeenCalledOnce() );
|
|
837
802
|
|
|
838
803
|
await act( async () => {
|
|
839
804
|
await new Promise( ( resolve ) =>
|
|
@@ -846,7 +811,7 @@ describe( 'Cropper', () => {
|
|
|
846
811
|
|
|
847
812
|
it( 'ignores keyboard pan while disabled', async () => {
|
|
848
813
|
const controller = createController();
|
|
849
|
-
|
|
814
|
+
await renderCropper(
|
|
850
815
|
<Cropper
|
|
851
816
|
src="test.jpg"
|
|
852
817
|
controller={ controller }
|
|
@@ -858,7 +823,9 @@ describe( 'Cropper', () => {
|
|
|
858
823
|
);
|
|
859
824
|
|
|
860
825
|
const canvas = screen.getByRole( 'group', { name: 'Crop area' } );
|
|
861
|
-
|
|
826
|
+
canvas.focus();
|
|
827
|
+
expect( canvas ).toHaveFocus();
|
|
828
|
+
await userEvent.keyboard( '{ArrowRight}' );
|
|
862
829
|
|
|
863
830
|
expect( controller.setPan ).not.toHaveBeenCalled();
|
|
864
831
|
} );
|
|
@@ -872,7 +839,7 @@ describe( 'Cropper', () => {
|
|
|
872
839
|
showGrid: 'interactive' as const,
|
|
873
840
|
freeformCrop: true,
|
|
874
841
|
};
|
|
875
|
-
const { rerender } = render( <Cropper { ...props } /> );
|
|
842
|
+
const { rerender } = await render( <Cropper { ...props } /> );
|
|
876
843
|
|
|
877
844
|
await screen.findByRole( 'button', {
|
|
878
845
|
name: 'Resize from top-left corner',
|
|
@@ -887,8 +854,8 @@ describe( 'Cropper', () => {
|
|
|
887
854
|
isPrimary: true,
|
|
888
855
|
} );
|
|
889
856
|
|
|
890
|
-
rerender( <Cropper { ...props } disabled /> );
|
|
891
|
-
( controller.setPan
|
|
857
|
+
await rerender( <Cropper { ...props } disabled /> );
|
|
858
|
+
vi.mocked( controller.setPan ).mockClear();
|
|
892
859
|
|
|
893
860
|
fireEvent.pointerMove( canvas, {
|
|
894
861
|
button: 0,
|
|
@@ -908,7 +875,7 @@ describe( 'Cropper', () => {
|
|
|
908
875
|
|
|
909
876
|
it( 'does not resize the crop from a handle drag while disabled', async () => {
|
|
910
877
|
const controller = createController();
|
|
911
|
-
render(
|
|
878
|
+
await render(
|
|
912
879
|
<Cropper
|
|
913
880
|
src="test.jpg"
|
|
914
881
|
controller={ controller }
|
|
@@ -951,12 +918,12 @@ describe( 'Cropper', () => {
|
|
|
951
918
|
cb( 0 );
|
|
952
919
|
return 0;
|
|
953
920
|
};
|
|
954
|
-
globalThis.cancelAnimationFrame =
|
|
921
|
+
globalThis.cancelAnimationFrame = vi.fn();
|
|
955
922
|
|
|
956
923
|
try {
|
|
957
924
|
const controller = createController();
|
|
958
|
-
const onGestureEnd =
|
|
959
|
-
|
|
925
|
+
const onGestureEnd = vi.fn();
|
|
926
|
+
await renderCropper(
|
|
960
927
|
<Cropper
|
|
961
928
|
src="test.jpg"
|
|
962
929
|
controller={ controller }
|
|
@@ -982,7 +949,7 @@ describe( 'Cropper', () => {
|
|
|
982
949
|
isPrimary: true,
|
|
983
950
|
} );
|
|
984
951
|
|
|
985
|
-
( controller.setPan as
|
|
952
|
+
( controller.setPan as Mock ).mockClear();
|
|
986
953
|
fireEvent.pointerDown( canvas, {
|
|
987
954
|
button: 0,
|
|
988
955
|
clientX: 250,
|
|
@@ -1002,12 +969,21 @@ describe( 'Cropper', () => {
|
|
|
1002
969
|
|
|
1003
970
|
expect( controller.setPan ).not.toHaveBeenCalled();
|
|
1004
971
|
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
972
|
+
const touch = ( identifier: number, clientX: number ) =>
|
|
973
|
+
new Touch( {
|
|
974
|
+
identifier,
|
|
975
|
+
target: canvas,
|
|
976
|
+
clientX,
|
|
977
|
+
clientY: 200,
|
|
978
|
+
} );
|
|
979
|
+
fireEvent(
|
|
980
|
+
canvas,
|
|
981
|
+
new TouchEvent( 'touchstart', {
|
|
982
|
+
bubbles: true,
|
|
983
|
+
cancelable: true,
|
|
984
|
+
touches: [ touch( 1, 250 ), touch( 2, 350 ) ],
|
|
985
|
+
} )
|
|
986
|
+
);
|
|
1011
987
|
|
|
1012
988
|
await act( async () => {
|
|
1013
989
|
await Promise.resolve();
|
|
@@ -1016,8 +992,8 @@ describe( 'Cropper', () => {
|
|
|
1016
992
|
expect( controller.settleCrop ).not.toHaveBeenCalled();
|
|
1017
993
|
expect( onGestureEnd ).not.toHaveBeenCalled();
|
|
1018
994
|
|
|
1019
|
-
( controller.setCropRect as
|
|
1020
|
-
( controller.setZoomAtPoint as
|
|
995
|
+
( controller.setCropRect as Mock ).mockClear();
|
|
996
|
+
( controller.setZoomAtPoint as Mock ).mockClear();
|
|
1021
997
|
|
|
1022
998
|
fireEvent.pointerMove( resizeHandle, {
|
|
1023
999
|
button: 0,
|
|
@@ -1027,17 +1003,26 @@ describe( 'Cropper', () => {
|
|
|
1027
1003
|
pointerType: 'touch',
|
|
1028
1004
|
isPrimary: true,
|
|
1029
1005
|
} );
|
|
1030
|
-
fireEvent
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1006
|
+
fireEvent(
|
|
1007
|
+
document,
|
|
1008
|
+
new TouchEvent( 'touchmove', {
|
|
1009
|
+
bubbles: true,
|
|
1010
|
+
cancelable: true,
|
|
1011
|
+
touches: [ touch( 1, 225 ), touch( 2, 375 ) ],
|
|
1012
|
+
} )
|
|
1013
|
+
);
|
|
1036
1014
|
|
|
1037
1015
|
expect( controller.setCropRect ).not.toHaveBeenCalled();
|
|
1038
1016
|
expect( controller.setZoomAtPoint ).toHaveBeenCalled();
|
|
1039
1017
|
|
|
1040
|
-
fireEvent
|
|
1018
|
+
fireEvent(
|
|
1019
|
+
canvas,
|
|
1020
|
+
new TouchEvent( 'touchend', {
|
|
1021
|
+
bubbles: true,
|
|
1022
|
+
cancelable: true,
|
|
1023
|
+
touches: [],
|
|
1024
|
+
} )
|
|
1025
|
+
);
|
|
1041
1026
|
|
|
1042
1027
|
expect( onGestureEnd ).toHaveBeenCalledTimes( 1 );
|
|
1043
1028
|
} finally {
|
|
@@ -1047,7 +1032,7 @@ describe( 'Cropper', () => {
|
|
|
1047
1032
|
} );
|
|
1048
1033
|
|
|
1049
1034
|
// View-scale: at rest, the scene magnifies so an under-filling crop fills
|
|
1050
|
-
// the canvas.
|
|
1035
|
+
// the canvas. The sized fixture gives the canvas a real 600x400 box. A tall 400x1200 image contain-
|
|
1051
1036
|
// fits to a 133.33x400 footprint (fills the height, 133px of 600 wide). A
|
|
1052
1037
|
// square crop is bound by the footprint's 133px width, so it magnifies.
|
|
1053
1038
|
const TALL_IMAGE = {
|
|
@@ -1080,7 +1065,7 @@ describe( 'Cropper', () => {
|
|
|
1080
1065
|
};
|
|
1081
1066
|
}
|
|
1082
1067
|
|
|
1083
|
-
it( 'magnifies the scene so an under-filling crop fills the canvas at rest', () => {
|
|
1068
|
+
it( 'magnifies the scene so an under-filling crop fills the canvas at rest', async () => {
|
|
1084
1069
|
const controller = createController();
|
|
1085
1070
|
controller.state = {
|
|
1086
1071
|
...controller.state,
|
|
@@ -1089,14 +1074,16 @@ describe( 'Cropper', () => {
|
|
|
1089
1074
|
// 1/3 of 400 = 133px. On-screen 133x133, well below 0.8 * 400.
|
|
1090
1075
|
cropRect: { x: 0, y: 1 / 3, width: 1, height: 1 / 3 },
|
|
1091
1076
|
};
|
|
1092
|
-
|
|
1077
|
+
await renderCropper(
|
|
1078
|
+
<Cropper src="tall.jpg" controller={ controller } />
|
|
1079
|
+
);
|
|
1093
1080
|
|
|
1094
1081
|
// Footprint is 133.33x400. The square crop's binding axis (height) fills
|
|
1095
1082
|
// to 0.8 * 400, so the image magnifies by 0.8 * 400 / 133.33 = 2.4.
|
|
1096
|
-
expect( imageScale() ).toBeCloseTo( 2.4, 2 );
|
|
1083
|
+
await waitFor( () => expect( imageScale() ).toBeCloseTo( 2.4, 2 ) );
|
|
1097
1084
|
} );
|
|
1098
1085
|
|
|
1099
|
-
it( 'computes image pan from the unscaled footprint before applying view scale', () => {
|
|
1086
|
+
it( 'computes image pan from the unscaled footprint before applying view scale', async () => {
|
|
1100
1087
|
const controller = createController();
|
|
1101
1088
|
controller.state = {
|
|
1102
1089
|
...controller.state,
|
|
@@ -1104,8 +1091,11 @@ describe( 'Cropper', () => {
|
|
|
1104
1091
|
cropRect: { x: 0, y: 1 / 3, width: 1, height: 1 / 3 },
|
|
1105
1092
|
pan: { x: 0.1, y: -0.05 },
|
|
1106
1093
|
};
|
|
1107
|
-
|
|
1094
|
+
await renderCropper(
|
|
1095
|
+
<Cropper src="tall.jpg" controller={ controller } />
|
|
1096
|
+
);
|
|
1108
1097
|
|
|
1098
|
+
await waitFor( () => expect( imageScale() ).toBeCloseTo( 2.4, 2 ) );
|
|
1109
1099
|
const { viewScale, matrix } = imageTransformParts();
|
|
1110
1100
|
|
|
1111
1101
|
expect( viewScale ).toBeCloseTo( 2.4, 2 );
|
|
@@ -1113,20 +1103,22 @@ describe( 'Cropper', () => {
|
|
|
1113
1103
|
// 133.33x400 footprint, then magnified by the leading scale(viewScale).
|
|
1114
1104
|
// If useTransformStyle received scaledVisualSize, these would be 32 and
|
|
1115
1105
|
// -48 instead.
|
|
1116
|
-
expect( matrix[ 4 ] ).toBeCloseTo( ( 400 / 3 ) * 0.1,
|
|
1106
|
+
expect( matrix[ 4 ] ).toBeCloseTo( ( 400 / 3 ) * 0.1, 4 );
|
|
1117
1107
|
expect( matrix[ 5 ] ).toBeCloseTo( 400 * -0.05, 5 );
|
|
1118
1108
|
} );
|
|
1119
1109
|
|
|
1120
|
-
it( 'positions crop overlays against the magnified footprint', () => {
|
|
1110
|
+
it( 'positions crop overlays against the magnified footprint', async () => {
|
|
1121
1111
|
const controller = createController();
|
|
1122
1112
|
controller.state = {
|
|
1123
1113
|
...controller.state,
|
|
1124
1114
|
image: TALL_IMAGE,
|
|
1125
1115
|
cropRect: { x: 0, y: 1 / 3, width: 1, height: 1 / 3 },
|
|
1126
1116
|
};
|
|
1127
|
-
|
|
1117
|
+
await renderCropper(
|
|
1118
|
+
<Cropper src="tall.jpg" controller={ controller } showGrid />
|
|
1119
|
+
);
|
|
1128
1120
|
|
|
1129
|
-
expect( imageScale() ).toBeCloseTo( 2.4, 2 );
|
|
1121
|
+
await waitFor( () => expect( imageScale() ).toBeCloseTo( 2.4, 2 ) );
|
|
1130
1122
|
|
|
1131
1123
|
const expectedRect = {
|
|
1132
1124
|
left: 140,
|
|
@@ -1160,7 +1152,7 @@ describe( 'Cropper', () => {
|
|
|
1160
1152
|
expectOverlayRect( screen.getByTestId( GRID_TEST_ID ) );
|
|
1161
1153
|
} );
|
|
1162
1154
|
|
|
1163
|
-
it( 'does not magnify when the crop already fills the canvas', () => {
|
|
1155
|
+
it( 'does not magnify when the crop already fills the canvas', async () => {
|
|
1164
1156
|
const controller = createController();
|
|
1165
1157
|
controller.state = {
|
|
1166
1158
|
...controller.state,
|
|
@@ -1168,8 +1160,16 @@ describe( 'Cropper', () => {
|
|
|
1168
1160
|
// Full-frame crop already fills the canvas height (footprint == 400).
|
|
1169
1161
|
cropRect: { x: 0, y: 0, width: 1, height: 1 },
|
|
1170
1162
|
};
|
|
1171
|
-
|
|
1163
|
+
await renderCropper(
|
|
1164
|
+
<Cropper src="tall.jpg" controller={ controller } />
|
|
1165
|
+
);
|
|
1172
1166
|
|
|
1167
|
+
await waitFor( () =>
|
|
1168
|
+
expect( controller.setVisualSize ).toHaveBeenLastCalledWith( {
|
|
1169
|
+
width: expect.closeTo( 400 / 3, 2 ),
|
|
1170
|
+
height: 400,
|
|
1171
|
+
} )
|
|
1172
|
+
);
|
|
1173
1173
|
// viewScale = 1, so the leading scale is identity (no magnification).
|
|
1174
1174
|
expect( imageScale() ).toBe( 1 );
|
|
1175
1175
|
} );
|
|
@@ -1181,16 +1181,15 @@ describe( 'Cropper', () => {
|
|
|
1181
1181
|
image: TALL_IMAGE,
|
|
1182
1182
|
cropRect: { x: 0, y: 1 / 3, width: 1, height: 1 / 3 },
|
|
1183
1183
|
};
|
|
1184
|
-
|
|
1184
|
+
await renderCropper(
|
|
1185
1185
|
<Cropper src="tall.jpg" controller={ controller } freeformCrop />
|
|
1186
1186
|
);
|
|
1187
1187
|
|
|
1188
|
-
// At rest the square crop magnifies the image by 2.4.
|
|
1189
|
-
expect( imageScale() ).toBeCloseTo( 2.4, 2 );
|
|
1190
|
-
|
|
1191
1188
|
const handle = await screen.findByRole( 'button', {
|
|
1192
1189
|
name: 'Resize from top-left corner',
|
|
1193
1190
|
} );
|
|
1191
|
+
// At rest the square crop magnifies the image by 2.4.
|
|
1192
|
+
expect( imageScale() ).toBeCloseTo( 2.4, 2 );
|
|
1194
1193
|
fireEvent.pointerDown( handle, {
|
|
1195
1194
|
button: 0,
|
|
1196
1195
|
clientX: 100,
|
|
@@ -1218,17 +1217,17 @@ describe( 'Cropper', () => {
|
|
|
1218
1217
|
{ ...controller.state, cropRect },
|
|
1219
1218
|
{ width: image.naturalWidth, height: image.naturalHeight }
|
|
1220
1219
|
);
|
|
1221
|
-
|
|
1220
|
+
await renderCropper(
|
|
1222
1221
|
<Cropper src="tiny.png" controller={ controller } freeformCrop />
|
|
1223
1222
|
);
|
|
1224
1223
|
|
|
1225
1224
|
const handle = await screen.findByRole( 'button', {
|
|
1226
1225
|
name: 'Resize from right edge',
|
|
1227
1226
|
} );
|
|
1228
|
-
|
|
1227
|
+
handle.focus();
|
|
1228
|
+
await userEvent.keyboard( '{ArrowRight}' );
|
|
1229
1229
|
|
|
1230
|
-
const rect = ( controller.setCropRect as
|
|
1231
|
-
.calls[ 0 ][ 0 ];
|
|
1230
|
+
const rect = ( controller.setCropRect as Mock ).mock.calls[ 0 ][ 0 ];
|
|
1232
1231
|
const region = getSourceRegion(
|
|
1233
1232
|
{ ...controller.state, cropRect: rect },
|
|
1234
1233
|
{ width: image.naturalWidth, height: image.naturalHeight }
|
|
@@ -1260,18 +1259,18 @@ describe( 'Cropper', () => {
|
|
|
1260
1259
|
{ ...controller.state, cropRect },
|
|
1261
1260
|
{ width: image.naturalWidth, height: image.naturalHeight }
|
|
1262
1261
|
);
|
|
1263
|
-
|
|
1262
|
+
await renderCropper(
|
|
1264
1263
|
<Cropper src="wide.png" controller={ controller } freeformCrop />
|
|
1265
1264
|
);
|
|
1266
1265
|
|
|
1267
1266
|
const handle = await screen.findByRole( 'button', {
|
|
1268
1267
|
name: 'Resize from right edge',
|
|
1269
1268
|
} );
|
|
1270
|
-
( controller.setCropRect as
|
|
1271
|
-
|
|
1269
|
+
( controller.setCropRect as Mock ).mockClear();
|
|
1270
|
+
handle.focus();
|
|
1271
|
+
await userEvent.keyboard( '{ArrowRight}' );
|
|
1272
1272
|
|
|
1273
|
-
const rect = ( controller.setCropRect as
|
|
1274
|
-
.calls[ 0 ][ 0 ];
|
|
1273
|
+
const rect = ( controller.setCropRect as Mock ).mock.calls[ 0 ][ 0 ];
|
|
1275
1274
|
const region = getSourceRegion(
|
|
1276
1275
|
{ ...controller.state, cropRect: rect },
|
|
1277
1276
|
{ width: image.naturalWidth, height: image.naturalHeight }
|
|
@@ -1295,17 +1294,17 @@ describe( 'Cropper', () => {
|
|
|
1295
1294
|
image,
|
|
1296
1295
|
cropRect,
|
|
1297
1296
|
};
|
|
1298
|
-
|
|
1297
|
+
await renderCropper(
|
|
1299
1298
|
<Cropper src="large.png" controller={ controller } freeformCrop />
|
|
1300
1299
|
);
|
|
1301
1300
|
|
|
1302
1301
|
const handle = await screen.findByRole( 'button', {
|
|
1303
1302
|
name: 'Resize from right edge',
|
|
1304
1303
|
} );
|
|
1305
|
-
|
|
1304
|
+
handle.focus();
|
|
1305
|
+
await userEvent.keyboard( '{ArrowRight}' );
|
|
1306
1306
|
|
|
1307
|
-
const rect = ( controller.setCropRect as
|
|
1308
|
-
.calls[ 0 ][ 0 ];
|
|
1307
|
+
const rect = ( controller.setCropRect as Mock ).mock.calls[ 0 ][ 0 ];
|
|
1309
1308
|
const region = getSourceRegion(
|
|
1310
1309
|
{ ...controller.state, cropRect: rect },
|
|
1311
1310
|
{ width: image.naturalWidth, height: image.naturalHeight }
|
|
@@ -1337,7 +1336,7 @@ describe( 'Cropper', () => {
|
|
|
1337
1336
|
cropRect,
|
|
1338
1337
|
zoom: 0.5,
|
|
1339
1338
|
};
|
|
1340
|
-
const { rerender } =
|
|
1339
|
+
const { rerender } = await renderCropper(
|
|
1341
1340
|
<Cropper
|
|
1342
1341
|
src="crossing.png"
|
|
1343
1342
|
controller={ controller }
|
|
@@ -1349,7 +1348,7 @@ describe( 'Cropper', () => {
|
|
|
1349
1348
|
expect( controller.setCropRect ).not.toHaveBeenCalled();
|
|
1350
1349
|
|
|
1351
1350
|
controller.state = { ...controller.state, zoom: 3 };
|
|
1352
|
-
rerender(
|
|
1351
|
+
await rerender(
|
|
1353
1352
|
<Cropper
|
|
1354
1353
|
src="crossing.png"
|
|
1355
1354
|
controller={ controller }
|
|
@@ -1360,8 +1359,7 @@ describe( 'Cropper', () => {
|
|
|
1360
1359
|
await waitFor( () =>
|
|
1361
1360
|
expect( controller.setCropRect ).toHaveBeenCalledTimes( 1 )
|
|
1362
1361
|
);
|
|
1363
|
-
const rect = ( controller.setCropRect as
|
|
1364
|
-
.calls[ 0 ][ 0 ];
|
|
1362
|
+
const rect = ( controller.setCropRect as Mock ).mock.calls[ 0 ][ 0 ];
|
|
1365
1363
|
const region = getSourceRegion(
|
|
1366
1364
|
{ ...controller.state, cropRect: rect },
|
|
1367
1365
|
{ width: image.naturalWidth, height: image.naturalHeight }
|
|
@@ -1394,7 +1392,7 @@ describe( 'Cropper', () => {
|
|
|
1394
1392
|
},
|
|
1395
1393
|
zoom: 0.5,
|
|
1396
1394
|
};
|
|
1397
|
-
const { rerender } =
|
|
1395
|
+
const { rerender } = await renderCropper(
|
|
1398
1396
|
<Cropper
|
|
1399
1397
|
src="locked-ratio.png"
|
|
1400
1398
|
controller={ controller }
|
|
@@ -1405,7 +1403,7 @@ describe( 'Cropper', () => {
|
|
|
1405
1403
|
|
|
1406
1404
|
await screen.findByTestId( 'cropper-image' );
|
|
1407
1405
|
controller.state = { ...controller.state, zoom: 3 };
|
|
1408
|
-
rerender(
|
|
1406
|
+
await rerender(
|
|
1409
1407
|
<Cropper
|
|
1410
1408
|
src="locked-ratio.png"
|
|
1411
1409
|
controller={ controller }
|
|
@@ -1418,11 +1416,12 @@ describe( 'Cropper', () => {
|
|
|
1418
1416
|
} );
|
|
1419
1417
|
|
|
1420
1418
|
function imageRendering(): string {
|
|
1421
|
-
return
|
|
1422
|
-
.
|
|
1419
|
+
return getComputedStyle(
|
|
1420
|
+
screen.getByTestId< HTMLImageElement >( 'cropper-image' )
|
|
1421
|
+
).imageRendering;
|
|
1423
1422
|
}
|
|
1424
1423
|
|
|
1425
|
-
it( 'renders upscaled (small) images pixelated so pixel boundaries stay crisp', () => {
|
|
1424
|
+
it( 'renders upscaled (small) images pixelated so pixel boundaries stay crisp', async () => {
|
|
1426
1425
|
const controller = createController();
|
|
1427
1426
|
controller.state = {
|
|
1428
1427
|
...controller.state,
|
|
@@ -1430,12 +1429,14 @@ describe( 'Cropper', () => {
|
|
|
1430
1429
|
// upscale — so the display scale exceeds 1:1.
|
|
1431
1430
|
image: { src: 'tiny.png', naturalWidth: 50, naturalHeight: 50 },
|
|
1432
1431
|
};
|
|
1433
|
-
|
|
1432
|
+
await renderCropper(
|
|
1433
|
+
<Cropper src="tiny.png" controller={ controller } />
|
|
1434
|
+
);
|
|
1434
1435
|
|
|
1435
|
-
expect( imageRendering() ).toBe( 'pixelated' );
|
|
1436
|
+
await waitFor( () => expect( imageRendering() ).toBe( 'pixelated' ) );
|
|
1436
1437
|
} );
|
|
1437
1438
|
|
|
1438
|
-
it( 'renders downscaled (large) images smoothly', () => {
|
|
1439
|
+
it( 'renders downscaled (large) images smoothly', async () => {
|
|
1439
1440
|
const controller = createController();
|
|
1440
1441
|
controller.state = {
|
|
1441
1442
|
...controller.state,
|
|
@@ -1444,8 +1445,16 @@ describe( 'Cropper', () => {
|
|
|
1444
1445
|
image: TALL_IMAGE,
|
|
1445
1446
|
cropRect: { x: 0, y: 1 / 3, width: 1, height: 1 / 3 },
|
|
1446
1447
|
};
|
|
1447
|
-
|
|
1448
|
+
await renderCropper(
|
|
1449
|
+
<Cropper src="tall.jpg" controller={ controller } />
|
|
1450
|
+
);
|
|
1448
1451
|
|
|
1452
|
+
await waitFor( () =>
|
|
1453
|
+
expect( controller.setVisualSize ).toHaveBeenLastCalledWith( {
|
|
1454
|
+
width: expect.closeTo( 400 / 3, 2 ),
|
|
1455
|
+
height: 400,
|
|
1456
|
+
} )
|
|
1457
|
+
);
|
|
1449
1458
|
expect( imageRendering() ).not.toBe( 'pixelated' );
|
|
1450
1459
|
} );
|
|
1451
1460
|
} );
|