Below I have 3 functions that do the same. Each uses a different way to call setTimeout, delay1 () uses setTimeout directly, delay2 () uses angularjs $ timeout, and delay3 () uses lodash debounce. Everything is working fine.
Problems arise when I test Jasmine. setTimeout works great with the jasmine.clock () method. tick (), but $ timeout and debounce are not
I am interested in canceling work with Jasmine. I know I can use $ timeout.flush () with angularjs, but $ timeout and setTimeout give me problems elsewhere in my code where I use it with booklet maps. debounce works with an elevator.
I created a plunker here: plnkr , where you will see that the $ timeout and debounce tests do not pass while the setTimeout test passes.
Is there any way around this problem? Thanks
Js
var app = angular.module('plunker', []); app.controller('MainCtrl', function($scope, $timeout) { $scope.name = 'World'; $scope.delayed1 = function(){ setTimeout(function(){ $scope.name = "Hello world by setTimeout"; },500) } $scope.delayed2 = function(){ $timeout(function(){ $scope.name = "Hello world by $timeout"; },500) } $scope.delayed3 = function(){ _.debounce(function(){ $scope.name = "Hello world by debounce"; },500) } });
specifications
describe('Testing a Hello World controller', function() { var $scope = null; var ctrl = null; //you need to indicate your module in a test beforeEach(module('plunker')); beforeEach(inject(function($rootScope, $controller) { $scope = $rootScope.$new(); ctrl = $controller('MainCtrl', { $scope: $scope }); })); it('should say hallo to the World', function() { expect($scope.name).toEqual('World'); }); it('should say Hello world by setTimeout', function() { jasmine.clock().install(); $scope.delayed1(); jasmine.clock().tick(600); expect($scope.name).toEqual('Hello world by setTimeout'); jasmine.clock().uninstall(); }); it('should say Hello world by timeout', function() { jasmine.clock().install(); $scope.delayed2(); jasmine.clock().tick(600); expect($scope.name).toEqual('Hello world by timeout'); jasmine.clock().uninstall(); }); it('should say Hello world by debouce', function() { jasmine.clock().install(); $scope.delayed3(); jasmine.clock().tick(600); expect($scope.name).toEqual('Hello world by debouce'); jasmine.clock().uninstall(); }); });
source share