Skip to content

Commit

Permalink
feat(common): PAYPAL-7 Pass in merchant ID on PayPal button script fo…
Browse files Browse the repository at this point in the history
…r PayPal Express Checkout
  • Loading branch information
bc-fetisov committed Oct 3, 2019
1 parent de482de commit fe07c6e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/script-loader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ describe('ScriptLoader', () => {
expect(document.body.appendChild)
.toHaveBeenCalledTimes(1);
});

it('attaches script tag to document with data attributes', async () => {
await loader.loadScript(
'https://code.jquery.com/jquery-3.2.1.min.js',
{'data-attribute1': '1', 'data-attribute2': '2'});

expect(script.attributes.getNamedItem('data-attribute1')!.value)
.toEqual('1');

expect(script.attributes.getNamedItem('data-attribute2')!.value)
.toEqual('2');
});
});

describe('when script fails to load', () => {
Expand Down
12 changes: 11 additions & 1 deletion src/script-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export interface PreloadScriptOptions {
prefetch: boolean;
}

export interface ScriptAttributes {
[key: string]: string;
}

export default class ScriptLoader {
private _scripts: { [key: string]: Promise<void> } = {};
private _preloadedScripts: { [key: string]: Promise<void> } = {};
Expand All @@ -22,12 +26,18 @@ export default class ScriptLoader {
private _requestSender: RequestSender
) {}

loadScript(src: string, options?: LoadScriptOptions): Promise<void> {
loadScript(src: string, options?: LoadScriptOptions, scriptAttributes?: ScriptAttributes): Promise<void> {
if (!this._scripts[src]) {
this._scripts[src] = new Promise((resolve, reject) => {
const script = document.createElement('script') as LegacyHTMLScriptElement;
const { async = false } = options || {};

for (const key in scriptAttributes) {
if (scriptAttributes.hasOwnProperty(key)) {
script.setAttribute(key, scriptAttributes[key]);
}
}

script.onload = () => resolve();
script.onreadystatechange = () => resolve();
script.onerror = event => {
Expand Down

0 comments on commit fe07c6e

Please sign in to comment.