From: Lucas Di Pentima Date: Mon, 15 Feb 2021 21:03:30 +0000 (-0300) Subject: 16848: Adds test for idle timer reset via localStorage event. X-Git-Tag: 2.1.2.1~10^2~17 X-Git-Url: https://git.arvados.org/arvados-workbench2.git/commitdiff_plain/b7eb7d60846333d93e91f98ec3e6fcdd94fca570 16848: Adds test for idle timer reset via localStorage event. Arvados-DCO-1.1-Signed-off-by: Lucas Di Pentima --- diff --git a/src/views-components/auto-logout/auto-logout.test.tsx b/src/views-components/auto-logout/auto-logout.test.tsx index f8daa764..49496724 100644 --- a/src/views-components/auto-logout/auto-logout.test.tsx +++ b/src/views-components/auto-logout/auto-logout.test.tsx @@ -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('', () => { 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('', () => { 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 diff --git a/src/views-components/auto-logout/auto-logout.tsx b/src/views-components/auto-logout/auto-logout.tsx index 54411926..f7e6f4b8 100644 --- a/src/views-components/auto-logout/auto-logout.tsx +++ b/src/views-components/auto-logout/auto-logout.tsx @@ -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;