index.js (913B)
1 const M = require('../'); 2 const test = require('tape'); 3 4 const running = {}; 5 6 async function worker(name) { 7 running[name] = running[name] || 0; 8 await M.lock(name); 9 running[name]++; 10 await new Promise(r => setTimeout(r,1000)); 11 M.unlock(name); 12 } 13 14 test('Method exports', t => { 15 t.plan(1); 16 17 t.equal(typeof M.dbounce, 'function', '\'dbounce\' must be a function'); 18 }); 19 20 test('Quick burst of calls returns single true', async t => { 21 t.plan(1); 22 let timesCalled = 0; 23 24 // Debounced function 25 async function callHandler() { 26 if (!await M.dbounce(`callHandler`, 50)) return; 27 timesCalled++; 28 } 29 30 // Call a bunch of times 31 for(let i=0; i<10; i++) { 32 callHandler(); 33 } 34 35 // Wait for it to finish 36 await new Promise(r => setTimeout(r,1000)); 37 38 // callHandler should've only been called once 39 t.equal(timesCalled, 1, 'dbounce returns true a single time for quick consecutive calls'); 40 });