Skip to content

Commit e1ada53

Browse files
committed
test: add beforeLoad test
1 parent 2928ff3 commit e1ada53

1 file changed

Lines changed: 32 additions & 1 deletion

File tree

packages/repack/src/modules/ScriptManager/__tests__/ScriptManagerHooks.test.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ describe('ScriptManager hooks', () => {
164164
);
165165
});
166166

167-
it('should allow beforeResolve hook to override resolution args', async () => {
167+
it('should allow beforeResolve hook to override options', async () => {
168168
ScriptManager.shared.hooks.beforeResolve(async ({ options }) => {
169169
return {
170170
options: {
@@ -353,6 +353,37 @@ describe('ScriptManager hooks', () => {
353353
).resolves.not.toThrow();
354354
});
355355

356+
it('should allow beforeLoad hook to override script', async () => {
357+
const spy = jest.spyOn(NativeScriptManager, 'loadScript');
358+
359+
ScriptManager.shared.hooks.beforeLoad(async ({ script, options }) => {
360+
script.locator.url = 'http://domain.ext/custom-script.js';
361+
362+
return {
363+
script,
364+
options: {
365+
...options,
366+
scriptId: 'custom-script',
367+
caller: 'custom-caller',
368+
},
369+
};
370+
});
371+
372+
ScriptManager.shared.addResolver(async () => {
373+
return { url: Script.getRemoteURL('http://domain.ext/script') };
374+
});
375+
376+
await ScriptManager.shared.loadScript(
377+
'original-script',
378+
'original-caller'
379+
);
380+
381+
expect(spy).toHaveBeenCalledWith(
382+
'custom-script',
383+
expect.objectContaining({ url: 'http://domain.ext/custom-script.js' })
384+
);
385+
});
386+
356387
it('should allow load hook to handle loading with custom logic', async () => {
357388
const spy = jest.spyOn(NativeScriptManager, 'loadScript');
358389

0 commit comments

Comments
 (0)