-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(@toss/utils): Add new test for loadScript (#432)
- Loading branch information
Showing
1 changed file
with
26 additions
and
0 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,26 @@ | ||
import * as deviceModule from './device/isServer'; | ||
import { loadScript } from './loadScript'; | ||
|
||
describe('loadScript', () => { | ||
it('should immediately resolve with a Promise in a server environment', async () => { | ||
jest.spyOn(deviceModule, 'isServer').mockReturnValue(true); | ||
await expect(loadScript('test-script.js')).resolves.toBeUndefined(); | ||
}); | ||
|
||
it('should immediately resolve with a Promise if the script is already loaded or loading', async () => { | ||
jest.spyOn(deviceModule, 'isServer').mockReturnValue(false); | ||
const script = document.createElement('script'); | ||
script.src = 'test-script.js'; | ||
document.body.appendChild(script); | ||
await expect(loadScript('test-script.js')).resolves.toBeUndefined(); | ||
}); | ||
|
||
it('should append a script element to the document body and resolve the Promise on load event', () => { | ||
jest.spyOn(deviceModule, 'isServer').mockReturnValue(false); | ||
const source = 'https://example.com/script.js'; | ||
loadScript(source); | ||
|
||
const scriptElement = document.querySelector(`script[src="${source}"]`); | ||
expect(scriptElement).toBeTruthy(); | ||
}); | ||
}); |