{"id":24864,"date":"2015-08-03T01:39:56","date_gmt":"2015-08-02T20:09:56","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=24864"},"modified":"2015-11-19T15:34:05","modified_gmt":"2015-11-19T10:04:05","slug":"jasmine-testing-promises","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/jasmine-testing-promises\/","title":{"rendered":"Testing promises with Jasmine"},"content":{"rendered":"<p>We have been using <a title=\"Custom matcher for testing with Jasmine\" href=\"http:\/\/www.tothenew.com\/blog\/make-custom-matcher-for-testing-with-jasmine\/\">Jasmine to test our angular code<\/a>. Recently I came across a piece of code in Javascript which included a REST API call and returned a promise. The promise then had a success callback and all the rest of the play. So in this blog, I have tried to illustrate how to test a promise.<\/p>\n<p>Here I have a function which returns a promise and has a success handler and failure handler. So let&#8217;s see how to test such a function:<\/p>\n<p>[js]<\/p>\n<p>myFactory.getCallStatus = function() {<br \/>\n    MyApi.getData().$promise.then(function(data) {<br \/>\n        switch (data.status) {<br \/>\n            case &#8216;UPDATED&#8217;:<br \/>\n                myFactory.afterUpdate()<br \/>\n                break;<br \/>\n            case &#8216;COMPLETED&#8217;:<br \/>\n                myFactory.afterCompleted();<br \/>\n                break;<br \/>\n            case &#8216;FAILED&#8217;:<br \/>\n                myFactory.afterFailure()<br \/>\n                break;<br \/>\n        }<br \/>\n   });<br \/>\n}<\/p>\n<p>myFactory.afterUpdate=function(){<br \/>\n    &#8230;<br \/>\n    \/\/some code here&#8230;.<br \/>\n    &#8230;<br \/>\n}<\/p>\n<p>myFactory.afterCompleted=function(){<br \/>\n    &#8230;<br \/>\n    \/\/some code here&#8230;.<br \/>\n    &#8230;<br \/>\n}<\/p>\n<p>myFactory.afterFailure=function(){<br \/>\n    &#8230;<br \/>\n    \/\/some code here&#8230;.<br \/>\n    &#8230;<br \/>\n}<\/p>\n<p>[\/js]<\/p>\n<p>To start testing this, create a new file named &#8220;myFactory.test.js&#8221; and initialize the factory you need to test, which in this case is &#8220;MyFactory&#8221;.<\/p>\n<p>[js]<\/p>\n<p>&#8216;use strict&#8217;;<\/p>\n<p>describe(&#8216;MyFactory&#8217;, function () {<\/p>\n<p>  var factory, $q, MyApi; \/\/ declare variables to hold reference of factory and $q service.<\/p>\n<p>  beforeEach(function() {<br \/>\n    module(&#8216;someModule&#8217;); \/\/ initialized module to which your factory belongs.<br \/>\n    module(function ($provide) {<br \/>\n        \/\/ resolve all required dependencies of factory.<br \/>\n        $provide.factory(&#8216;XYZFactory&#8217;, function() {});<br \/>\n    })<\/p>\n<p>\tinject(function (_MyFactory_, _$q_, _MyApi_) {<br \/>\n       \tfactory = _MyFactory_;   \/\/ inject factory for which you are writting test cases.<br \/>\n        $q = _$q_; \t\t\t\t\/\/ As we need $q service to test function which returns promise.<br \/>\n       \tMyApi=_MyApi_;<br \/>\n    });<br \/>\n  });<\/p>\n<p>  describe(&#8216;Validate getCallStatus&#8217;, function () {<\/p>\n<p>    var deferred;<br \/>\n    var data = {&#8216;status&#8217;:&quot;UPDATED&quot;,info:&#8217;success&#8217;};<\/p>\n<p>    \/\/Mocked data to resolve getData() call of MyApi .<\/p>\n<p>   \tit(&quot;Validate getCallStatus call has been completed.&quot;,function() {<br \/>\n        deferred = $q.defer();<\/p>\n<p>        spyOn(MyApi, &#8216;getData&#8217;).and.callFake(function () {<\/p>\n<p>            \/\/Here we will don&#8217;t have mocked our getData() api call and now it returns an object of<br \/>\n           \/\/promise and it&#8217;s not been resolved.<\/p>\n<p>           return {$promise:deferred.promise, $resolve:false};<br \/>\n    \t});<\/p>\n<p>   \t\t\/\/ Now we will resolve this promise with our stubbed data.<\/p>\n<p>   \t\tdeferred.resolve(data);<br \/>\n    \texpect(MyApi.getData).toHaveBeenCalled();<br \/>\n        expect(factory.afterUpdate).toHaveBeenCalled();<br \/>\n    });<br \/>\n  });<\/p>\n<p>});<\/p>\n<p>[\/js]<\/p>\n<p>So that&#8217;s how we tested promise and it&#8217;s resolution. I hope this helps you in your testing.<\/p>\n<p>Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We have been using Jasmine to test our angular code. Recently I came across a piece of code in Javascript which included a REST API call and returned a promise. The promise then had a success callback and all the rest of the play. So in this blog, I have tried to illustrate how to [&hellip;]<\/p>\n","protected":false},"author":72,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":3},"categories":[1439,7,1],"tags":[955,4840,1158,55],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/24864"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/72"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=24864"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/24864\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=24864"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=24864"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=24864"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}