File tree Expand file tree Collapse file tree 2 files changed +23
-1
lines changed Expand file tree Collapse file tree 2 files changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -272,4 +272,26 @@ describe('useScript', () => {
272272 expect ( document . querySelectorAll ( 'script' ) . length ) . toBe ( 1 ) ;
273273 } ) ;
274274 } ) ;
275+
276+ it ( 'should remove script from DOM and scripts cache when unmounted during loading' , ( ) => {
277+ expect ( document . querySelectorAll ( 'script' ) . length ) . toBe ( 0 ) ;
278+ expect ( Object . keys ( scripts ) . length ) . toBe ( 0 ) ;
279+
280+ const testSrc = 'http://scriptsrc/test' ;
281+ const { unmount } = renderHook ( ( ) => useScript ( { src : testSrc } ) ) ;
282+
283+ // Verify script was added
284+ expect ( document . querySelectorAll ( 'script' ) . length ) . toBe ( 1 ) ;
285+ expect ( Object . keys ( scripts ) . length ) . toBe ( 1 ) ;
286+ expect ( scripts [ testSrc ] ) . toBeDefined ( ) ;
287+ expect ( scripts [ testSrc ] . loading ) . toBe ( true ) ;
288+
289+ // Unmount the component while script is still loading (before load event)
290+ unmount ( ) ;
291+
292+ // Verify script was removed from DOM and cache
293+ expect ( document . querySelectorAll ( 'script' ) . length ) . toBe ( 0 ) ;
294+ expect ( Object . keys ( scripts ) . length ) . toBe ( 0 ) ;
295+ expect ( scripts [ testSrc ] ) . toBeUndefined ( ) ;
296+ } ) ;
275297} ) ;
Original file line number Diff line number Diff line change @@ -131,7 +131,7 @@ export default function useScript({
131131 // but only applied when loading
132132 if ( status && status . loading ) {
133133 scriptEl . remove ( ) ;
134- delete exports . scripts [ src ] ;
134+ delete scripts [ src ] ;
135135 }
136136 } ;
137137 // we need to ignore the attributes as they're a new object per call, so we'd never skip an effect call
You can’t perform that action at this time.
0 commit comments