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;
24 sessionIdleTimeout: sessionIdleTimeout,
25 lastWarningDuration: lastWarningDuration,
28 doCloseWarn: jest.fn(),
30 mount(<div><AutoLogoutComponent {...props} /></div>);
33 it('should logout after idle timeout', () => {
34 jest.advanceTimersByTime((sessionIdleTimeout-1)*1000);
35 expect(props.doLogout).not.toBeCalled();
36 jest.advanceTimersByTime(1*1000);
37 expect(props.doLogout).toBeCalled();
40 it('should warn the user previous to close the session', () => {
41 jest.advanceTimersByTime((sessionIdleTimeout-lastWarningDuration-1)*1000);
42 expect(props.doWarn).not.toBeCalled();
43 jest.advanceTimersByTime(1*1000);
44 expect(props.doWarn).toBeCalled();
47 it('should reset the idle timer when activity event is received', () => {
48 jest.advanceTimersByTime((sessionIdleTimeout-lastWarningDuration-1)*1000);
49 expect(props.doWarn).not.toBeCalled();
50 // Simulate activity from other window/tab
51 eventListeners.storage({
52 key: LAST_ACTIVE_TIMESTAMP,
53 newValue: '42' // value currently doesn't matter
55 jest.advanceTimersByTime(1*1000);
56 // Warning should not appear because idle timer was reset
57 expect(props.doWarn).not.toBeCalled();