Skip to content
This repository has been archived by the owner on Oct 29, 2024. It is now read-only.

Commit

Permalink
Use fetchScriptData hook
Browse files Browse the repository at this point in the history
  • Loading branch information
volkanceylan committed Oct 14, 2024
1 parent 3a6c7ba commit a93bf25
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions build/test-utils/mocks.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
import { ScriptData, peekScriptData, setScriptData } from "@serenity-is/corelib";
import { ScriptData, peekScriptData, scriptDataHooks, setScriptData } from "@serenity-is/corelib";

let orgEnsure: any;
let orgFetchScriptData: any;

export function mockDynamicData() {

if (orgEnsure)
if (orgFetchScriptData != void 0)
return;

orgEnsure = ScriptData.ensure;
ScriptData.ensure = function(name: string, dynJS?: boolean) {

if (peekScriptData(name) == null) {
try {
var data = jest.requireActual("test-utils/dynamic-data/" + name + ".json");
setScriptData(name, data);
}
catch(e) {
console.warn(e);
}
orgFetchScriptData = scriptDataHooks.fetchScriptData ?? null;
scriptDataHooks.fetchScriptData = <TData>(name: string) => {
try {
return jest.requireActual("test-utils/dynamic-data/" + name + ".json");
}
catch (e) {
console.warn("Failed to load mock dynamic data for: " + name);
}

return orgEnsure(name, dynJS);
}
}

export function unmockDynamicData() {
if (!orgEnsure)
if (!orgFetchScriptData)
return;

ScriptData.ensure = orgEnsure;
scriptDataHooks.fetchScriptData = orgFetchScriptData == null ? void 0 : orgFetchScriptData;
}

import { resolveServiceUrl } from "@serenity-is/corelib";
Expand Down Expand Up @@ -147,7 +141,7 @@ class MockXHR {
getResponseHeader(name: string): string {
return this._info?.responseHeaders[name];
}

open(_method: string, url: string, _async?: boolean): void {
this._info ??= {} as any;
this._info.url = url;
Expand Down Expand Up @@ -183,7 +177,7 @@ class MockXHR {

this._responseData = callback(this._info);
}

setRequestHeader(name: string, value: string): void {
}
}
Expand Down

0 comments on commit a93bf25

Please sign in to comment.