Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unit test a document.ready() function using jasmine

How do you test document.ready block using Jasmine? To be more specific , if I have a block like this :

$(document).ready(
    function () {
        abc = true;
    }
});

How do you test that the inner function was called when the document was ready, using Jasmine?

like image 992
ash123 Avatar asked Mar 16 '23 16:03

ash123


2 Answers

You could refactor your code to be something like this:

var onReady = function(){
    abc = true;
}

$(document).ready(onReady);

and your tests:

it("Tests abc", function() {
    onReady() ;
    expect(tesabctVar).toEqual(true);
});
like image 59
Elena Avatar answered Mar 24 '23 22:03

Elena


How do you test document.ready block using Jasmine? To be more specific , > if I have a block like this :

$(document).ready( function(){ abc= true; } );

My understanding is that code you have written within $(document).ready closure above is not testable. This link has a good explanation of how to make it more testable : http://bittersweetryan.github.io/jasmine-presentation/#slide-17

How do you test that the inner function was called when the document was ready, using Jasmine?

Answered above by m59 in comment already.

like image 32
webcodervk Avatar answered Mar 24 '23 22:03

webcodervk