lundi 3 août 2015

how to unit test custom directive?

i am trying to test my custom directive in angularjs with karma and jasmine but could'nt its gives me syntax error. here is my directive :

     <div my-data remoteurl='url' filter='test' order-by='sortExpression' order='order' >
    </div>

myappcontroller:

 (function() {

   'use strict';

    var myApp = angular.module('myApp', [])
.controller('myAppCtrl', ['$scope', '$http', function($scope, $http) {
  $scope.url = 'http://ift.tt/1KEkUhj';
  $scope.filter= 'test';
  $scope.orderBy= 'sortExpression';
  $scope.order= 'orderBy';
}])
.directive('myData', ['$http', function($http) {
  return {
    restrict: 'A',
    scope: {
      remoteurl: '=',
      filter: '=',
      orderBy: '=',
      order: '='
     // orderBy:'sortExpression':'order' ;
    },
    templateUrl: 'DataTable.html',
    link: function(scope, element, attr) {

      $http.get(scope.remoteurl)
        .success(function(response) {
          scope.names = response.data.children;
        });
    }
  };
}]);

})();

my controllerspec:

     describe('myAppCtrl', function() {
          var scope, controller, httpBackend;
          var data = {
          "kind": "Listing",
         "data": {
              "modhash": "",
                "children": [{
               "kind": "t3",
             "data": {
                  "domain": "theguardian.com",
                  "banned_by": null,
            "media_embed": {},
            "subreddit": "worldnews",
            "selftext_html": null,
            "selftext": "",
            "likes": null,
            "suggested_sort": null,
            "user_reports": [],
            "secure_media": null,
            "link_flair_text": null,
            "id": "3ef7lv",
            "from_kind": null,
            "gilded": 0,
            "archived": false,
            "clicked": false,
            "report_reasons": null,
            "author": "anutensil",
            "media": null,
            "score": 4,
            "approved_by": null,
            "over_18": false,
            "hidden": false,
            "num_comments": 0,
            "thumbnail": "",
            "subreddit_id": "t5_2qh13",
            "edited": false,
            "link_flair_css_class": null,
            "author_flair_css_class": null,
            "downs": 0,
            "secure_media_embed": {},
            "saved": false,
            "removal_reason": null,
            "stickied": false,
            "from": null,
            "is_self": false,
            "from_id": null,
            "permalink": "/r/worldnews/comments/3ef7lv/beekilling_pesticides_quietly_permitted_by_the_uk/",
            "name": "t3_3ef7lv",
            "created": 1437752870.0,
            "url": "http://ift.tt/1HVoePi",
            "author_flair_text": null,
            "title": "Bee-killing pesticides quietly permitted by the UK govt",
            "created_utc": 1437724070.0,
            "distinguished": null,
            "mod_reports": [],
            "visited": false,
            "num_reports": null,
            "ups": 4
        }
    }, {
        "kind": "t3",
        "data": {
            "domain": "m.sputniknews.com",
            "banned_by": null,
            "media_embed": {},
            "subreddit": "worldnews",
            "selftext_html": null,
            "selftext": "",
            "likes": null,
            "suggested_sort": null,
            "user_reports": [],
            "secure_media": null,
            "link_flair_text": "Ukraine/Russia",
            "id": "3ef6q4",
            "from_kind": null,
            "gilded": 0,
            "archived": false,
            "clicked": false,
            "report_reasons": null,
            "author": "dexter93",
            "media": null,
            "score": 0,
            "approved_by": null,
            "over_18": false,
            "hidden": false,
            "num_comments": 2,
            "thumbnail": "",
            "subreddit_id": "t5_2qh13",
            "edited": false,
            "link_flair_css_class": "ukrassia",
            "author_flair_css_class": null,
            "downs": 0,
            "secure_media_embed": {},
            "saved": false,
            "removal_reason": null,
            "stickied": false,
            "from": null,
            "is_self": false,
            "from_id": null,
            "permalink": "/r/worldnews/comments/3ef6q4/french_lawmakers_surprised_by_happy_citizens_in/",
            "name": "t3_3ef6q4",
            "created": 1437752114.0,
            "url": "http://ift.tt/1fqXI8S",
            "author_flair_text": null,
            "title": "French Lawmakers Surprised by \u2018Happy\u2019 Citizens in Crimea\u2019s Yalta / Sputnik International",
            "created_utc": 1437723314.0,
            "distinguished": null,
            "mod_reports": [],
            "visited": false,
            "num_reports": null,
            "ups": 0
        }

    }],
    "after": "t3_3eeqtg",
    "before": null
}

}

beforeEach(module('myApp'));
beforeEach(inject(function($rootScope, $controller, $httpBackend) {
    scope = $rootScope;
    controller = $controller;
    httpBackend = $httpBackend;
}));
it("should populate the reddit data when the HTTP request succeeds", function () {
   httpBackend.when('GET','http://ift.tt/1KEkUhj').respond(200,{data:data});

    controller('myAppCtrl', {'$scope': scope });
    //httpBackend.flush();
    scope.$apply();

    // Create an instance of the directive
    element = angular.element('<div my-data remoteurl="url" filter="test" order-by="sortExpression" order="order" >
    </div>');
    $compile(element)(scope); // Compile the directive
    scope.$digest(); // Update the HTML

    // Get the isolate scope for the directive
    var isoScope = element.isolateScope();

    // Make our assertions
    expect(isoScope.data.children).not.toBeNull();
});

}); how can i check weather request is successful to that external url and also check mocked data?

Aucun commentaire:

Enregistrer un commentaire