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
Written by Mateusz Gachowski
Related protips
6 Responses
Well done mate, glad you posted it here!
Thanks to YOU!
Very helpful!
But one thing: You shouldn't use "toggleClass", will you? Apart from it - .removeClass
I'm glad that it was helpful.
Just fixed! Thank you!
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!
Happy to help :)
Have a fresh tip? Share with Coderwall community!
JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.
Security insights from Cside