Skip to content

Commit

Permalink
pr review fixes
Browse files Browse the repository at this point in the history
- fix typos/syntax issues
- add JSDoc and try/catch block to session timeout warning component
- add hof logger to session timeout warning component
- remove beta version in package.json
- - amend content for session timeout warning readme and move it out of components folder into main readme to make it easier for users to find
- update yarn.lock
  • Loading branch information
Rhodine-orleans-lindsay committed Aug 16, 2024
1 parent c54f575 commit 47aabe9
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 80 deletions.
49 changes: 48 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@ Using the translation key `fields.field-name.label` will return different values
# HOF Components
#Date Component
# Date Component
A component for handling the rendering and processing of 3-input date fields used in HOF Applications.
Expand Down Expand Up @@ -1255,6 +1255,53 @@ transportOptions: {
Disables sending email. No options are required.
# Session Timeout Warning Component
HOF component for customising session timeout related pages
This feature allows you to customise the content related to the session timeout warning, including the messages displayed in the session timeout warning dialog and on the exit page after a user exits the form due to a session timeout.
## Usage
To enable and customize the session timeout behavior, you need to set the component in your project's `hof.settings.json` file:
```js
"behaviours": [
"hof/components/session-timeout-warning"
]
```
By default, the framework uses the standard content provided by HOF. If you wish to override this with custom content at the project level, you must set the following variables to `true` in `hof.settings.json`:
```js
behaviours: [
require('../').components.sessionTimeoutWarning
],
sessionTimeoutWarningContent: true,
exitFormContent: true
```
## Customising content in `pages.json`
Once the variables are set, you can customize the session timeout warning and exit messages in your project's pages.json:
```json
"exit": {
"message": "We have cleared your information to keep it secure. Your information has not been saved."
},
"session-timeout-warning": {
"dialog-title": "Your application will close soon",
"dialog-text": "If that happens, your progress will not be saved.",
"timeout-continue-button": "Stay on this page",
"dialog-exit-link": "Exit this form"
}
```
## Editing content on the Exit Page Header and Title
To edit the exit page's header and title, create an `exit.json` file in your project and set the desired content:
```json
{
"header": "You have left this form",
"title": "You have left this form"
}
```
# UTILITIES
# Autofill Utility
Expand Down
43 changes: 0 additions & 43 deletions components/session-timeout-warning/Readme.md

This file was deleted.

28 changes: 24 additions & 4 deletions components/session-timeout-warning/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
/**
*
* @fileOverview
* Provides custom behavior for handling session timeout warnings and exit actions. This includes
* - Resetting the session if the user exits due to a session timeout.
* - Customizing the session timeout warning dialog content.
* - Setting custom content and titles on the exit page.
*
* @module SessionTimeoutWarningBehavior
* @requires ../../config/hof-defaults
* @param {Class} superclass - The class to be extended.
* @returns {Class} - The extended class with session timeout handling functionality.
*/

'use strict';
const config = require('../../config/hof-defaults');
const logger = require('../../lib/logger')(config);

module.exports = superclass => class extends superclass {
configure(req, res, next) {
// reset the session if user chooses to exit on session timeout warning
if (req.form.options.route === '/exit') {
req.sessionModel.reset();
try {
// Reset the session if the user chooses to exit on session timeout warning
if (req.form.options.route === '/exit') {
req.sessionModel.reset();
logger.log('info', 'Session has been reset on exit');
}
return super.configure(req, res, next);
} catch (error) {
logger.error('Error during session reset:', error);
return next(error); // Pass the error to the next middleware for centralised handling
}
return super.configure(req, res, next);
}

locals(req, res) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "hof",
"description": "A bootstrap for HOF projects",
"version": "22.0.0-timeout-warning-beta.13",
"version": "22.0.0",
"license": "MIT",
"main": "index.js",
"author": "HomeOffice",
Expand Down
51 changes: 25 additions & 26 deletions test/components/session-timeout-warning.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const Component = require('../../components').sessionTimeoutWarning;
const { sessionTimeoutWarning: Component } = require('../../components');
const config = require('../../config/hof-defaults');

describe('session timeout warning component', () => {
Expand All @@ -22,6 +22,7 @@ describe('session timeout warning component', () => {
req.sessionModel.reset = resetStub;
next = sinon.stub();
});

describe("The 'configure' method ", () => {
beforeEach(() => {
sinon.stub(Base.prototype, 'configure').returns(req, res, next);
Expand Down Expand Up @@ -50,7 +51,7 @@ describe('session timeout warning component', () => {


afterEach(() => {
Base.prototype.configure.restore();
sinon.restore();
});
});

Expand All @@ -64,26 +65,28 @@ describe('session timeout warning component', () => {
res.locals = {
sessionTimeoutWarningContent: true
};
instance.locals(req, res);
instance.locals(req, res).should.have.property('dialogTitle')
.and.deep.equal(true);
instance.locals(req, res).should.have.property('dialogText')
.and.deep.equal(true);
instance.locals(req, res).should.have.property('timeoutContinueButton')
.and.deep.equal(true);
instance.locals(req, res).should.have.property('dialogExitLink')
.and.deep.equal(true);
const locals = instance.locals(req, res);
const checkDialogProperties = expected => {
locals.should.have.property('dialogTitle').and.deep.equal(expected);
locals.should.have.property('dialogText').and.deep.equal(expected);
locals.should.have.property('timeoutContinueButton').and.deep.equal(expected);
locals.should.have.property('dialogExitLink').and.deep.equal(expected);
};
checkDialogProperties(true);
});

it('does not set the dialog content to true if locals.sessionTimeoutWarningContent is set to false', () => {
res.locals = {
sessionTimeoutWarningContent: false
};
instance.locals(req, res);
instance.locals(req, res).should.not.have.property('dialogTitle');
instance.locals(req, res).should.not.have.property('dialogText');
instance.locals(req, res).should.not.have.property('timeoutContinueButton');
instance.locals(req, res).should.not.have.property('dialogExitLink');
const locals = instance.locals(req, res);
const checkDialogProperties = () => {
locals.should.not.have.property('dialogTitle');
locals.should.not.have.property('dialogText');
locals.should.not.have.property('timeoutContinueButton');
locals.should.not.have.property('dialogExitLink');
};
checkDialogProperties();
});

it('sets the custom content to true on the exit page if exitFormContent is set to true', () => {
Expand All @@ -93,9 +96,8 @@ describe('session timeout warning component', () => {
route: '/exit'
}
};
instance.locals(req, res);
instance.locals(req, res).should.have.property('exitFormContent')
.and.deep.equal(true);
const locals = instance.locals(req, res);
locals.should.have.property('exitFormContent').and.deep.equal(true);
});

it('does sets the default content on the exit page if exitFormContent is set to false', () => {
Expand All @@ -105,13 +107,10 @@ describe('session timeout warning component', () => {
route: '/exit'
}
};
instance.locals(req, res);
instance.locals(req, res).should.have.property('header')
.and.deep.equal('exit.header');
instance.locals(req, res).should.have.property('title')
.and.deep.equal('exit.title');
instance.locals(req, res).should.have.property('message')
.and.deep.equal('exit.message');
const locals = instance.locals(req, res);
locals.should.have.property('header').and.deep.equal('exit.header');
locals.should.have.property('title').and.deep.equal('exit.title');
locals.should.have.property('message').and.deep.equal('exit.message');
});

afterEach(() => {
Expand Down
3 changes: 1 addition & 2 deletions test/frontend/jest/sessionDialog.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ const fs = require('fs');
const path = require('path');
const sessionTimeoutWarningHtml = fs.readFileSync(path.resolve(__dirname, '../../../frontend/template-partials/views/partials/session-timeout-warning.html'), 'utf8');

jest
.dontMock('fs');
jest.dontMock('fs');

describe('sessionDialog', () => {
let sessionDialog;
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1766,9 +1766,9 @@ aws4@^1.8.0:
integrity sha512-3AungXC4I8kKsS9PuS4JH2nc+0bVY/mjgrephHTIi8fpEeGsTHBUJeosp0Wc1myYMElmD0B3Oc4XL/HVJ4PV2g==

axios@^1.5.1, axios@^1.6.1:
version "1.7.2"
resolved "https://registry.yarnpkg.com/axios/-/axios-1.7.2.tgz#b625db8a7051fbea61c35a3cbb3a1daa7b9c7621"
integrity sha512-2A8QhOMrbomlDuiLeK9XibIBzuHeRcqqNOHp0Cyp5EoJ1IFDh+XZH3A6BkXtv0K4gFGCI0Y4BM7B1wOEi0Rmgw==
version "1.7.4"
resolved "https://registry.yarnpkg.com/axios/-/axios-1.7.4.tgz#4c8ded1b43683c8dd362973c393f3ede24052aa2"
integrity sha512-DukmaFRnY6AzAALSH4J2M3k6PkaC+MfaAGdEERRWcC9q3/TWQwLpHR8ZRLKTdQ3aBDL64EdluRDjJqKw+BPZEw==
dependencies:
follow-redirects "^1.15.6"
form-data "^4.0.0"
Expand Down

0 comments on commit 47aabe9

Please sign in to comment.