Skip to content

Commit 238dc24

Browse files
authored
tests/integration: use asyncLoadScript() (#8751)
Replace duplicated script-loading code with existing `commonUtils.asyncLoadScript()` function.
1 parent aa6d500 commit 238dc24

3 files changed

Lines changed: 7 additions & 21 deletions

File tree

‎tests/common-utils.js‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,14 @@ commonUtils.loadPouchDBForBrowser = function (plugins) {
120120

121121
// Thanks to http://engineeredweb.com/blog/simple-async-javascript-loader/
122122
commonUtils.asyncLoadScript = function (url) {
123-
return new commonUtils.Promise(function (resolve) {
123+
return new commonUtils.Promise(function (resolve, reject) {
124124
// Create a new script and setup the basics.
125125
var script = document.createElement("script");
126-
var firstScript = document.getElementsByTagName('script')[0];
127126

128127
script.async = true;
129128
script.src = url;
130129

130+
script.onerror = reject;
131131
script.onload = function () {
132132
resolve();
133133

@@ -141,9 +141,7 @@ commonUtils.asyncLoadScript = function (url) {
141141
}
142142
};
143143

144-
// Attach the script tag to the page (before the first script) so the
145-
//magic can happen.
146-
firstScript.parentNode.insertBefore(script, firstScript);
144+
document.body.append(script);
147145
});
148146
};
149147

‎tests/integration/browser.migration.js‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,7 @@ describe('migration', function () {
4343
if (!match) {
4444
return testUtils.Promise.resolve();
4545
}
46-
return new testUtils.Promise(function (resolve, reject) {
47-
var script = document.createElement('script');
48-
script.onload = resolve;
49-
script.onerror = reject;
50-
script.src = 'deps/pouchdb-' + match[1] + '-postfixed.js';
51-
document.body.appendChild(script);
52-
});
46+
return testUtils.asyncLoadScript('deps/pouchdb-' + match[1] + '-postfixed.js');
5347
}));
5448
});
5549

‎tests/integration/test.attachments.js‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3113,15 +3113,9 @@ adapters.forEach(function (adapter) {
31133113
var bigimage = require('./deps/bigimage.js');
31143114
cb(null, bigimage);
31153115
} else { // browser
3116-
var script = document.createElement('script');
3117-
script.src = 'deps/bigimage.js';
3118-
document.body.appendChild(script);
3119-
var timeout = setInterval(function () {
3120-
if (window.bigimage) {
3121-
clearInterval(timeout);
3122-
cb(null, window.bigimage);
3123-
}
3124-
}, 500);
3116+
testUtils.asyncLoadScript('deps/bigimage.js')
3117+
.then(() => cb(null, window.bigimage))
3118+
.catch(err => cb(err));
31253119
}
31263120
}
31273121

0 commit comments

Comments
 (0)