Skip to content

Commit

Permalink
improved some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dannystey committed Dec 19, 2018
1 parent d0344e8 commit 579bf36
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 13 deletions.
23 changes: 16 additions & 7 deletions lib/loadmanager.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/loadmanager.js.map

Large diffs are not rendered by default.

19 changes: 14 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,17 @@ class loadManager {
scripts.forEach((script) => this.addToDom(script));

let counter = 0;
let checkComplete = () => {
let checkComplete = (script) => {
if(script.onRequest) return;

counter++;

if(counter >= scripts.length) {
this.emit('complete');
}
}
this.on('loaded', () => checkComplete())
this.on('error', () => checkComplete())
this.on('loaded', (script) => checkComplete(script))
this.on('error', (script) => checkComplete(script))
}

addToDom(data) {
Expand Down Expand Up @@ -133,16 +135,22 @@ class loadManager {
if(script.loaded) {
resolve()
}
else {
else if(script.onRequest) {
// onRequest
// check if the script is allowed to be loaded
if(script.onRequest) {
let level = this.getLevel();
if(level >= script.level) {
this.addToDom(script);
}
this.on('loaded', (script) => {
if(script.key == key) {
resolve();
}
})
}

}
else {
this.scriptListener[key] = this.scriptListener[key] || [];
this.scriptListener[key].push(resolve);
}
Expand All @@ -164,6 +172,7 @@ class loadManager {
for(let key in this.scriptListener) {
this.scriptListener[key].forEach((callback) => callback());
}
this.scriptListener = {};
}

// EVENT HANDLING
Expand Down
18 changes: 18 additions & 0 deletions test/loadTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,23 @@ export default function (scripts) {
manager.load(1);
});
});

describe('#whenever (on request)', function() {
it('should load the script and execute afterwards without error', function(done) {
var manager = new loadManager;


scripts[0].onRequest = true;

// set scripts
manager.setScripts(scripts);

manager.whenever('cdn-jquery-1').then(() => {
done();
});

manager.load(1);
});
});
});
}

0 comments on commit 579bf36

Please sign in to comment.