Skip to content

Commit

Permalink
put test file back since it's used in other repos
Browse files Browse the repository at this point in the history
  • Loading branch information
cgdibble committed Jan 5, 2024
1 parent f581829 commit 7de6f31
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* @flow */

import { extendUrl, getScript, memoize } from "@krakenjs/belter/src";
import { SDK_QUERY_KEYS } from "@paypal/sdk-constants/src";

import { getHost, getPath } from "./global";
import { getSDKScript, getSDKAttributes } from "./script";
import { setupLogger } from "./tracking";

type ScriptSettings = {|
query?: {
[string]: string,
},
attributes?: {
[string]: string,
},
|};

const DEFAULT_QUERY = {
[SDK_QUERY_KEYS.CLIENT_ID]: "abcxyz123",
};

const DEFAULT_ATTRIBUTES = {};

export function insertMockSDKScript({
query = DEFAULT_QUERY,
attributes = DEFAULT_ATTRIBUTES,
}: ScriptSettings = {}): string {
const scripts = document.querySelectorAll('script[type="test/javascript"]');

for (const script of scripts) {
if (script && script.parentNode) {
script.parentNode.removeChild(script);
}
}

// $FlowFixMe
delete getScript.__inline_memoize_cache__;
// $FlowFixMe
delete getSDKScript.__inline_memoize_cache__;
// $FlowFixMe
delete getSDKAttributes.__inline_memoize_cache__;

const script = document.createElement("script");
script.setAttribute("type", "test/javascript");
script.setAttribute("id", "test-sdk-script");

const url = extendUrl(`https://${getHost()}${getPath()}`, {
query: {
...DEFAULT_QUERY,
...query,
},
});

script.setAttribute("src", url);

for (const key of Object.keys(attributes)) {
script.setAttribute(key, attributes[key]);
}

if (!document.body) {
throw new Error(`No document body found`);
}

document.body.appendChild(script); // eslint-disable-line compat/compat
memoize.clear();
setupLogger();

return url;
}

0 comments on commit 7de6f31

Please sign in to comment.