UNPKG

2.15 kBJavaScriptView Raw
1describe('ng-throttle',function() {
2 var ngServices = factory('services/ng-services');
3 var module = factory('services/ng-throttle',{
4 'services/ng-services': ngServices
5 });
6
7 var $throttle,$timeout;
8
9 beforeEach(function() {
10 angular.mock.module(module.name);
11 angular.mock.inject(function(_$throttle_,_$timeout_) {
12 $throttle = _$throttle_;
13 $timeout = _$timeout_;
14 });
15 });
16
17 it('should call immediately',function() {
18 var spy = jasmine.createSpy('spy');
19 var f = $throttle(spy,1000);
20 f('foo');
21 expect(spy).toHaveBeenCalledWith('foo');
22 });
23
24 it('should call only once when multiple invokations',function() {
25 var spy = jasmine.createSpy('spy');
26 var f = $throttle(spy,1000);
27 f('foo');
28 f('foo');
29 f('foo');
30 expect(spy.calls.count()).toBe(1);
31 $timeout.flush();
32 expect(spy.calls.count()).toBe(2);
33 });
34
35 it('should not call the leading edge if that option is given',function() {
36 var spy = jasmine.createSpy('spy');
37 var f = $throttle(spy,1000,{
38 leading: false
39 });
40 f('foo');
41 f('foo');
42 f('foo');
43 expect(spy.calls.count()).toBe(0);
44 $timeout.flush();
45 expect(spy.calls.count()).toBe(1);
46 });
47
48 it('should even work when the throttled function invokes the throttle again',function() {
49 var spy = jasmine.createSpy('spy');
50 var f = $throttle(spy,1000);
51 spy.and.callFake(f);
52 f('foo');
53 f('foo');
54 f('foo');
55 expect(spy.calls.count()).toBe(1);
56 $timeout.flush();
57 expect(spy.calls.count()).toBe(2);
58 });
59
60 it('should even work when the throttled function invokes the throttle again',function() {
61 var spy = jasmine.createSpy('spy');
62 var f = $throttle(spy,1000,{
63 leading: false
64 });
65 spy.and.callFake(f);
66 f('foo');
67 f('foo');
68 f('foo');
69 expect(spy.calls.count()).toBe(0);
70 $timeout.flush();
71 expect(spy.calls.count()).toBe(1);
72 });
73});