|
| 1 | +import { MessageChannel } from 'worker_threads'; |
| 2 | + |
| 3 | +import React from 'react'; |
| 4 | +import { |
| 5 | + render, |
| 6 | + cleanup, |
| 7 | + act, |
| 8 | + fireEvent, |
| 9 | + waitFor, |
| 10 | +} from '@testing-library/react'; |
| 11 | + |
| 12 | +import authService from '../../authentication/authService'; |
| 13 | +import IdleTimer from '../IdleTimer'; |
| 14 | + |
| 15 | +const originalEnv = process.env; |
| 16 | + |
| 17 | +beforeAll(() => { |
| 18 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 19 | + // @ts-ignore |
| 20 | + global.MessageChannel = MessageChannel; |
| 21 | + process.env = { |
| 22 | + ...originalEnv, |
| 23 | + REACT_APP_IDLE_TIMEOUT_IN_MS: '3600000', |
| 24 | + }; |
| 25 | + jest.useFakeTimers(); |
| 26 | +}); |
| 27 | + |
| 28 | +afterAll(() => { |
| 29 | + cleanup(); |
| 30 | + process.env = originalEnv; |
| 31 | + jest.useFakeTimers(); |
| 32 | +}); |
| 33 | + |
| 34 | +test('check idle timer has logged out after 60min and 1s', async () => { |
| 35 | + render(<IdleTimer />); |
| 36 | + const start = Date.now(); |
| 37 | + |
| 38 | + act(() => { |
| 39 | + jest.setSystemTime(start + 1000 * 60 * 60 + 1); |
| 40 | + fireEvent.focus(document); |
| 41 | + }); |
| 42 | + |
| 43 | + jest.spyOn(authService, 'isAuthenticated').mockReturnValueOnce(true); |
| 44 | + jest.spyOn(authService, 'logout'); |
| 45 | + await waitFor(() => { |
| 46 | + expect(authService.logout()).resolves.toEqual(1); |
| 47 | + }); |
| 48 | +}); |
| 49 | + |
| 50 | +test('check idle timer has not logged out after 50min', async () => { |
| 51 | + render(<IdleTimer />); |
| 52 | + const start = Date.now(); |
| 53 | + |
| 54 | + act(() => { |
| 55 | + jest.setSystemTime(start + 1000 * 60 * 50); |
| 56 | + fireEvent.focus(document); |
| 57 | + }); |
| 58 | + |
| 59 | + jest.spyOn(authService, 'isAuthenticated').mockReturnValueOnce(true); |
| 60 | + jest.spyOn(authService, 'logout'); |
| 61 | + expect(authService.logout()).resolves.toEqual(0); |
| 62 | +}); |
0 commit comments