Skip to content

Instantly share code, notes, and snippets.

@delwar2016
Created January 16, 2017 13:35
Show Gist options
  • Save delwar2016/0dcb4bec69c3892e77d5bd4a916fb8ac to your computer and use it in GitHub Desktop.
Save delwar2016/0dcb4bec69c3892e77d5bd4a916fb8ac to your computer and use it in GitHub Desktop.
How to stub jQuery remove using sinonJS
Suppose we have following situation
$('#dom1').remove();
$('#dom2').remove();
$('#dom3').remove();
$('#dom4').remove();
So, we can write unit test as follows:
this.removeStub = sinon.stub($.fn, 'remove');
it('Should remove dom', function(){
expect(this.removeStub.callCount).to.equal(4);
expect(this.removeStub.thisValues[0].selector).to.be.equal('#dom1');
expect(this.removeStub.thisValues[1].selector).to.be.equal('#dom2');
expect(this.removeStub.thisValues[2].selector).to.be.equal('#dom3');
expect(this.removeStub.thisValues[3].selector).to.be.equal('#dom4');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment