Decrypter
Decrypter

Reputation: 3000

Sinon JQuery selectors mock

I am trying to test a function using mocha/sinonjs. The function I want to test is responsible for showing or hiding some element in my DOM.

This is my function

var updateUI = function() {
  $('#login').show();
  $('#logout').hide();
};

I tried to mock using sinon but I'm not sure if it is possible or the correct thing to do in this case.

This is what I have tried but I keep getting an error "TypeError: undefined is not a function" during the expect call

 var mockLogin = sinon.mock($);
 mockLogin.withArgs('#login').expects("show").once();

I simple want to test my 2 jquery calls have been called. I tried to use spies but continue to get exceptions

Upvotes: 0

Views: 3188

Answers (1)

Decrypter
Decrypter

Reputation: 3000

Looking further into the sinon doc I found that the following worked for me.

var jQueryShow = sinon.stub($.fn, 'show');
var jQueryHide = sinon.stub($.fn, 'hide');

jQueryShow.callCount.should.be.equal(1);
jQueryShow.thisValues[0].selector.should.be.equal("#login");

jQueryHide.callCount.should.be.equal(1);
jQueryHide.thisValues[0].selector.should.be.equal("#logout");

I'm not sure if there an easier way but it checks for the selectors I need

Upvotes: 3

Related Questions