From fb0d92d38839e038047d5520b4ad5684d9df463a Mon Sep 17 00:00:00 2001 From: Minsu <52266597+Gaic4o@users.noreply.github.com> Date: Fri, 22 Mar 2024 17:25:27 +0900 Subject: [PATCH] test(@toss/utils): Add new test for loadScript (#432) --- packages/common/utils/src/loadScript.spec.ts | 26 ++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 packages/common/utils/src/loadScript.spec.ts diff --git a/packages/common/utils/src/loadScript.spec.ts b/packages/common/utils/src/loadScript.spec.ts new file mode 100644 index 000000000..00324f52b --- /dev/null +++ b/packages/common/utils/src/loadScript.spec.ts @@ -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(); + }); +});