Priyanker Rao
Priyanker Rao

Reputation: 83

How to check if jQuery, $ is called with a specific selector in Jasmine?

My Code:

$('#test1').val('string1');

I'm able to test if val has been called with 'string1'.

spyOn($.fn, 'val');

expect($.fn.val).toHaveBeenCalledWith('string1');

But I want to test if $ has been called with a specific selector. I want to be able to do this.

expect($.fn).toHaveBeenCalledWith('#test1');

Upvotes: 4

Views: 261

Answers (1)

Louys Patrice Bessette
Louys Patrice Bessette

Reputation: 33933

Looking at the jQuery script (version 3.5.1), I found that the function that handles the selector is $.fn.init

 jQuery.fn.init = function( selector, context, root ) {...}

That is at line # 3133.

So your Jasmine expect would be:

expect($.fn.init).toHaveBeenCalledWith('#test1', undefined, jasmine.anything());

You can ignore some arguments. Reference

Upvotes: 2

Related Questions