Jasmine, Unit testing XMLHttpRequest with Promises

Rory

I have the following function, that uses the Q promise library

getConfig: function(params){

            return this.codeApiClient.get(this.endpoints.config, params, {}).then(function(response){
                return response;
            }, function(err){
                throw err;
            });
        }

The above code calls the API code shown below (I've abbreviated the code to rid noise)

CodeApiClient.prototype = {
  get: function(endpoint, paramStr, headers){
      var defer = new Q.defer();
      var start_time = new Date().getTime();

      var req = new XMLHttpRequest();
      var url = endpoint + (paramStr.indexOf('?') !== -1 ? paramStr : '?' + paramStr);

      req.open('GET', url);

      req.onload = function() {
          var request_time = new Date().getTime() - start_time;
          // This is called even on 404 etc
          if (req.status < 400) {
              var response = JSON.parse(req.response)
              response.__request_time = request_time;
              defer.resolve(response);
          } else {
              // Otherwise reject with the status text
              defer.reject(Error(req.statusText));
          }
      };
}

My question is: How do I write a Jasmine test for getConfig i.e. faking the response and stubbing the underlying XMLHttpRequest ? Is sinon.js capable of this. I know it can stub callbacks on $.ajax, but I'm not sure how to do this with a promise lib such as Q. Note this is pure JavaScript, no angular etc

Winter Soldier
  • Here is how to use callFake in jasmine to return a promise.
  • Now since both Q as well as traditional jQuery provide the deferred objects, you could substitute $.Deferred() with Q.defer();

var tempObj = {
    	getConfig: function(params){

                return this.codeApiClient.get(this.endpoints.config, params, {}).then(function(response){
                    return response;
                }, function(err){
                    throw err;
                });
            }
        codeApiClient : {
          get : function(){
            // some get function of codeApiClient
          }
        }
    }


    it('test getConfig success', function(){
    	var dummyResponse = {'someDummyKey' : 'someDummyValue'};
    	spyOn(tempObj.codeApiClient, 'get').and.callFake(function(e){
    		return $.Deferred().resolve(dummyResponse).promise();
    	}); 
      tempObj.getConfig({});
      //any expectations
    });

    it('test getConfig failure', function(){
    	var dummyResponse = {'error' : 'someDummyFailure'};
    	spyOn(tempObj.codeApiClient, 'get').and.callFake(function(e){
    		return $.Deferred().reject(dummyResponse).promise();
    	});
       tempObj.getConfig({});
      //any expectations
    });

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Unit testing promises from an api using Jasmine

From Dev

Unit testing promises in AngularJS

From Dev

Unit Testing with Promises and Spies

From Dev

Unit Testing with Promises and Spies

From Dev

Unit testing Angularjs jasmine

From Dev

Unit testing $modal with Jasmine

From Dev

Angular/Jasmine testing with deffered promises

From Dev

Testing AngularJS promises in Jasmine 2.0

From Dev

Angularjs unit testing resolving promises

From Dev

Unit testing promises in controllers in AngularJS

From Dev

Angularjs unit testing resolving promises

From Dev

Unit testing with Jasmine, mocking a constructor

From Dev

Jasmine unit testing an element is not :hidden

From Dev

jasmine unit testing was method called

From Dev

expect() in .then() - jasmine unit testing, AngularJS

From Dev

Jasmine unit testing an element is not :hidden

From Dev

Jasmine Ignoring Tests When Testing Promises

From Dev

Unit testing in AngularJS - Mocking Services and Promises

From Dev

Ionic Couchbase Lite Karma Jasmine Unit Testing

From Dev

Unit testing an angular service method in jasmine

From Dev

Unit-Testing a filter with Jasmine in AngularJS

From Dev

Jasmine unit testing current context or this var

From Dev

Unit testing a modalInstance controller with Karma / Jasmine

From Dev

Unit Testing Angular + Browserify + Gulp and Jasmine

From Dev

mock angularjs service for unit testing using jasmine

From Dev

Unit-testing with Karma-Jasmine

From Dev

Jasmine Karma Unit Testing on form fields

From Dev

Jasmine Angular unit testing: controller with an external variable

From Dev

Unit Testing an Angular Service using Karma and Jasmine

Related Related

HotTag

Archive