-
-
Notifications
You must be signed in to change notification settings - Fork 421
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: useScript failed to remove script from cache when passing `remov…
…eOnUnmount` prop (#354) * fix: useScript failed to remove script from cache when passing `removeOnUnmount` prop The `removeOnUnmount` prop didn't remove the script from the cache. This created a side effect where if you try and render the same component by mounting -> unmounting -> mounting again, the `useScript` hook behaves as the script is loaded when in reality is has remove the script from the tree but failed to remove it from the cache. * 🎨 Lint test file * ♻️ Use a Map for the cache to avoid `delete` syntax Prefer `cache.delete(key)` to `delete cache[key]` * 🔖 Add changeset --------- Co-authored-by: Julien <juliencaron@protonmail.com>
- Loading branch information
1 parent
87a5141
commit 4146c39
Showing
3 changed files
with
74 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'usehooks-ts': patch | ||
--- | ||
|
||
fix: `useScript` failed to remove script from cache when passing `removeOnUnmount` prop (#354 by @ShanSenanayake) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { act, cleanup, renderHook } from '@testing-library/react' | ||
|
||
import { useScript } from './useScript' | ||
|
||
describe('useScript', () => { | ||
it('should handle script loading error', () => { | ||
const src = 'https://example.com/myscript.js' | ||
|
||
const { result } = renderHook(() => useScript(src)) | ||
|
||
expect(result.current).toBe('loading') | ||
|
||
act(() => { | ||
// Simulate script error | ||
document | ||
.querySelector(`script[src="${src}"]`) | ||
?.dispatchEvent(new Event('error')) | ||
}) | ||
|
||
expect(result.current).toBe('error') | ||
}) | ||
|
||
it('should remove script on unmount', () => { | ||
const src = '/' | ||
|
||
// First load the script | ||
const { result } = renderHook(() => | ||
useScript(src, { removeOnUnmount: true }), | ||
) | ||
|
||
expect(result.current).toBe('loading') | ||
|
||
// Make sure the document is loaded | ||
act(() => { | ||
document | ||
.querySelector(`script[src="${src}"]`) | ||
?.dispatchEvent(new Event('load')) | ||
}) | ||
|
||
expect(result.current).toBe('ready') | ||
|
||
// Remove the hook by unmounting and cleaning up the hook | ||
cleanup() | ||
|
||
// Check if the script is removed from the DOM | ||
expect(document.querySelector(`script[src="${src}"]`)).toBeNull() | ||
|
||
// Try loading the script again | ||
const { result: result2 } = renderHook(() => | ||
useScript(src, { removeOnUnmount: true }), | ||
) | ||
|
||
expect(result2.current).toBe('loading') | ||
|
||
// Make sure the document is loaded | ||
act(() => { | ||
document | ||
.querySelector(`script[src="${src}"]`) | ||
?.dispatchEvent(new Event('load')) | ||
}) | ||
|
||
expect(result2.current).toBe('ready') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters