17782: Fixes almost all tests (4 left) mostly by fixing namespace-type imports.
[arvados-workbench2.git] / 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     jest.useFakeTimers();
18
19     beforeAll(() => {
20         window.addEventListener = jest.fn((event, cb) => {
21             eventListeners[event] = cb;
22         });
23     });
24
25     beforeEach(() => {
26         props = {
27             sessionIdleTimeout: sessionIdleTimeout,
28             lastWarningDuration: lastWarningDuration,
29             doLogout: jest.fn(),
30             doWarn: jest.fn(),
31             doCloseWarn: jest.fn(),
32         };
33         mount(<div><AutoLogoutComponent {...props} /></div>);
34     });
35
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();
41     });
42
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();
48     });
49
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
57         })
58         jest.runTimersToTime(1*1000);
59         // Warning should not appear because idle timer was reset
60         expect(props.doWarn).not.toBeCalled();
61     });
62 });