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/28a0e4705a76c9371e06 to your computer and use it in GitHub Desktop.
Save bbraithwaite/28a0e4705a76c9371e06 to your computer and use it in GitHub Desktop.
<html>
<head>
<title>Interval 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://code.angularjs.org/1.4.0-rc.2/angular.js"></script>
<script type="text/javascript" src="https://code.angularjs.org/1.4.0-rc.2/angular-mocks.js"></script>
</head>
<body>
<script type="text/javascript">
var app = angular.module('calculatorApp', []);
app.controller('CalculatorController', function calculatorController($scope, $interval) {
$scope.result1 = Math.random();
$scope.result2 = Math.random();
$scope.sum1 = function sum1() {
$scope.result1 = $scope.result1 + 1;
}
$scope.sum2 = function sum2() {
$scope.result2 = $scope.result2 + 1;
}
/* Sum Interval #1 - run every 1 second */
var sumInterval = $interval($scope.sum1, 1000);
/* Sum Interval #2 - run every 1 second, but stop after 10 times */
var sumIntervalTenTimes = $interval($scope.sum2, 1000, 10);
$scope.cancel = function() {
if (angular.isDefined(sumInterval) || angular.isDefined(sumIntervalTenTimes)) {
$interval.cancel(sumInterval);
$interval.cancel(sumIntervalTenTimes);
sumInterval = undefined;
sumIntervalTenTimes = undefined;
}
}
$scope.$on('$destroy', function() {
$scope.cancel();
});
});
describe('calculator tests', function () {
beforeEach(module('calculatorApp'));
var $controller;
var $scope;
var $intervalSpy;
beforeEach(inject(function(_$controller_, _$rootScope_, _$interval_) {
$controller = _$controller_;
$intervalSpy = jasmine.createSpy('$interval', _$interval_).and.callThrough();
$scope = _$rootScope_.$new();
}));
describe('interval tests (using spy)', function() {
it('should register the intervals', function () {
// Create the controller instance and pass in our spy in place of the $interval instance.
var calculatorController = $controller('CalculatorController', { $scope: $scope, $interval: $intervalSpy });
// We can ask if the spy was called
expect($intervalSpy).toHaveBeenCalled();
// Or to make more accurate assertions, such as the number of times called
expect($intervalSpy.calls.count()).toBe(2);
// ... or the arguments passed
expect($intervalSpy).toHaveBeenCalledWith($scope.sum1, 1000);
expect($intervalSpy).toHaveBeenCalledWith($scope.sum2, 1000, 10);
// We can also access the calls to the $interval spy as follows:
var calls = $intervalSpy.calls.all();
var args0 = calls[0].args; // first
var args1 = calls[1].args; // second
// .. do something interesting here...
});
it('should cancel the intervals on click', function () {
spyOn($intervalSpy, 'cancel');
var calculatorController = $controller('CalculatorController', { $scope: $scope, $interval: $intervalSpy });
$scope.cancel();
expect($intervalSpy.cancel.calls.count()).toBe(2);
// how do we assert that cancel is called with the correct interval promises?
expect($intervalSpy.cancel.calls.argsFor(0)[0].$$intervalId).toBe(0);
expect($intervalSpy.cancel.calls.argsFor(1)[0].$$intervalId).toBe(1);
});
it('should cancel the intervals on destroy', function () {
var calculatorController = $controller('CalculatorController', { $scope: $scope, $interval: $intervalSpy });
spyOn($scope, 'cancel');
$scope.$destroy();
expect($scope.cancel).toHaveBeenCalled();
});
it('should not cancel the already cancelled intervals on destroy', function () {
spyOn($intervalSpy, 'cancel');
var calculatorController = $controller('CalculatorController', { $scope: $scope, $interval: $intervalSpy });
$scope.cancel();
$scope.$destroy();
expect($intervalSpy.cancel.calls.count()).toBe(2);
});
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment