{"id":8014,"date":"2015-11-21T03:44:26","date_gmt":"2015-11-21T03:44:26","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/11\/21\/spying-on-jquery-selectors-in-jasmine-open-source-projects-jasmine-jasmine\/"},"modified":"2022-08-30T15:03:04","modified_gmt":"2022-08-30T15:03:04","slug":"spying-on-jquery-selectors-in-jasmine-open-source-projects-jasmine-jasmine","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/11\/21\/spying-on-jquery-selectors-in-jasmine-open-source-projects-jasmine-jasmine\/","title":{"rendered":"Spying on JQuery Selectors in Jasmine-open source projects jasmine\/jasmine"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/99c6bbd7b1e330780e2c784bacf43432?s=128&amp;d=identicon&amp;r=PG\" \/> <strong>Roland W\u00fcrth<\/strong><\/p>\n<p>I wrote a helper-function, which accepts an array of id\/value-pairs.<\/p>\n<pre><code>var jasminTestHelper = {\n    spyOnValAndFake : function(obj) {\n        var i, j;\n        spyOn($.fn, 'val').andCallFake(function() {\n            for ( i = 0, j = obj.length; i &lt; j; i++) {\n                if (this.selector === '#' + obj[i][0]) {\n                    return obj[i][1];\n                }\n            }\n        })\n    }\n}\n<\/code><\/pre>\n<p>Each pair tells the faker-function for which id, which value should be returned if the jQuery-val()-function is called with the id-selector. It is used like this:<\/p>\n<pre><code>jasminTestHelper.spyOnValAndFake([[\"id1\", \"value1\"], [\"id2\", \"value2\"]]);\n<\/code><\/pre>\n<p>If <code>$('#id1').val()<\/code> is called in your function under test, the fake-function returns <code>value1<\/code>, if <code>$('#id2').val()<\/code> is called it returns <code>value2<\/code>. So you don&#8217;t need to fiddle with the DOM, you just mock the jQuery-val()-function and simulate return-values. Other jQuery-functions could probably mocked the same way.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Roland W\u00fcrth I wrote a helper-function, which accepts an array of id\/value-pairs. var jasminTestHelper = { spyOnValAndFake : function(obj) { var i, j; spyOn($.fn, &#8216;val&#8217;).andCallFake(function() { for ( i = 0, j = obj.length; i &lt; j; i++) { if (this.selector === &#8216;#&#8217; + obj[i][0]) { return obj[i][1]; } } }) } } Each pair [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8014","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8014","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=8014"}],"version-history":[{"count":1,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8014\/revisions"}],"predecessor-version":[{"id":8696,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8014\/revisions\/8696"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=8014"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=8014"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=8014"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}