@@ -24,6 +24,10 @@ let images = [];
2424let onLoadSpy = null ;
2525let actualLoadSpy = null ;
2626
27+ let waitForAll ;
28+ let waitFor ;
29+ let assertLog ;
30+
2731function PhaseMarkers ( { children} ) {
2832 Scheduler . unstable_yieldValue ( 'render start' ) ;
2933 React . useLayoutEffect ( ( ) => {
@@ -94,6 +98,11 @@ describe('ReactDOMImageLoad', () => {
9498 ReactDOMClient = require ( 'react-dom/client' ) ;
9599 // Suspense = React.Suspense;
96100
101+ const InternalTestUtils = require ( 'internal-test-utils' ) ;
102+ waitForAll = InternalTestUtils . waitForAll ;
103+ waitFor = InternalTestUtils . waitFor ;
104+ assertLog = InternalTestUtils . assertLog ;
105+
97106 onLoadSpy = jest . fn ( reactEvent => {
98107 const src = reactEvent . target . getAttribute ( 'src' ) ;
99108 Scheduler . unstable_yieldValue ( 'onLoadSpy [' + src + ']' ) ;
@@ -206,26 +215,17 @@ describe('ReactDOMImageLoad', () => {
206215 ) ,
207216 ) ;
208217
209- expect ( Scheduler ) . toFlushAndYieldThrough ( [
210- 'render start' ,
211- 'Img default' ,
212- 'Yield' ,
213- ] ) ;
218+ await waitFor ( [ 'render start' , 'Img default' , 'Yield' ] ) ;
214219 const img = last ( images ) ;
215220 loadImage ( img ) ;
216- expect ( Scheduler ) . toHaveYielded ( [
221+ assertLog ( [
217222 'actualLoadSpy [default]' ,
218223 // no onLoadSpy since we have not completed render
219224 ] ) ;
220- expect ( Scheduler ) . toFlushAndYield ( [
221- 'a' ,
222- 'load triggered' ,
223- 'last layout' ,
224- 'last passive' ,
225- ] ) ;
225+ await waitForAll ( [ 'a' , 'load triggered' , 'last layout' , 'last passive' ] ) ;
226226 expect ( img . __needsDispatch ) . toBe ( true ) ;
227227 loadImage ( img ) ;
228- expect ( Scheduler ) . toHaveYielded ( [
228+ assertLog ( [
229229 'actualLoadSpy [default]' , // the browser reloading of the image causes this to yield again
230230 'onLoadSpy [default]' ,
231231 ] ) ;
@@ -244,7 +244,7 @@ describe('ReactDOMImageLoad', () => {
244244 ) ,
245245 ) ;
246246
247- expect ( Scheduler ) . toFlushAndYieldThrough ( [
247+ await waitFor ( [
248248 'render start' ,
249249 'Img default' ,
250250 'load triggered' ,
@@ -253,11 +253,8 @@ describe('ReactDOMImageLoad', () => {
253253 Scheduler . unstable_requestPaint ( ) ;
254254 const img = last ( images ) ;
255255 loadImage ( img ) ;
256- expect ( Scheduler ) . toHaveYielded ( [
257- 'actualLoadSpy [default]' ,
258- 'onLoadSpy [default]' ,
259- ] ) ;
260- expect ( Scheduler ) . toFlushAndYield ( [ 'last passive' ] ) ;
256+ assertLog ( [ 'actualLoadSpy [default]' , 'onLoadSpy [default]' ] ) ;
257+ await waitForAll ( [ 'last passive' ] ) ;
261258 expect ( img . __needsDispatch ) . toBe ( false ) ;
262259 expect ( onLoadSpy ) . toHaveBeenCalledTimes ( 1 ) ;
263260 } ) ;
@@ -286,16 +283,12 @@ describe('ReactDOMImageLoad', () => {
286283
287284 React . startTransition ( ( ) => root . render ( < Base /> ) ) ;
288285
289- expect ( Scheduler ) . toFlushAndYieldThrough ( [
290- 'render start' ,
291- 'Img a' ,
292- 'Yield' ,
293- ] ) ;
286+ await waitFor ( [ 'render start' , 'Img a' , 'Yield' ] ) ;
294287 const img = last ( images ) ;
295288 loadImage ( img ) ;
296- expect ( Scheduler ) . toHaveYielded ( [ 'actualLoadSpy [a]' ] ) ;
289+ assertLog ( [ 'actualLoadSpy [a]' ] ) ;
297290
298- expect ( Scheduler ) . toFlushAndYieldThrough ( [
291+ await waitFor ( [
299292 'load triggered' ,
300293 'last layout' ,
301294 // the update in layout causes a passive effects flush before a sync render
@@ -309,7 +302,7 @@ describe('ReactDOMImageLoad', () => {
309302 ] ) ;
310303 expect ( images . length ) . toBe ( 1 ) ;
311304 loadImage ( img ) ;
312- expect ( Scheduler ) . toHaveYielded ( [ 'actualLoadSpy [b]' , 'onLoadSpy [b]' ] ) ;
305+ assertLog ( [ 'actualLoadSpy [b]' , 'onLoadSpy [b]' ] ) ;
313306 expect ( onLoadSpy ) . toHaveBeenCalledTimes ( 1 ) ;
314307 } ) ;
315308
@@ -323,7 +316,7 @@ describe('ReactDOMImageLoad', () => {
323316 </ PhaseMarkers > ,
324317 ) ;
325318
326- expect ( Scheduler ) . toFlushAndYield ( [
319+ await waitForAll ( [
327320 'render start' ,
328321 'Img default' ,
329322 'load triggered' ,
@@ -332,10 +325,7 @@ describe('ReactDOMImageLoad', () => {
332325 ] ) ;
333326 const img = last ( images ) ;
334327 loadImage ( img ) ;
335- expect ( Scheduler ) . toHaveYielded ( [
336- 'actualLoadSpy [default]' ,
337- 'onLoadSpy [default]' ,
338- ] ) ;
328+ assertLog ( [ 'actualLoadSpy [default]' , 'onLoadSpy [default]' ] ) ;
339329 expect ( onLoadSpy ) . toHaveBeenCalledTimes ( 1 ) ;
340330 } ) ;
341331
@@ -365,26 +355,17 @@ describe('ReactDOMImageLoad', () => {
365355 ) ,
366356 ) ;
367357
368- expect ( Scheduler ) . toFlushAndYieldThrough ( [
369- 'render start' ,
370- 'Img default' ,
371- 'Yield' ,
372- ] ) ;
358+ await waitFor ( [ 'render start' , 'Img default' , 'Yield' ] ) ;
373359 const img = last ( images ) ;
374360 loadImage ( img ) ;
375- expect ( Scheduler ) . toHaveYielded ( [ 'actualLoadSpy [default]' ] ) ;
376- expect ( Scheduler ) . toFlushAndYield ( [
377- 'a' ,
378- 'load triggered' ,
379- 'last layout' ,
380- 'last passive' ,
381- ] ) ;
361+ assertLog ( [ 'actualLoadSpy [default]' ] ) ;
362+ await waitForAll ( [ 'a' , 'load triggered' , 'last layout' , 'last passive' ] ) ;
382363 expect ( img . __needsDispatch ) . toBe ( true ) ;
383364 loadImage ( img ) ;
384365 // we expect the browser to load the image again but since we are no longer rendering
385366 // the img there will be no onLoad called
386- expect ( Scheduler ) . toHaveYielded ( [ 'actualLoadSpy [default]' ] ) ;
387- expect ( Scheduler ) . toFlushWithoutYielding ( ) ;
367+ assertLog ( [ 'actualLoadSpy [default]' ] ) ;
368+ await waitForAll ( [ ] ) ;
388369 expect ( onLoadSpy ) . not . toHaveBeenCalled ( ) ;
389370 } ) ;
390371
@@ -426,16 +407,16 @@ describe('ReactDOMImageLoad', () => {
426407 ) ,
427408 ) ;
428409
429- expect ( Scheduler ) . toFlushAndYieldThrough ( [
410+ await waitFor ( [
430411 // initial render
431412 'render start' ,
432413 'Img default' ,
433414 'Yield' ,
434415 ] ) ;
435416 const img = last ( images ) ;
436417 loadImage ( img ) ;
437- expect ( Scheduler ) . toHaveYielded ( [ 'actualLoadSpy [default]' ] ) ;
438- expect ( Scheduler ) . toFlushAndYield ( [
418+ assertLog ( [ 'actualLoadSpy [default]' ] ) ;
419+ await waitForAll ( [
439420 'a' ,
440421 'load triggered' ,
441422 // img is present at first
@@ -450,8 +431,8 @@ describe('ReactDOMImageLoad', () => {
450431 loadImage ( img ) ;
451432 // we expect the browser to load the image again but since we are no longer rendering
452433 // the img there will be no onLoad called
453- expect ( Scheduler ) . toHaveYielded ( [ 'actualLoadSpy [default]' ] ) ;
454- expect ( Scheduler ) . toFlushWithoutYielding ( ) ;
434+ assertLog ( [ 'actualLoadSpy [default]' ] ) ;
435+ await waitForAll ( [ ] ) ;
455436 expect ( onLoadSpy ) . not . toHaveBeenCalled ( ) ;
456437 } ) ;
457438
@@ -548,22 +529,18 @@ describe('ReactDOMImageLoad', () => {
548529
549530 root . render ( < Base /> ) ;
550531
551- expect ( Scheduler ) . toFlushWithoutYielding ( ) ;
532+ await waitForAll ( [ ] ) ;
552533
553534 React . startTransition ( ( ) => externalSetSrc ( 'a' ) ) ;
554535
555- expect ( Scheduler ) . toFlushAndYieldThrough ( [
556- 'YieldingWithImage' ,
557- 'Img a' ,
558- 'Yield' ,
559- ] ) ;
536+ await waitFor ( [ 'YieldingWithImage' , 'Img a' , 'Yield' ] ) ;
560537 let img = last ( images ) ;
561538 loadImage ( img ) ;
562- expect ( Scheduler ) . toHaveYielded ( [ 'actualLoadSpy [a]' ] ) ;
539+ assertLog ( [ 'actualLoadSpy [a]' ] ) ;
563540
564541 ReactDOM . flushSync ( ( ) => externalSetSrcAlt ( 'b' ) ) ;
565542
566- expect ( Scheduler ) . toHaveYielded ( [
543+ assertLog ( [
567544 'YieldingWithImage' ,
568545 'Img b' ,
569546 'Yield' ,
@@ -576,18 +553,12 @@ describe('ReactDOMImageLoad', () => {
576553 expect ( img . __needsDispatch ) . toBe ( true ) ;
577554 loadImage ( img ) ;
578555
579- expect ( Scheduler ) . toHaveYielded ( [ 'actualLoadSpy [b]' , 'onLoadSpy [b]' ] ) ;
556+ assertLog ( [ 'actualLoadSpy [b]' , 'onLoadSpy [b]' ] ) ;
580557 // why is there another update here?
581- expect ( Scheduler ) . toFlushAndYield ( [
582- 'YieldingWithImage' ,
583- 'Img b' ,
584- 'Yield' ,
585- 'b' ,
586- 'Committed' ,
587- ] ) ;
558+ await waitForAll ( [ 'YieldingWithImage' , 'Img b' , 'Yield' , 'b' , 'Committed' ] ) ;
588559 } ) ;
589560
590- it ( 'preserves the src property / attribute when triggering a potential new load event' , ( ) => {
561+ it ( 'preserves the src property / attribute when triggering a potential new load event' , async ( ) => {
591562 // this test covers a regression identified in https://github.com/mui/material-ui/pull/31263
592563 // where the resetting of the src property caused the property to change from relative to fully qualified
593564
@@ -612,17 +583,13 @@ describe('ReactDOMImageLoad', () => {
612583 ) ;
613584
614585 // render to yield to capture state of img src attribute and property before commit
615- expect ( Scheduler ) . toFlushAndYieldThrough ( [
616- 'render start' ,
617- 'Img default' ,
618- 'Yield' ,
619- ] ) ;
586+ await waitFor ( [ 'render start' , 'Img default' , 'Yield' ] ) ;
620587 const img = last ( images ) ;
621588 const renderSrcProperty = img . src ;
622589 const renderSrcAttr = img . getAttribute ( 'src' ) ;
623590
624591 // finish render and commit causing the src property to be rewritten
625- expect ( Scheduler ) . toFlushAndYield ( [ 'a' , 'last layout' , 'last passive' ] ) ;
592+ await waitForAll ( [ 'a' , 'last layout' , 'last passive' ] ) ;
626593 const commitSrcProperty = img . src ;
627594 const commitSrcAttr = img . getAttribute ( 'src' ) ;
628595
0 commit comments