AnsweredAssumed Answered

WAB unit testing,handling XMLHttpRequest response

Question asked by sandhyabaratam@gmail.com on Apr 10, 2020
Latest reply on Apr 13, 2020 by rscheitlin

Hi,
I am working on unit testing of my custom widget. I am unable to receive the response from XMLHttpRequest requests.Can some one please help me.Here I have copied a simple XML HTTP request.I am getting the same problem with dojoPromise response as well.

 

unit test method----

'Test getTravelModes':function(){
   var defferd = this.async(1000);

   testApp.getTravelModes().then(defferd.callback(
   function(data) {
      console.log(data);
      assert.equal(response,8,'travel modes count matches');

   }, function(err) {
      console.log(err);
   }));

}

widget method----
getTravelModes:function(){

   var travelModes = [];
   var xhr = new XMLHttpRequest();
   xhr.onreadystatechange = function() {
      console.log(this.status + " " + this.readyState);
      if (this.readyState == 4 && this.status == 200) {

      if(this.responseText.indexOf("error") == -1){
      for(var i=0;i<JSON.parse(this.responseText).supportedTravelModes.length;i++){
         travelModes.push(JSON.parse(this.responseText).supportedTravelModes[i].name);
      }
      return travelModes;
      }
   };
   }
xhr.open("POST", "https://hostname/rest/services/Routing/NetworkAnalysis/NAServer/Route_World/retrieveTravelModes",false);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
var chkParams = "token="+tokenValue+"&f=json";
xhr.send(chkParams);


}

 

Regards,

Sandhya

Outcomes