Skip to content

Commit 7d10faf

Browse files
committed
Fix undefined exports issue
1 parent 236c4af commit 7d10faf

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/react-script-hook/index.test.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff 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
});

src/react-script-hook/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)