Skip to content

Commit 890c8ad

Browse files
Merge pull request bigcommerce#9 from bc-fetisov/PAYPAL-7
feat(payments): PAYPAL-7 Pass in merchant ID on Pa…
2 parents de482de + bdfbcd5 commit 890c8ad

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/script-loader.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@ describe('ScriptLoader', () => {
7676
expect(document.body.appendChild)
7777
.toHaveBeenCalledTimes(1);
7878
});
79+
80+
it('attaches script tag to document with data attributes', async () => {
81+
await loader.loadScript(
82+
'https://code.jquery.com/jquery-3.2.1.min.js',
83+
{async: true, attributes: {'data-attribute1': '1', 'data-attribute2': '2'}});
84+
85+
expect(script.attributes.getNamedItem('data-attribute1')!.value)
86+
.toEqual('1');
87+
88+
expect(script.attributes.getNamedItem('data-attribute2')!.value)
89+
.toEqual('2');
90+
});
7991
});
8092

8193
describe('when script fails to load', () => {

src/script-loader.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@ import BrowserSupport from './browser-support';
44

55
export interface LoadScriptOptions {
66
async: boolean;
7+
attributes: ScriptAttributes;
78
}
89

910
export interface PreloadScriptOptions {
1011
prefetch: boolean;
1112
}
1213

14+
export interface ScriptAttributes {
15+
[key: string]: string;
16+
}
17+
1318
export default class ScriptLoader {
1419
private _scripts: { [key: string]: Promise<void> } = {};
1520
private _preloadedScripts: { [key: string]: Promise<void> } = {};
@@ -26,7 +31,12 @@ export default class ScriptLoader {
2631
if (!this._scripts[src]) {
2732
this._scripts[src] = new Promise((resolve, reject) => {
2833
const script = document.createElement('script') as LegacyHTMLScriptElement;
29-
const { async = false } = options || {};
34+
const { async = false, attributes = {} } = options || {};
35+
36+
Object.keys(attributes)
37+
.forEach(key => {
38+
script.setAttribute(key, attributes[key]);
39+
});
3040

3141
script.onload = () => resolve();
3242
script.onreadystatechange = () => resolve();

0 commit comments

Comments
 (0)