I'm creating dynamically some scripts like this:
var js1 = document.createElement('script');
js1.type = 'text/javascript';
js1.src = '/js-1.js';
docmuent.body.appendChild(js1);
js1.onload = function() {
var js2 = document.createElement('script');
js2.type = 'text/javascript';
js2.src = '/js-2.js';
document.body.appendChild(js2);
};
Here's my spec:
it('Tests inject script', function() {
expect($("script[src*='/js-1.js']").length).toBeGreaterThan(0);
expect($("script[src*='/js-2.js']").length).toBeGreaterThan(0);
});
The test on js-2 always fails. How can i spy js-1 onload event ?
I think the problem may be in your ordering. You should create the
script
tag, add it to thebody
, set theonload
, and then set thesrc
.