Skip to content

Commit

Permalink
fix: spyOn loadScript
Browse files Browse the repository at this point in the history
  • Loading branch information
hosseinmd committed Oct 16, 2024
1 parent 2d78e03 commit 55cce05
Showing 1 changed file with 34 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import NativeScriptManager from '../NativeScriptManager';
import NativeScriptManager, {
type NormalizedScriptLocator,
} from '../NativeScriptManager';
import { Script } from '../Script';
import { ScriptManager } from '../ScriptManager';

jest.mock('../NativeScriptManager', () => ({
loadScript: jest.fn((locator) =>
locator.fetch
? new Promise<void>((resolve) => {
setTimeout(() => resolve(), 10);
})
: Promise.resolve()
),
loadScript: jest.fn(),
prefetchScript: jest.fn(),
invalidateScripts: jest.fn(),
NormalizedScriptLocatorHTTPMethod: {
Expand Down Expand Up @@ -741,9 +737,11 @@ describe('ScriptManagerAPI', () => {
expect(setTimeout).toHaveBeenCalledTimes(2);
expect(NativeScriptManager.loadScript).toHaveBeenCalledTimes(3);
jest.useRealTimers();
})
});

it('should await loadScript with same scriptId to finish', async () => {
const spy = mockLoadScriptBasedOnFetch();

const cache = new FakeCache();
ScriptManager.shared.setStorage(cache);

Expand All @@ -763,9 +761,13 @@ describe('ScriptManagerAPI', () => {
await ScriptManager.shared.loadScript('miniApp');

expect(loadingScriptIsFinished).toEqual(true);
spy.mockReset();
spy.mockRestore();
});

it('should wait loadScript with same scriptId to finished in a complex scenario', async () => {
const spy = mockLoadScriptBasedOnFetch();

const cache = new FakeCache();
ScriptManager.shared.setStorage(cache);

Expand All @@ -787,7 +789,6 @@ describe('ScriptManagerAPI', () => {
ScriptManager.shared.loadScript('miniApp2').then(() => {
loadingScript2IsFinished = true;
});

await ScriptManager.shared.loadScript('miniApp');
expect(loadingScriptIsFinished).toEqual(true);

Expand All @@ -797,16 +798,18 @@ describe('ScriptManagerAPI', () => {
});

ScriptManager.shared.loadScript('miniApp2');

await ScriptManager.shared.loadScript('miniApp');

expect(loadingScriptIsFinished).toEqual(true);

await ScriptManager.shared.loadScript('miniApp2');
expect(loadingScript2IsFinished).toEqual(true);
spy.mockReset();
spy.mockRestore();
});

it('should wait loadScript and prefetchScript', async () => {
const spy = mockLoadScriptBasedOnFetch();

const cache = new FakeCache();
ScriptManager.shared.setStorage(cache);

Expand All @@ -823,9 +826,26 @@ describe('ScriptManagerAPI', () => {
ScriptManager.shared.prefetchScript('miniApp').then(() => {
prefetchScriptIsFinished = true;
});

await ScriptManager.shared.loadScript('miniApp');

expect(prefetchScriptIsFinished).toEqual(true);
spy.mockReset();
spy.mockRestore();
});
});

function mockLoadScriptBasedOnFetch() {
jest.useFakeTimers({ advanceTimers: true });
const spy = jest.spyOn(NativeScriptManager, 'loadScript');

spy.mockImplementation(
(_scriptId: string, scriptConfig: NormalizedScriptLocator) =>
scriptConfig.fetch
? new Promise<null>((resolve) => {
setTimeout(() => resolve(null), 10);
})
: Promise.resolve(null)
);

return spy;
}

0 comments on commit 55cce05

Please sign in to comment.