debounce.test.js (1405B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const expect = require("expect"); 7 8 const { 9 debounceActions, 10 } = require("resource://devtools/client/shared/redux/middleware/debounce.js"); 11 12 describe("Debounce Middleware", () => { 13 let nextArgs = []; 14 const fakeStore = {}; 15 const fakeNext = (...args) => { 16 nextArgs.push(args); 17 }; 18 19 beforeEach(() => { 20 nextArgs = []; 21 }); 22 23 it("should pass the intercepted action to next", () => { 24 const fakeAction = { 25 type: "FAKE_ACTION", 26 }; 27 28 debounceActions()(fakeStore)(fakeNext)(fakeAction); 29 30 expect(nextArgs.length).toEqual(1); 31 expect(nextArgs[0]).toEqual([fakeAction]); 32 }); 33 34 it("should debounce if specified", () => { 35 const fakeAction = { 36 type: "FAKE_ACTION", 37 meta: { 38 debounce: true, 39 }, 40 }; 41 42 const executed = debounceActions(1, 1)(fakeStore)(fakeNext)(fakeAction); 43 expect(nextArgs.length).toEqual(0); 44 45 return executed.then(() => { 46 expect(nextArgs.length).toEqual(1); 47 }); 48 }); 49 50 it("should have no effect if no timeout", () => { 51 const fakeAction = { 52 type: "FAKE_ACTION", 53 meta: { 54 debounce: true, 55 }, 56 }; 57 58 debounceActions()(fakeStore)(fakeNext)(fakeAction); 59 expect(nextArgs.length).toEqual(1); 60 expect(nextArgs[0]).toEqual([fakeAction]); 61 }); 62 });