I tried to load multiple external js using javascript. I had separate code for injection logic. When I load one js file means the test case working fine. But when I try to load multiple js file 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('oad the correct js library', function(){
window.$script = sinon.spy();
factory.init();
sinon.assert.calledOnce(window.$script);
});
});
The above code working fine. But when i try to load multiple external file the test case failed.
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('oad the correct js 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
Have any idea to fix this issue.