I attempted to load multiple external JavaScript files using JavaScript. I had a separate code for the injection logic. When I loaded one JavaScript file, the test case worked fine. However, when I tried to load multiple JavaScript files, the test case FAILED.
Main.js
var externalJs = "abcd.js";
function loadJs() {
window.$script(externalJs);
}
function init(domElement) {
loadJs();
}
module.exports = {
init: init
};
Test.js
/* global assert, sinon*/
describe('Test', function () {
var factory = require('main.js');
it('load the correct JavaScript library', function(){
window.$script = sinon.spy();
factory.init();
sinon.assert.calledOnce(window.$script);
});
});
The above code works fine. However, when I attempt to load multiple external files, the test case fails.
Main.js
var externalJs = ["abcd.js", "xyz.js"];
function loadJs() {
window.$script(externalJs[0], function(){
window.$script(externalJs[1], function(){
});
});
}
function init(domElement) {
loadJs();
}
module.exports = {
init: init
};
Test.js
/* global assert, sinon*/
describe('Test', function () {
var factory = require('main.js');
it('load the correct JavaScript library', function(){
window.$script = sinon.spy();
factory.init();
sinon.assert.calledTwice(window.$script);
});
});
Error Details:
expected $script to be called twice but was called once
Any ideas on how to resolve this issue?