I already asked this question where the main point was scope doesn't exists in terminal but it exists in Chrome debugging tool. Despite the answers it didn't get fixed.
The question is what is the right syntax to test the bellow directive, especially the line expect(scope.measurementScroll).toBe(true);
. While digging through web i couldn't find any similar question most questions are related to $q.defer()
where in my case there is underscore method _.defer()
Controller
'use strict';
angular.module('myApp')
.controller('MeasurementsTimelineCtrl', ['$scope', '$state', 'Measurements', function($scope, $state, Measurements) {
$scope.measurements = null;
var userId = $scope.currentUser ? $scope.currentUser.id : null;
if (userId) {
var listOfMeasurements = Measurements.userIndex(userId);
listOfMeasurements.then(function(data){
$scope.measurements = data;
$scope.$broadcast('measurements-updated', $scope.measurements);
});
}
}]);
Directive:
'use strict';
angular.module('myApp')
.directive('dashboardMeasurementTimeline', ['$window', function($window) {
return {
restrict: 'E',
templateUrl: 'myView.html',
controller: 'MeasurementsTimelineCtrl',
link: function(scope, element){
scope.$on('measurements-updated', function(measurements) {
_.defer(function(){
if(measurements) {
scope.measurementScroll = true;
}
});
});
}
};
}]);
Test
'use strict';
describe('Directive: dashboardMeasurementTimeline', function () {
var $rootScope, $compile, element, scope;
beforeEach(function() {
module('myApp');
inject(function($injector) {
$rootScope = $injector.get('$rootScope');
$compile = $injector.get('$compile');
});
scope = $rootScope.$new();
element = angular.element('<dashboard-measurement-timeline></dashboard-measurement-timeline>');
element = $compile(element)(scope);
scope.currentUser = {id : 'someId'};
scope.$digest();
scope.measurements = [{id: 'someId', time_of_test: 'Tue, 30 Dec 2014 14:00:00 -0000'},
{id: 'someId', time_of_test: 'Thu, 20 Nov 2014 03:00:00 -0000'},];
scope.$broadcast('measurements-updated', scope.measurements);
scope.$apply();
});
it('should assign true value to measurementScroll', function () {
expect(scope.measurementScroll).toBe(true);
});
});