angular.js/test/service/deferSpec.js
Igor Minar fe5240732d feat(strict mode): adding strict mode flag to all js files
the flag must be in all src and test files so that we get the benefit of
running in the strict mode even in jstd

the following script was used to modify all files:

for file in `find src test -name "*.js"`; do
  echo -e "'use strict';\n" > temp.txt
  cat $file >> temp.txt
  mv temp.txt $file
done
2011-07-18 12:12:55 -07:00

78 lines
2 KiB
JavaScript

'use strict';
describe('$defer', function() {
var scope, $browser, $defer, $exceptionHandler;
beforeEach(function(){
scope = angular.scope({}, angular.service,
{'$exceptionHandler': jasmine.createSpy('$exceptionHandler')});
$browser = scope.$service('$browser');
$defer = scope.$service('$defer');
$exceptionHandler = scope.$service('$exceptionHandler');
});
afterEach(function(){
dealoc(scope);
});
it('should delegate functions to $browser.defer', function() {
var counter = 0;
$defer(function() { counter++; });
expect(counter).toBe(0);
$browser.defer.flush();
expect(counter).toBe(1);
$browser.defer.flush(); //does nothing
expect(counter).toBe(1);
expect($exceptionHandler).not.toHaveBeenCalled();
});
it('should delegate exception to the $exceptionHandler service', function() {
$defer(function() {throw "Test Error";});
expect($exceptionHandler).not.toHaveBeenCalled();
$browser.defer.flush();
expect($exceptionHandler).toHaveBeenCalledWith("Test Error");
});
it('should call eval after each callback is executed', function() {
var evalSpy = this.spyOn(scope, '$eval').andCallThrough();
$defer(function() {});
expect(evalSpy).not.toHaveBeenCalled();
$browser.defer.flush();
expect(evalSpy).toHaveBeenCalled();
evalSpy.reset(); //reset the spy;
$defer(function() {});
$defer(function() {});
$browser.defer.flush();
expect(evalSpy.callCount).toBe(2);
});
it('should call eval even if an exception is thrown in callback', function() {
var evalSpy = this.spyOn(scope, '$eval').andCallThrough();
$defer(function() {throw "Test Error";});
expect(evalSpy).not.toHaveBeenCalled();
$browser.defer.flush();
expect(evalSpy).toHaveBeenCalled();
});
it('should allow you to specify the delay time', function(){
var defer = this.spyOn($browser, 'defer');
$defer(noop, 123);
expect(defer.callCount).toEqual(1);
expect(defer.mostRecentCall.args[1]).toEqual(123);
});
});