sorting feature in ngTable using Jasmine Testing
Asked Answered
A

1

2

I have created an application using ng-table , the application is working fine but i don'nt know how to write a test case for that sorting and getData.

Can anyone please tell me some solution for testing that functionality

My code is as 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 is 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() 
    {
        // some other logic
    };
});

Update 2

I have done like this for testing, but getting

<failure type="">TypeError: &apos;undefined&apos; is not a function (evaluating &apos;$defer.resolve($scope.myValues)&apos;)

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 );
}));
Anion answered 10/10, 2014 at 14:17 Comment(0)
B
4

You could:

Declare getData function on controller's $scope, thus making it available in your 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 a promise object using $q.

Assign some $scope.myValues for your unit test.

Declare a variable containing your expected result - that is your sorted $scope.myValues array. Then:

promise.then(function(result){
    expect(result).toEqual(expectedResult);
}
$scope.getData(deferred , $scope.tableParams);
Barrios answered 11/10, 2014 at 8:39 Comment(11)
+1 Thanks for the reoly....but promise object using $q.....actually I am not getting. ...Anion
i have updated my answer with the test cases but getting the said exceptionAnion
in your controller, do you have $scope.getData() ?Barrios
no i have updated my method.....the getData is within the $scope.tableParams methodAnion
You should: Declare getData function on controller's $scope, thus making it available in your test.Barrios
how can we declare getData within the controller's $scope that's where i am confused....since the getData is within the $scope.tableParams method and is a part of that method rightAnion
can you please tell me how to do thatAnion
thanks for the update right now i am getting undefined exception....i have updated my questionAnion
sorry, my mistake, you should pass deferred to getData instead of promiseBarrios
now i am getting TypeError: &apos;undefined&apos; is not a function (evaluating &apos;$scope.getData(deferred,$scope.tableParams)&apos;)Anion
runs smoothly as JavascriptTourer

© 2022 - 2024 — McMap. All rights reserved.