This repository has been archived by the owner on Aug 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
spying-on-interval-spec.js
68 lines (64 loc) · 1.74 KB
/
spying-on-interval-spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Good intro to unit testing Angular $interval service
// http://www.bradoncode.com/blog/2015/06/15/unit-testing-interval-angularls/
angular.module('IntervalExample', [])
.service('numbers', function ($interval, $rootScope) {
return function emitNumbers(delay, n) {
var k = 0;
$interval(function () {
$rootScope.$emit('number', k);
k += 1;
}, 100, n);
};
});
/* global ngDescribe, it */
ngDescribe({
name: 'testing $interval',
module: 'IntervalExample',
inject: ['numbers', '$rootScope', '$interval'],
verbose: false,
only: false,
tests: function (deps) {
it('emits 3 numbers', function (done) {
deps.$rootScope.$on('number', function (event, k) {
if (k === 2) {
done();
}
});
// emit 3 numbers with 100ms interval
deps.numbers(100, 3);
// advance mock $interval service by 500 ms
// forcing 3 100ms intervals to fire
deps.$interval.flush(500);
});
}
});
var intervalCalled;
ngDescribe({
name: 'spying on $interval',
module: 'IntervalExample',
inject: ['numbers', '$rootScope'],
verbose: false,
only: false,
mocks: {
IntervalExample: {
$interval: function mockInterval(fn, delay, n) {
var injector = angular.injector(['ng']);
var $interval = injector.get('$interval');
intervalCalled = true;
return $interval(fn, delay, n);
}
}
},
tests: function (deps) {
it('emits 3 numbers', function (done) {
deps.$rootScope.$on('number', function (event, k) {
if (k === 2) {
done();
}
});
// emit 3 numbers with 100ms interval
deps.numbers(100, 3);
la(intervalCalled, 'the $interval was called somewhere');
});
}
});