Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing DOM manipulating in Jasmine test

Tags:

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?)?

like image 926
Sławosz Avatar asked Oct 06 '11 09:10

Sławosz


People also ask

How is DOM tested with jest?

Testing DOM manipulation code using jQuery to trigger DOM events. Using Jest globals like beforeAll , beforeEach , it , and describe. Using expect with matchers like toEqual , toBe , and toHaveBeenCalled. Mock Functions API to spy on functions called by the code being tested.


1 Answers

For setting up HTML fixtures in my specs, I wrote jasmine-fixture. With it, you can do stuff like this:

var $foo, $input; beforeEach(function(){   $foo = affix('.foo');     # appends to the DOM <div class="foo"></div>    $input = $foo.affix('input[id="name"][value="Jim"]');     # appends <input id="name" value="Jim"/> beneath the .foo div 

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:

it('is named Jim', function(){   expect($input).toHaveValue("Jim"); }); 
like image 64
Justin Searls Avatar answered Oct 04 '22 21:10

Justin Searls