dimanche 31 janvier 2016

AngularJS Unit Test Array with Karma

I created an app of a calendar and I am trying to create a unit test to show where Jan 1, 2016 will show up on the calendar's array which should be at

$scope.weeks[0][5]

. However, when I test my array I keep getting an undefined on the first element on the array.

Here is the error:

TypeError: Cannot read property '0' of undefined

Not sure where I am going wrong. Any help would be appreciated.

Here is my unit test code:

describe('correctDate', function(){
    var $scope, ctrl;
    beforeEach(module("calendarDemoApp"));
    beforeEach(inject(function($controller, $rootScope){
        $scope = $rootScope.$new();
        ctrl = $controller('MonthCtrl', {
            $scope: $scope
        });
        $scope.today = "Fri Jan 01 2016";
    }));
    fit('should be the 5th element in array', function(){
        console.log($scope.today);
        expect($scope.today).toBe($scope.weeks[0][5]);
    });
});

Here is the code from my app:

angular.module('calendarDemoApp', [])
    .controller('MonthCtrl', function($scope){
        $scope.$watch('date', function(newDate){
            var range = CalendarRange.getMonthlyRange(new Date(newDate.year, newDate.month, 1));
            var totalWeeks = range.days.length/7;           
            var weeks = [];
            for(i=0; i<totalWeeks; i++){
                weeks[i] = [];
                for(j=0; j<7; j++){
                    weeks[i].push(range.days[j+(i*7)]);
                }
            }
            $scope.weeks = weeks;
        }, true);
})

Aucun commentaire:

Enregistrer un commentaire