Device Testing Watchers in angular js controller

How can this fragment code be tested using jasmine?

$scope.profileObject = ProfilesSharedObject; $scope.$watch("profileObject.startDate", function() { var startDate = $scope.profileObject.startDate._d; var endDate = $scope.profileObject.endDate._d; var newStartDate = moment(startDate).format("YYYY-MM-DD"); var newEndDate = moment(endDate).format("YYYY-MM-DD"); $scope.startDate = moment(startDate).format("MM/DD"); $scope.endDate = moment(endDate).format("MM/DD/YYYY"); $scope.getSleepData(newStartDate, newEndDate); }); 

where ProfileSharedObject is an angular js service

+4
source share
1 answer

Observers are tapped in every digest cycle. This usually happens automatically, but during unit testing you need to manually run it:

 it('should update the start date', function() { // Arrange ProfileSharedObjectMock.startDate = new Date(2013, 0, 1); // Act $scope.$digest(); // Assert expect($scope.startDate).toEqual(new Date(2013, 0, 1)); }); 

I created a Plunker script so you can see the whole test suite.

+5
source

Source: https://habr.com/ru/post/1502522/


All Articles