I created an application using ng-table, the application works fine, but I donβt know how to write a test case for this sort and getData.
Can someone tell me some solution to test this functionality
My code is below
jasmine test
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 is called', function() { }); }); });
<strong> 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() {
Update 2
I did it for testing, but I get
<failure type="">TypeError: 'undefined' is not a 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 ); }));