Skip to content

Commit

Permalink
test(@toss/utils): Add new test for loadScript (#432)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaic4o authored Mar 22, 2024
1 parent d012c13 commit fb0d92d
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions packages/common/utils/src/loadScript.spec.ts
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();
});
});

0 comments on commit fb0d92d

Please sign in to comment.