|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { userEvent } from 'vitest/browser'; |
| 3 | +import { act, screen, waitFor } from '@testing-library/react'; |
| 4 | +import { render } from 'vitest-browser-react'; |
| 5 | +import { useEffect } from '@wordpress/element'; |
| 6 | +import { Cropper } from '../../image-editor'; |
| 7 | +import { |
| 8 | + useMediaEditorState, |
| 9 | + type MediaEditorController, |
| 10 | +} from '../use-media-editor-state'; |
| 11 | +// Browser Mode needs the package's real styles for layout and transitions. |
| 12 | +// eslint-disable-next-line @wordpress/no-non-module-stylesheet-imports |
| 13 | +import '../../image-editor/style.scss'; |
| 14 | + |
| 15 | +// Shown below 1:1 in the fixture, so pixel snapping stays off until a crop |
| 16 | +// magnifies the image past one screen pixel per source pixel. |
| 17 | +const IMAGE = { |
| 18 | + src: 'large.png', |
| 19 | + naturalWidth: 999, |
| 20 | + naturalHeight: 999, |
| 21 | +}; |
| 22 | + |
| 23 | +function CropperWithHistory( { |
| 24 | + onController, |
| 25 | +}: { |
| 26 | + onController: ( controller: MediaEditorController ) => void; |
| 27 | +} ) { |
| 28 | + const controller = useMediaEditorState( { cropper: { image: IMAGE } } ); |
| 29 | + useEffect( () => onController( controller ), [ controller, onController ] ); |
| 30 | + return ( |
| 31 | + <div style={ { width: 648, height: 448 } }> |
| 32 | + <Cropper |
| 33 | + src={ IMAGE.src } |
| 34 | + controller={ controller } |
| 35 | + freeformCrop |
| 36 | + onGestureStart={ controller.beginGesture } |
| 37 | + onGestureEnd={ controller.endGesture } |
| 38 | + /> |
| 39 | + </div> |
| 40 | + ); |
| 41 | +} |
| 42 | + |
| 43 | +describe( 'useMediaEditorState with a Cropper', () => { |
| 44 | + it( 'keeps redo steps after redoing a handle crop that turns on pixel snapping', async () => { |
| 45 | + let controller!: MediaEditorController; |
| 46 | + const onController = ( next: MediaEditorController ) => { |
| 47 | + controller = next; |
| 48 | + }; |
| 49 | + await render( <CropperWithHistory onController={ onController } /> ); |
| 50 | + const handle = await screen.findByRole( 'button', { |
| 51 | + name: 'Resize from bottom-right corner', |
| 52 | + } ); |
| 53 | + |
| 54 | + // Shrink the crop to 30% in steps of 10% of a 999px image, so its |
| 55 | + // edges fall between source pixels, then wait for it to settle. |
| 56 | + handle.focus(); |
| 57 | + await userEvent.keyboard( |
| 58 | + '{Shift>}' + |
| 59 | + '{ArrowLeft}'.repeat( 7 ) + |
| 60 | + '{ArrowUp}'.repeat( 7 ) + |
| 61 | + '{/Shift}' |
| 62 | + ); |
| 63 | + await waitFor( () => |
| 64 | + expect( controller.state.zoom ).toBeGreaterThan( 1 ) |
| 65 | + ); |
| 66 | + |
| 67 | + act( () => |
| 68 | + controller.setFlip( { horizontal: true, vertical: false } ) |
| 69 | + ); |
| 70 | + |
| 71 | + for ( let i = 0; i < 10 && controller.hasUndo; i++ ) { |
| 72 | + act( () => controller.undo() ); |
| 73 | + } |
| 74 | + act( () => controller.redo() ); |
| 75 | + |
| 76 | + expect( controller.hasRedo ).toBe( true ); |
| 77 | + } ); |
| 78 | +} ); |
0 commit comments