generated from ministryofjustice/hmpps-template-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
97 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { Request } from 'express' | ||
import { SessionData } from 'express-session' | ||
import { updateSessionData } from './session' | ||
|
||
describe(updateSessionData.name, () => { | ||
let req: Request | ||
|
||
beforeEach(() => { | ||
req = { | ||
session: {} as SessionData, | ||
} as Request | ||
}) | ||
|
||
it('should initialize applicationData if it does not exist', () => { | ||
updateSessionData(req, { prisonerName: 'John Doe' }) | ||
|
||
expect(req.session.applicationData).toEqual({ | ||
prisonerName: 'John Doe', | ||
}) | ||
}) | ||
|
||
it('should update an existing field without removing other fields', () => { | ||
req.session.applicationData = { | ||
type: { name: 'Swap VOs', value: 'swap-vos' }, | ||
prisonerName: 'Jane Doe', | ||
} | ||
|
||
updateSessionData(req, { prisonerName: 'John Doe' }) | ||
|
||
expect(req.session.applicationData).toEqual({ | ||
type: { name: 'Swap VOs', value: 'swap-vos' }, | ||
prisonerName: 'John Doe', | ||
}) | ||
}) | ||
|
||
it('should add new fields while keeping existing ones', () => { | ||
req.session.applicationData = { | ||
type: { name: 'Swap VOs', value: 'swap-vos' }, | ||
} | ||
|
||
updateSessionData(req, { prisonerName: 'John Doe', date: new Date('2024-02-05') }) | ||
|
||
expect(req.session.applicationData).toEqual({ | ||
type: { name: 'Swap VOs', value: 'swap-vos' }, | ||
prisonerName: 'John Doe', | ||
date: new Date('2024-02-05'), | ||
}) | ||
}) | ||
|
||
it('should not overwrite nested objects but merge updates', () => { | ||
req.session.applicationData = { | ||
type: { name: 'Swap VOs', value: 'swap-vos' }, | ||
additionalData: { swapVOsToPinCreditDetails: 'Old value' }, | ||
} | ||
|
||
updateSessionData(req, { | ||
additionalData: { swapVOsToPinCreditDetails: 'New value' }, | ||
}) | ||
|
||
expect(req.session.applicationData).toEqual({ | ||
type: { name: 'Swap VOs', value: 'swap-vos' }, | ||
additionalData: { swapVOsToPinCreditDetails: 'New value' }, | ||
}) | ||
}) | ||
|
||
it('should handle undefined session gracefully', () => { | ||
req.session = undefined | ||
|
||
expect(() => updateSessionData(req, { prisonerName: 'John Doe' })).toThrow() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Request } from 'express' | ||
import { SessionData } from 'express-session' | ||
|
||
// eslint-disable-next-line import/prefer-default-export | ||
export const updateSessionData = (req: Request, updates: Partial<SessionData['applicationData']>) => { | ||
if (!req.session.applicationData) { | ||
req.session.applicationData = {} as SessionData['applicationData'] | ||
} | ||
|
||
req.session.applicationData = { | ||
...req.session.applicationData, | ||
...updates, | ||
} | ||
} |