detect-user-session-start.test.js (3709B)
1 import { actionCreators as ac, actionTypes as at } from "common/Actions.mjs"; 2 import { DetectUserSessionStart } from "content-src/lib/detect-user-session-start"; 3 4 describe("detectUserSessionStart", () => { 5 let store; 6 class PerfService { 7 getMostRecentAbsMarkStartByName() { 8 return 1234; 9 } 10 mark() {} 11 } 12 13 beforeEach(() => { 14 store = { dispatch: () => {} }; 15 }); 16 describe("#sendEventOrAddListener", () => { 17 it("should call ._sendEvent immediately if the document is visible", () => { 18 const mockDocument = { visibilityState: "visible" }; 19 const instance = new DetectUserSessionStart(store, { 20 document: mockDocument, 21 }); 22 sinon.stub(instance, "_sendEvent"); 23 24 instance.sendEventOrAddListener(); 25 26 assert.calledOnce(instance._sendEvent); 27 }); 28 it("should add an event listener on visibility changes the document is not visible", () => { 29 const mockDocument = { 30 visibilityState: "hidden", 31 addEventListener: sinon.spy(), 32 }; 33 const instance = new DetectUserSessionStart(store, { 34 document: mockDocument, 35 }); 36 sinon.stub(instance, "_sendEvent"); 37 38 instance.sendEventOrAddListener(); 39 40 assert.notCalled(instance._sendEvent); 41 assert.calledWith( 42 mockDocument.addEventListener, 43 "visibilitychange", 44 instance._onVisibilityChange 45 ); 46 }); 47 }); 48 describe("#_sendEvent", () => { 49 it("should dispatch an action with the SAVE_SESSION_PERF_DATA", () => { 50 const dispatch = sinon.spy(store, "dispatch"); 51 const instance = new DetectUserSessionStart(store); 52 53 instance._sendEvent(); 54 55 assert.calledWith( 56 dispatch, 57 ac.AlsoToMain({ 58 type: at.SAVE_SESSION_PERF_DATA, 59 data: { 60 visibility_event_rcvd_ts: sinon.match.number, 61 window_inner_width: sinon.match.number, 62 window_inner_height: sinon.match.number, 63 }, 64 }) 65 ); 66 }); 67 68 it("shouldn't send a message if getMostRecentAbsMarkStartByName throws", () => { 69 let perfService = new PerfService(); 70 sinon.stub(perfService, "getMostRecentAbsMarkStartByName").throws(); 71 const dispatch = sinon.spy(store, "dispatch"); 72 const instance = new DetectUserSessionStart(store, { perfService }); 73 74 instance._sendEvent(); 75 76 assert.notCalled(dispatch); 77 }); 78 79 it('should call perfService.mark("visibility_event_rcvd_ts")', () => { 80 let perfService = new PerfService(); 81 sinon.stub(perfService, "mark"); 82 const instance = new DetectUserSessionStart(store, { perfService }); 83 84 instance._sendEvent(); 85 86 assert.calledWith(perfService.mark, "visibility_event_rcvd_ts"); 87 }); 88 }); 89 90 describe("_onVisibilityChange", () => { 91 it("should not send an event if visiblity is not visible", () => { 92 const instance = new DetectUserSessionStart(store, { 93 document: { visibilityState: "hidden" }, 94 }); 95 sinon.stub(instance, "_sendEvent"); 96 97 instance._onVisibilityChange(); 98 99 assert.notCalled(instance._sendEvent); 100 }); 101 it("should send an event and remove the event listener if visibility is visible", () => { 102 const mockDocument = { 103 visibilityState: "visible", 104 removeEventListener: sinon.spy(), 105 }; 106 const instance = new DetectUserSessionStart(store, { 107 document: mockDocument, 108 }); 109 sinon.stub(instance, "_sendEvent"); 110 111 instance._onVisibilityChange(); 112 113 assert.calledOnce(instance._sendEvent); 114 assert.calledWith( 115 mockDocument.removeEventListener, 116 "visibilitychange", 117 instance._onVisibilityChange 118 ); 119 }); 120 }); 121 });