BlueGit

blue-star danique-bot

Files

danique-bot / node_modules / throttleit / test.js

test.js

Import template
timfa Tim · pushed by timfa · 4 years ago
c788ad20 History
test.js 1.5 KiB Download Raw
1var assert = require('assert');
2var throttle = require('./');
3
4describe('throttle', function(){
5 function counter() {
6 function count(){
7 count.invoked++;
8 }
9 count.invoked = 0;
10 return count;
11 }
12
13 it('should throttle a function', function(done){
14 var count = counter();
15 var wait = 100;
16 var total = 500;
17 var fn = throttle(count, wait);
18 var interval = setInterval(fn, 20);
19 setTimeout(function(){
20 clearInterval(interval);
21 assert(count.invoked === (total / wait));
22 done();
23 }, total + 5);
24 });
25
26 it('should call the function last time', function(done){
27 var count = counter();
28 var wait = 100;
29 var fn = throttle(count, wait);
30 fn();
31 fn();
32 assert(count.invoked === 1);
33 setTimeout(function(){
34 assert(count.invoked === 2);
35 done();
36 }, wait + 5);
37 });
38
39 it('should pass last context', function(done){
40 var wait = 100;
41 var ctx;
42 var fn = throttle(logctx, wait);
43 var foo = {};
44 var bar = {};
45 fn.call(foo);
46 fn.call(bar);
47 assert(ctx === foo);
48 setTimeout(function(){
49 assert(ctx === bar);
50 done();
51 }, wait + 5);
52 function logctx() {
53 ctx = this;
54 }
55 });
56
57 it('should pass last arguments', function(done){
58 var wait = 100;
59 var args;
60 var fn = throttle(logargs, wait);
61 fn.call(null, 1);
62 fn.call(null, 2);
63 assert(args && args[0] === 1);
64 setTimeout(function(){
65 assert(args && args[0] === 2);
66 done();
67 }, wait + 5);
68 function logargs() {
69 args = arguments;
70 }
71 });
72
73});