I'm creating a js widget and first part is to add script width javascript, something like this (example from google analytics):
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
How to test it with jasmine (using fixtures?)?
I suppose you could perform your action, then check the href on the first script tag like so:
Jasmine spec:
Would be interested in hearing a better method, though. Checking the DOM with Jasmine always feels a little hacky.
For heavy DOM testing with Jasmine you can use Jasmine Headless WebKit.
For setting up HTML fixtures in my specs, I wrote jasmine-fixture. With it, you can do stuff like this:
And afterEach, it'll clean up after you.
For expectations about the state of the DOM, I use jasmine-jquery. It offers a ton of matchers like the one below: