javascript - sorting feature in ngTable using Jasmine Testing -
i have created application using ng-table , application working fine don'nt know how write test case sorting , getdata.
can please tell me solution testing functionality
my code given below
jasmine test case
describe('testing controllers', function() { describe('testing workcontroller controller', function() { var workcontroller, $scope; beforeeach(module('wsd')); beforeeach(inject(function($controller, $rootscope) { $scope = $rootscope.$new(); workcontroller = $controller('workcontroller', { $rootscope: $rootscope, $scope: $scope, ngtableparams : ngtableparams, $filter: $filter }); })); it('should tableparams when tableparams called', function() { }); }); });
workstation/main.js
angular.module('wsd.workstations', []) .controller('workcontroller', function($rootscope, $scope, $filter, ngtableparams) { $scope.myvalues = [{name: "moroni", age: 50}, {name: "tiancum", age: 43}, {name: "jacob", age: 27}, {name: "nephi", age: 29}, {name: "enos", age: 34}, {name: "tiancum", age: 43}, {name: "jacob", age: 27}, {name: "nephi", age: 29}, {name: "enos", age: 34}, {name: "tiancum", age: 43}, {name: "jacob", age: 27}, {name: "nephi", age: 29}, {name: "enos", age: 34}, {name: "tiancum", age: 43}, {name: "jacob", age: 27}, {name: "nephi", age: 29}, {name: "enos", age: 34}]; $scope.tableparams = new ngtableparams({ sorting: { name: 'asc' } }, { getdata: function($defer, params) { $scope.myvalues = $filter('orderby')($scope.myvalues, params.orderby()); $defer.resolve($scope.myvalues); } }); $scope.searchdocuments = function() { // other logic }; });
update 2
i have done testing, getting
<failure type="">typeerror: 'undefined' not function (evaluating '$defer.resolve($scope.myvalues)')
test cases
it('should check tableparams getdata sorting', inject(function($q) { var deferred = $q.defer(); var promise = deferred.promise; promise.then(function(result) { expect(result).toequal(expectedresult); }); $scope.myvalues = [{name: "moroni", age: 50}, {name: "tiancum", age: 43}, {name: "jacob", age: 27}, {name: "nephi", age: 29}, {name: "enos", age: 34}, {name: "tiancum", age: 43}, {name: "jacob", age: 27}, {name: "nephi", age: 29}, {name: "enos", age: 34}, {name: "tiancum", age: 43}, {name: "jacob", age: 27}, {name: "nephi", age: 29}, {name: "enos", age: 34}, {name: "tiancum", age: 43}, {name: "jacob", age: 27}, {name: "nephi", age: 29}, {name: "enos", age: 34}]; $scope.getdata(promise, $scope.tableparams ); }));
you could:
declare getdata function on controller's $scope, making available in test:
$scope.tableparams = new ngtableparams({ sorting: { name: 'asc' } }, { getdata: $scope.getdata }); $scope.getdata = function($defer, params) { $scope.myvalues = $filter('orderby')($scope.myvalues, params.orderby()); $defer.resolve($scope.myvalues); }
inject $q in beforeeach().
create promise object using $q.
assign $scope.myvalues unit test.
declare variable containing expected result - sorted $scope.myvalues array. then:
promise.then(function(result){ expect(result).toequal(expectedresult); } $scope.getdata(deferred , $scope.tableparams);
javascript angularjs karma-jasmine ngtable
No comments:
Post a Comment