angularjs - Why is Angular $timeout blocking End-To-End tests? -
i've made directive used show notification messages user. show notification wrote this:
$scope.$watch($messaging.isupdated, function() { $scope.messages = $messaging.getmessages(); if ($scope.messages.length > 0) { $timeout(function() { (var = 0; < $scope.messages.length; i++) { if (i + 1 < $scope.messages.length) { $messaging.removemessage($scope.messages[i]); } else { $messaging.removemessage($scope.messages[i]); } } }, 5000); } });
i'm using $timeout make sure messages stays on screen 5 seconds.
now want write end-to-end test on can sure notification shown. problem when notification shown end-to-end timed out notification message. makes impossible check whether right notification shown. .
this test code:
it('submit update center', function() { input('center.name').enter('new name'); input('center.department').enter('new department'); input('center.contact').enter('new contact'); input('center.street').enter('new street'); input('center.city').enter('new city'); input('center.country').enter('new country'); element('button#center_button').click(); expect(element('.feedback').count()).tobe(1); expect(element('.feedback:first').attr('class')).tomatch(/success/); expect(element('.error.tooltip').count()).tobe(0); });
i’d avoid using javascript settimeout() , hope there’s (angular) solution problem.
bad news, pal. it's known issue in angularjs. there discussion here , "somehow related" issue here.
fortunately, can workaround rolling own $timeout
service, calling settimeout
, calling $apply
hand (this suggestion discussion i've referred). it's simple, although it's ugly. simple example:
app.service('mytimeout', function($rootscope) { return function(fn, delay) { return settimeout(function() { fn(); $rootscope.$apply(); }, delay); }; });
note 1 not compatible angular $timeout
, can extend functionality if need.
Comments
Post a Comment