Zack Macomber
Zack Macomber

Reputation: 6905

Getting the response from an angular function

I have the following scope function in my controller

$scope.getStaff = function (request, response) {
    var a = [];

    if ($scope.staff !== null) {
        // all terms must be contained
        a = $scope.staff;
        var terms = request.term.toLowerCase().split(' ');

        for (var i = 0; i < terms.length; i++) {
            var t = terms[i];

            if (t) {
                a = $.grep(a, function (item, index) {
                    var v = item.label.toLowerCase();
                    return v.indexOf(t) !== -1;
                });
            }
        }
    }

    response(a.length > 0 ? a : null);
};

I'm attempting to test it using jasmine like this:

describe('getStaff', function() {

    it('...', function() {
        $scope.staff = [
            { label: "test1" },
            { label: "test2" }
        ];
        var req = { term: "a b c" };
        expect(req.term.toLowerCase()).toBe('a b c');
        var res = function(a) {
            return a;
        }
        var result = $scope.getStaff(req, res).response;
        expect(result).toBe(null);
    });
});

I'm ultimately trying to see what "a" is in the getStaff function. How can I get that value in my jasmine test?

Upvotes: 1

Views: 53

Answers (1)

Dan Doyon
Dan Doyon

Reputation: 6720

My answer is really an opinion. Unless you are willing to expose your 'a' in the scope of the controller, then my answer would be "you don't care" your jasmine test should only be testing the answer of response(a.length > 0 ? a : null); returned.

My gut tells me you may want to consider creating a helper function for this code

 $.grep(a, function (item, index) {
                var v = item.label.toLowerCase();
                return v.indexOf(t) !== -1;
            });

and unit testing that separate from your getStaff function.

Upvotes: 1

Related Questions