qunit - How to write unit test to embeded ajax requests? -


i have handledownload method starts file download. function posts backend, gives response, based on new request posts server files are. saw can use mockjax mock requests, how handle different paths success, error, etc. how should know response triggers path (success,error, complete, ...). great startegy test handledownload function, , how? mocking use sinon.js don't have deep understanding yet. should check either handledownloadfinal function called.

handledownload: function(data, url) { $.ajax({     type: "post",     url: url,     data: {},     success: function(response) {         if (response.success) {             var start_token = response.token;             $.ajax({                 type: start_token.method,                 url: start_token.url,                 beforesend: function(xhr) {                     xhr.setrequestheader('authorization', start_token.header);                 },                 success: function(start_response) {                     handledownloadfinal(start_response.status_token);                 },                 error: function(start_response) {                     $.ajax({                         type: "post",                         url: url + 'proxy/',                         success: function(fallback_response) {                             if (fallback_response.success) {                                 handledownloadfinal(fallback_response.status_token, true, fallback_response.job_uuid);                             } else {                                 errordownload(response.error);                             }                         },                         error: function(fallback_response) {                             // real error                                                                 generalerrordownload();                         },                         datatype: 'json'                     });                 },                 datatype: 'json',                 xhrfields: {                     withcredentials: true                 }             });         } else {             errordownload(response.error);         }     },     error: function(response) {         generalerrordownload();     },     complete: function() {     },     datatype: "json" });  } 

you should use fake server coming sinon.

before(function(){     //create server      this.server = sinon.fakeserver.create();     // let server automatically respond every request     server.autorespond = true; })  it('test something', function(){      //let server respond  specific url 200-ok     this.server.respondwith("post", "/some/article/comments.json", [200, {       "content-type": "application/json"     }, '[{ "id": 12, "comment": "hey there" }]']); }) 

as have bunch of requests , have check combinations suggest have helper function every request fail success test cases this:

function letfirstrequestsucceed() {   this.server.respondwith("post", "urlforfirstrequest", [200, {     "content-type": "application/json"   }, '[{ "id": 12, "comment": "hey there" }]']); }   function letsecondrequestfail() {   this.server.respondwith("post", "urlforsecondrequest", [404, {     "content-type": "application/json"   }, '{error: "some error message"}'); }   function letthirdrequestfail() {   this.server.respondwith("post", "urlforthirdrequest", [404, {     "content-type": "application/json"   }, '{error: "some error message"}'); }  it("should when second , third request fails", function () {   sinon.spy(window, 'generalerrordownload');   letfirstrequestsucceed();   letsecondrequestfail();   letthirdrequestfail();    handledownload('somedate', 'aurl');    expect(generalerrordownload)  }) 

btw should think refactor code using jquerys deferred supported api ajax calls, make more readable.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -