SystemTickFeed.test.js (2286B)
1 import { 2 SYSTEM_TICK_INTERVAL, 3 SystemTickFeed, 4 } from "lib/SystemTickFeed.sys.mjs"; 5 import { actionTypes as at } from "common/Actions.mjs"; 6 import { GlobalOverrider } from "test/unit/utils"; 7 8 describe("System Tick Feed", () => { 9 let globals; 10 let instance; 11 let clock; 12 13 beforeEach(() => { 14 globals = new GlobalOverrider(); 15 clock = sinon.useFakeTimers(); 16 17 instance = new SystemTickFeed(); 18 instance.store = { 19 getState() { 20 return {}; 21 }, 22 dispatch() {}, 23 }; 24 }); 25 afterEach(() => { 26 globals.restore(); 27 clock.restore(); 28 }); 29 it("should create a SystemTickFeed", () => { 30 assert.instanceOf(instance, SystemTickFeed); 31 }); 32 it("should fire SYSTEM_TICK events at configured interval", () => { 33 globals.set("ChromeUtils", { 34 idleDispatch: f => f(), 35 }); 36 let expectation = sinon 37 .mock(instance.store) 38 .expects("dispatch") 39 .twice() 40 .withExactArgs({ type: at.SYSTEM_TICK }); 41 42 instance.onAction({ type: at.INIT }); 43 clock.tick(SYSTEM_TICK_INTERVAL * 2); 44 expectation.verify(); 45 }); 46 it("should not fire SYSTEM_TICK events after UNINIT", () => { 47 let expectation = sinon.mock(instance.store).expects("dispatch").never(); 48 49 instance.onAction({ type: at.UNINIT }); 50 clock.tick(SYSTEM_TICK_INTERVAL * 2); 51 expectation.verify(); 52 }); 53 it("should not fire SYSTEM_TICK events while the user is away", () => { 54 let expectation = sinon.mock(instance.store).expects("dispatch").never(); 55 56 instance.onAction({ type: at.INIT }); 57 instance._idleService = { idleTime: SYSTEM_TICK_INTERVAL + 1 }; 58 clock.tick(SYSTEM_TICK_INTERVAL * 3); 59 expectation.verify(); 60 instance.onAction({ type: at.UNINIT }); 61 }); 62 it("should fire SYSTEM_TICK immediately when the user is active again", () => { 63 globals.set("ChromeUtils", { 64 idleDispatch: f => f(), 65 }); 66 let expectation = sinon 67 .mock(instance.store) 68 .expects("dispatch") 69 .once() 70 .withExactArgs({ type: at.SYSTEM_TICK }); 71 72 instance.onAction({ type: at.INIT }); 73 instance._idleService = { idleTime: SYSTEM_TICK_INTERVAL + 1 }; 74 clock.tick(SYSTEM_TICK_INTERVAL * 3); 75 instance.observe(); 76 expectation.verify(); 77 instance.onAction({ type: at.UNINIT }); 78 }); 79 });