16848: Adds test for idle timer reset via localStorage event.
authorLucas Di Pentima <lucas@di-pentima.com.ar>
Mon, 15 Feb 2021 21:03:30 +0000 (18:03 -0300)
committerLucas Di Pentima <lucas@di-pentima.com.ar>
Mon, 15 Feb 2021 21:03:30 +0000 (18:03 -0300)
Arvados-DCO-1.1-Signed-off-by: Lucas Di Pentima <lucas@di-pentima.com.ar>

src/views-components/auto-logout/auto-logout.test.tsx
src/views-components/auto-logout/auto-logout.tsx

index f8daa764f86d6068a73c782ce26514c1cd8f9025..4949672437b9530cb36ca6e9cd23058812721b97 100644 (file)
@@ -5,7 +5,7 @@
 import * as React from 'react';
 import { configure, mount } from "enzyme";
 import * as Adapter from 'enzyme-adapter-react-16';
-import { AutoLogoutComponent, AutoLogoutProps } from './auto-logout';
+import { AutoLogoutComponent, AutoLogoutProps, LAST_ACTIVE_TIMESTAMP } from './auto-logout';
 
 configure({ adapter: new Adapter() });
 
@@ -13,8 +13,15 @@ describe('<AutoLogoutComponent />', () => {
     let props: AutoLogoutProps;
     const sessionIdleTimeout = 300;
     const lastWarningDuration = 60;
+    const eventListeners = {};
     jest.useFakeTimers();
 
+    beforeAll(() => {
+        window.addEventListener = jest.fn((event, cb) => {
+            eventListeners[event] = cb;
+        });
+    });
+
     beforeEach(() => {
         props = {
             sessionIdleTimeout: sessionIdleTimeout,
@@ -39,4 +46,17 @@ describe('<AutoLogoutComponent />', () => {
         jest.runTimersToTime(1*1000);
         expect(props.doWarn).toBeCalled();
     });
+
+    it('should reset the idle timer when activity event is received', () => {
+        jest.runTimersToTime((sessionIdleTimeout-lastWarningDuration-1)*1000);
+        expect(props.doWarn).not.toBeCalled();
+        // Simulate activity from other window/tab
+        eventListeners.storage({
+            key: LAST_ACTIVE_TIMESTAMP,
+            newValue: '42' // value currently doesn't matter
+        })
+        jest.runTimersToTime(1*1000);
+        // Warning should not appear because idle timer was reset
+        expect(props.doWarn).not.toBeCalled();
+    });
 });
\ No newline at end of file
index 54411926d215d42d1898f58c96475c437668d402..f7e6f4b838d0082feff5c2bc8d9ff0f4d14c61cb 100644 (file)
@@ -50,7 +50,7 @@ const debounce = (delay: number | undefined, fn: Function) => {
     };
 };
 
-const LAST_ACTIVE_TIMESTAMP = 'lastActiveTimestamp';
+export const LAST_ACTIVE_TIMESTAMP = 'lastActiveTimestamp';
 
 export const AutoLogoutComponent = (props: AutoLogoutProps) => {
     let logoutTimer: NodeJS.Timer;