21700: Install Bundler system-wide in Rails postinst
[arvados.git] / services / workbench2 / src / views-components / auto-logout / auto-logout.test.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
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';
9
10 configure({ adapter: new Adapter() });
11
12 describe('<AutoLogoutComponent />', () => {
13     let props: AutoLogoutProps;
14     const sessionIdleTimeout = 300;
15     const lastWarningDuration = 60;
16     const eventListeners = {};
17
18     beforeEach(() => {
19         jest.useFakeTimers();
20         window.addEventListener = jest.fn((event, cb) => {
21             eventListeners[event] = cb;
22         });
23         props = {
24             sessionIdleTimeout: sessionIdleTimeout,
25             lastWarningDuration: lastWarningDuration,
26             doLogout: jest.fn(),
27             doWarn: jest.fn(),
28             doCloseWarn: jest.fn(),
29         };
30         mount(<div><AutoLogoutComponent {...props} /></div>);
31     });
32
33     it('should logout after idle timeout', () => {
34         jest.runTimersToTime((sessionIdleTimeout-1)*1000);
35         expect(props.doLogout).not.toBeCalled();
36         jest.runTimersToTime(1*1000);
37         expect(props.doLogout).toBeCalled();
38     });
39
40     it('should warn the user previous to close the session', () => {
41         jest.runTimersToTime((sessionIdleTimeout-lastWarningDuration-1)*1000);
42         expect(props.doWarn).not.toBeCalled();
43         jest.runTimersToTime(1*1000);
44         expect(props.doWarn).toBeCalled();
45     });
46
47     it('should reset the idle timer when activity event is received', () => {
48         jest.runTimersToTime((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
54         })
55         jest.runTimersToTime(1*1000);
56         // Warning should not appear because idle timer was reset
57         expect(props.doWarn).not.toBeCalled();
58     });
59 });