fm2ziw
Last Updated: July 29, 2016
·
4.304K
· mateuszgachowski
Avatar

jQuery chain testing with sinonJS

If you want to test something like this:

$('.selector').siblings('.siblings').toggleClass('hidden');

You can easily try this:

// Fake jQuery methods
var jQueryMethods = { toggleClass: empty, siblings: empty };

// First the methods that can be chained
sinon.stub(jQueryMethods, 'toggleClass').returns(jQueryMethods);
sinon.stub(jQueryMethods, 'siblings').returns(jQueryMethods);

// Then all jQuery function
sinon.stub(window, '$').returns(jQueryMethods);

And now you have full test-ready faked jQuery methods!

thanks for MS

Say Thanks
Respond

6 Responses
Add your response

7018
Picture ms

Well done mate, glad you posted it here!

over 1 year ago ·
7019
Avatar

Thanks to YOU!

over 1 year ago ·
8381
De3433443a31d85d30a5c87f313cbd3d

Very helpful!
But one thing: You shouldn't use "toggleClass", will you? Apart from it - .removeClass

over 1 year ago ·
8383
Avatar

I'm glad that it was helpful.
Just fixed! Thank you!

over 1 year ago ·
16368
Gravatar

Thanks for this! Saved me a big headache when I switched my Express response calls from the deprecated res.json(status, body) calls to the new res.status(status).json(body) format. Cheers!

over 1 year ago ·
16380
Avatar

Happy to help :)

over 1 year ago ·