1- import { describe , expect , it } from 'vitest' ;
1+ import { describe , expect , it , vi } from 'vitest' ;
22import { render , screen , waitFor } from '@testing-library/react' ;
33import userEvent from '@testing-library/user-event' ;
44import { useState } from '@wordpress/element' ;
55import { SlotFillProvider } from '@wordpress/components' ;
6+ import { dispatch } from '@wordpress/data' ;
67import MediaReplaceFlow from '../' ;
8+ import { store as blockEditorStore } from '../../../store' ;
79
810globalThis . wpVitest . mockMatchMedia ( ) ;
911
1012const 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