Skip to content

Commit 5cf81d5

Browse files
committed
Block Editor: Pass onSuccess callback to mediaUpload in MediaReplaceFlow
1 parent 1a6c8ab commit 5cf81d5

2 files changed

Lines changed: 45 additions & 3 deletions

File tree

‎packages/block-editor/src/components/media-replace-flow/index.jsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ const MediaReplaceFlow = ( {
124124
onFileChange: ( [ media ] ) => {
125125
selectMedia( media, closeMenu );
126126
},
127+
onSuccess: ( [ media ] ) => {
128+
selectMedia( media, closeMenu );
129+
},
127130
onError: onUploadError,
128131
} );
129132
};

‎packages/block-editor/src/components/media-replace-flow/test/index.jsdom.test.jsx‎

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
import { describe, expect, it } from 'vitest';
1+
import { describe, expect, it, vi } from 'vitest';
22
import { render, screen, waitFor } from '@testing-library/react';
33
import userEvent from '@testing-library/user-event';
44
import { useState } from '@wordpress/element';
55
import { SlotFillProvider } from '@wordpress/components';
6+
import { dispatch } from '@wordpress/data';
67
import MediaReplaceFlow from '../';
8+
import { store as blockEditorStore } from '../../../store';
79

810
globalThis.wpVitest.mockMatchMedia();
911

1012
const noop = () => {};
1113

12-
function TestWrapper() {
14+
function TestWrapper( { onSelect = noop, ...props } ) {
1315
const [ mediaURL, setMediaURL ] = useState( 'https://example.media' );
1416
return (
1517
<SlotFillProvider>
@@ -18,10 +20,11 @@ function TestWrapper() {
1820
mediaURL={ mediaURL }
1921
allowedTypes={ [ 'png' ] }
2022
accept="image/*"
21-
onSelect={ noop }
23+
onSelect={ onSelect }
2224
onSelectURL={ setMediaURL }
2325
onError={ noop }
2426
onCloseModal={ noop }
27+
{ ...props }
2528
/>
2629
</SlotFillProvider>
2730
);
@@ -124,4 +127,40 @@ describe( 'General media replace flow', () => {
124127
} )
125128
).toHaveAttribute( 'href', 'https://new.example.media' );
126129
} );
130+
131+
it( 'calls onSelect when mediaUpload completes via onSuccess', async () => {
132+
const user = userEvent.setup();
133+
const onSelect = vi.fn();
134+
const testMedia = {
135+
id: 42,
136+
url: 'https://example.com/updated-file.png',
137+
title: 'updated-file',
138+
};
139+
140+
dispatch( blockEditorStore ).updateSettings( {
141+
mediaUpload: vi.fn( ( { onFileChange, onSuccess } ) => {
142+
onFileChange( [ { url: 'blob:https://example.com/uuid' } ] );
143+
onSuccess( [ testMedia ] );
144+
} ),
145+
} );
146+
147+
render( <TestWrapper onSelect={ onSelect } /> );
148+
149+
await user.click(
150+
screen.getByRole( 'button', {
151+
expanded: false,
152+
name: 'Replace',
153+
} )
154+
);
155+
156+
const uploadInput = screen.getByTestId( 'form-file-upload-input' );
157+
const file = new File( [ 'sample' ], 'test.png', {
158+
type: 'image/png',
159+
} );
160+
await user.upload( uploadInput, file );
161+
162+
await waitFor( () => {
163+
expect( onSelect ).toHaveBeenLastCalledWith( testMedia );
164+
} );
165+
} );
127166
} );

0 commit comments

Comments
 (0)