Skip to content

Instantly share code, notes, and snippets.

@bbraithwaite
Last active January 24, 2017 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bbraithwaite/5a5b817895db75250ea6 to your computer and use it in GitHub Desktop.
Save bbraithwaite/5a5b817895db75250ea6 to your computer and use it in GitHub Desktop.
<html>
<head>
<title>Timeout Examples</title>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.3/jasmine.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.3/jasmine.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.3/jasmine-html.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.3/boot.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular-mocks.js"></script>
</head>
<body>
<script type="text/javascript">
var app = angular.module('calculatorApp', []);
app.controller('CalculatorController', function calculatorController($scope, $timeout) {
$scope.sum = function sum() {
$scope.result = $scope.x + $scope.y;
}
$scope.sumWithWait = function sumWithWait() {
$timeout($scope.sum, 1000);
}
$scope.sumWithWaitAndReset = function sumWithWaitAndReset() {
// timeout 1: gives us the sum after 3 seconds
$timeout($scope.sum, 3000);
// timeout 2: after 4 seconds, the result is set back to zero.
$timeout(function() { $scope.result = 0; }, 4000);
}
});
describe('calculator tests', function () {
beforeEach(module('calculatorApp'));
var $controller;
var $scope;
beforeEach(inject(function(_$controller_) {
$controller = _$controller_;
$scope = {};
}));
describe('timeout tests', function() {
it('should set results to 3 for sum', function() {
var calculatorController = $controller('CalculatorController', { $scope: $scope });
$scope.x = 1;
$scope.y = 2;
$scope.sum();
expect($scope.result).toBe(3);
});
describe('using timeout', function() {
var $timeout;
beforeEach(inject(function(_$timeout_) {
$timeout = _$timeout_;
}));
it('should set results to 3 for sum with timeout', function() {
var calculatorController = $controller('CalculatorController', { $scope: $scope });
$scope.x = 1;
$scope.y = 2;
$scope.sumWithWait();
// flush timeout(s) for all code under test.
$timeout.flush();
// this will throw an exception if there are any pending timeouts.
$timeout.verifyNoPendingTasks();
expect($scope.result).toBe(3);
});
it('should set results to 3 for sum with timeout (using delay with flush)', function() {
var calculatorController = $controller('CalculatorController', { $scope: $scope });
$scope.x = 1;
$scope.y = 2;
$scope.sumWithWaitAndReset();
// first timeout
$timeout.flush(3001);
expect($scope.result).toBe(3);
expect($timeout.verifyNoPendingTasks).toThrow();
// second timeout
$timeout.flush(1000);
expect($scope.result).toBe(0);
$timeout.verifyNoPendingTasks();
});
});
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment