mardi 12 juillet 2016

Unit testing success and error functions of iron-ajax elements using webcomponentstester

I have the following code:

_getDetails: function() {
      var headers;
      var resp;
      var self = this;
      headers = this._setHeaderFunction();
      this.prepareAjaxCall({
        url: http: http://ift.tt/17vsUzp,
        headers: headers,
        method: 'GET',
        success: function(request) {
          self._handler(request.response, false);
        },
        error: function(data){
          resp = event.currentTarget.response.info;
          self._handler(resp, true);
        }
      });

      return this.$.ajax.generateRequest();

And I am trying to write a test suite(mocha, sinon) to cover this _getDetails function. So far, I am able to test the majority of the body, with the exception of the success() and error() methods. When I execute the test validation, it tells me that the lines concerning these methods are not covered. I have tried this so far:

          var ajax = myEl.$.ajax;
          sinon.spy(ajax,'generateRequest');
          sinon.spy(myEl, '_handler');
          sinon.spy(ajax, '_boundHandleResponse')
          var request = myEl._getDetails();

          //in the prepareAjaxCall method, if the object passed to the function has a success method, it is assigned to the iron-ajax's _boundHandleResponse variable. 
          var success = ajax._boundHandleResponse;

          setTimeout(function () {
            success(request, true);
            expect(success).to.have.been.calledWith(request);
            expect(myEl._handler).to.have.been.calledWith(request.response, false);
          },1000)

How can I isolate the success and error functions in order to test them individually and thus have mocha recognise these lines as being tested?

Aucun commentaire:

Enregistrer un commentaire