@@ -89,17 +89,28 @@ describe('LazyImage Component', () => {
8989
9090 it ( 'handles load event' , async ( ) => {
9191 const handleLoad = jest . fn ( )
92- render ( < LazyImage { ...defaultProps } onLoad = { handleLoad } /> )
93-
94- const image = screen . getByAltText ( 'Test image' )
95-
96- // Simulate image load
97- Object . defineProperty ( image , 'complete' , { value : true } )
98- image . dispatchEvent ( new Event ( 'load' ) )
99-
100- await waitFor ( ( ) => {
101- expect ( handleLoad ) . toHaveBeenCalled ( )
102- } )
92+ const OriginalImage = global . Image
93+ // jsdom does not finish decoding remote URLs; defer onload until after handlers attach (matches browser ordering).
94+ global . Image = class MockImage {
95+ constructor ( ) {
96+ this . onload = null
97+ }
98+
99+ set src ( _val ) {
100+ queueMicrotask ( ( ) => {
101+ if ( this . onload ) this . onload ( )
102+ } )
103+ }
104+ }
105+
106+ try {
107+ render ( < LazyImage { ...defaultProps } priority onLoad = { handleLoad } /> )
108+ await waitFor ( ( ) => {
109+ expect ( handleLoad ) . toHaveBeenCalled ( )
110+ } )
111+ } finally {
112+ global . Image = OriginalImage
113+ }
103114 } )
104115
105116 it ( 'handles error gracefully' , ( ) => {
@@ -122,10 +133,9 @@ describe('LazyImage Component', () => {
122133 } )
123134
124135 it ( 'handles missing src gracefully' , ( ) => {
125- render ( < LazyImage alt = "Test image" /> )
126-
127- const image = screen . getByAltText ( 'Test image' )
128- expect ( image ) . toBeInTheDocument ( )
136+ const { container } = render ( < LazyImage alt = "Test image" /> )
137+
138+ expect ( container . firstChild ) . toBeNull ( )
129139 } )
130140
131141 it ( 'applies custom styles' , ( ) => {
0 commit comments