1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import React from 'react';
6 import { configure, mount } from "enzyme";
7 import Adapter from 'enzyme-adapter-react-16';
8 import { AutoLogoutComponent, AutoLogoutProps, LAST_ACTIVE_TIMESTAMP } from './auto-logout';
10 configure({ adapter: new Adapter() });
12 describe('<AutoLogoutComponent />', () => {
13 let props: AutoLogoutProps;
14 const sessionIdleTimeout = 300;
15 const lastWarningDuration = 60;
16 const eventListeners = {};
20 window.addEventListener = jest.fn((event, cb) => {
21 eventListeners[event] = cb;
27 sessionIdleTimeout: sessionIdleTimeout,
28 lastWarningDuration: lastWarningDuration,
31 doCloseWarn: jest.fn(),
33 mount(<div><AutoLogoutComponent {...props} /></div>);
36 it('should logout after idle timeout', () => {
37 jest.runTimersToTime((sessionIdleTimeout-1)*1000);
38 expect(props.doLogout).not.toBeCalled();
39 jest.runTimersToTime(1*1000);
40 expect(props.doLogout).toBeCalled();
43 it('should warn the user previous to close the session', () => {
44 jest.runTimersToTime((sessionIdleTimeout-lastWarningDuration-1)*1000);
45 expect(props.doWarn).not.toBeCalled();
46 jest.runTimersToTime(1*1000);
47 expect(props.doWarn).toBeCalled();
50 it('should reset the idle timer when activity event is received', () => {
51 jest.runTimersToTime((sessionIdleTimeout-lastWarningDuration-1)*1000);
52 expect(props.doWarn).not.toBeCalled();
53 // Simulate activity from other window/tab
54 eventListeners.storage({
55 key: LAST_ACTIVE_TIMESTAMP,
56 newValue: '42' // value currently doesn't matter
58 jest.runTimersToTime(1*1000);
59 // Warning should not appear because idle timer was reset
60 expect(props.doWarn).not.toBeCalled();