Merge branch '22617-fix-encrypted-partitions'
[arvados.git] / services / workbench2 / src / views-components / auto-logout / auto-logout.cy.js
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 { AutoLogoutComponent, LAST_ACTIVE_TIMESTAMP } from './auto-logout';
7
8 describe('<AutoLogoutComponent />', () => {
9     let props;
10     const sessionIdleTimeout = 300;
11     const lastWarningDuration = 60;
12     const eventListeners = {};
13
14     beforeEach(() => {
15         cy.clock();
16         window.addEventListener = cy.stub((event, cb) => {
17             eventListeners[event] = cb;
18         });
19         props = {
20             sessionIdleTimeout: sessionIdleTimeout,
21             lastWarningDuration: lastWarningDuration,
22             doLogout: cy.spy().as('doLogout'),
23             doWarn: cy.stub().as('doWarn'),
24             doCloseWarn: cy.stub(),
25         };
26         cy.mount(<div><AutoLogoutComponent {...props} /></div>);
27     });
28
29     afterEach(() => {
30         cy.clock().invoke('restore');
31     });
32
33     it('should logout after idle timeout', () => {
34         cy.tick((sessionIdleTimeout-1)*1000);
35         cy.get('@doLogout').should('not.have.been.called');
36         cy.tick(1000);
37         cy.get('@doLogout').should('have.been.called');
38     });
39
40     it('should warn the user previous to close the session', () => {
41         cy.tick((sessionIdleTimeout-lastWarningDuration-1)*1000);
42         cy.get('@doWarn').should('not.have.been.called');
43         cy.tick(1000);
44         cy.get('@doWarn').should('have.been.called');
45     });
46
47     it('should reset the idle timer when activity event is received', () => {
48         cy.tick((sessionIdleTimeout-lastWarningDuration-1)*1000);
49         cy.get('@doWarn').should('not.have.been.called');
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         cy.tick(1000);
56         // Warning should not appear because idle timer was reset
57         cy.get('@doWarn').should('not.have.been.called');
58     });
59 });