DigitalConsumer.ISS/src/helpers/session-helper.spec.js
2023-08-09 11:19:41 -04:00

89 lines
2.7 KiB
JavaScript

import * as cookieHelper from '@/helpers/cookie-helper';
import {
isAnalyticsSessionStillActive,
isSavedSessionStillActive,
getDateForSavedSessionTimeout
} from '@/helpers/session-helper';
import applicationConfig from '@/constants/application-config';
describe('isAnalyticsSessionStillActive', () => {
test('isAnalyticsSessionStillActive, should return true', () => {
// Arrange
const mockDate = new Date(new Date().toUTCString());
mockDate.setDate(mockDate.getDate() + 1);
cookieHelper.getISSCookie = jest
.spyOn(cookieHelper, 'getISSCookie')
.mockReturnValue({ LastTouched: mockDate });
// Act
const result = isAnalyticsSessionStillActive();
// Assert
expect(result).toBe(true);
});
test('isAnalyticsSessionStillActive, should return false', () => {
// Arrange
const mockDate = new Date(new Date().toUTCString());
mockDate.setDate(mockDate.getDate() - 1);
cookieHelper.getISSCookie = jest
.spyOn(cookieHelper, 'getISSCookie')
.mockReturnValue({ LastTouched: mockDate });
// Act
const result = isAnalyticsSessionStillActive();
// Assert
expect(result).toBe(false);
});
});
describe('isSavedSessionStillActive', () => {
test('isSavedSessionStillActive, should return true', () => {
// Arrange
const mockDate = new Date(new Date().toUTCString());
mockDate.setDate(mockDate.getDate() + 1);
cookieHelper.getISSCookie = jest
.spyOn(cookieHelper, 'getISSCookie')
.mockReturnValue({ SavedSessionTimeoutDate: mockDate });
// Act
const result = isSavedSessionStillActive();
// Assert
expect(result).toBe(true);
});
test('isSavedSessionStillActive, should return false', () => {
// Arrange
const mockDate = new Date(new Date().toUTCString());
mockDate.setDate(mockDate.getDate() - 1);
cookieHelper.getISSCookie = jest
.spyOn(cookieHelper, 'getISSCookie')
.mockReturnValue({ SavedSessionTimeoutDate: mockDate });
// Act
const result = isSavedSessionStillActive();
// Assert
expect(result).toBe(false);
});
});
describe('getDateForSavedSessionTimeout', () => {
test('getDateForSavedSessionTimeout, should equal application config setting', () => {
// Arrange
const currentDate = new Date(new Date().toUTCString());
currentDate.setDate(currentDate.getDate() + applicationConfig.SAVED_SESSION_TIMEOUT_DAYS);
// Act
const result = getDateForSavedSessionTimeout();
// Assert
expect(result).toEqual(currentDate.toUTCString());
});
});