Jasmine jQuery: Check if element is visible

Rotan075 picture Rotan075 · Jul 23, 2015 · Viewed 9.2k times · Source

Hello I got a question regarding unit testing with Jasmine (plugin: jQuery)

How could I test if the object is within the DOM of a document. The thing is that I use a tooltip function which will only be activated when an event is simulated. When there is a simulation of the effect, an object is attached to the DOM and I want to check if it is visible or not.

it("test 1: should invoke the Tooltip() function.", function () {                               
        spyEvent = spyOnEvent('.span_width', "mouseover");                  
        $('.span_width').simulate('mouseover');                         

        expect('mouseover').toHaveBeenTriggeredOn('.span_width');
        expect(spyEvent).toHaveBeenTriggered();                             

        # A TEST TO check if .tooltip is visible???
        # IN JQUERY would that be: $('.tooltip').is(':visible');                                                            
});

Answer

actaram picture actaram · Jul 23, 2015

You commented IN JQUERY would that be: $('.tooltip').is(':visible');

Yes it would. In jasmine unit testing, so it passes the test, you're expecting the above to be true:

expect($('.tooltip').is(':visible')).toBe(true); // Passes
expect($('.tooltip').is(':visible')).toBe(false); // Fails