|
| 1 | +import { Request } from 'express' |
| 2 | +import { SessionData } from 'express-session' |
| 3 | +import { updateSessionData } from './session' |
| 4 | + |
| 5 | +describe(updateSessionData.name, () => { |
| 6 | + let req: Request |
| 7 | + |
| 8 | + beforeEach(() => { |
| 9 | + req = { |
| 10 | + session: {} as SessionData, |
| 11 | + } as Request |
| 12 | + }) |
| 13 | + |
| 14 | + it('should initialize applicationData if it does not exist', () => { |
| 15 | + updateSessionData(req, { prisonerName: 'John Doe' }) |
| 16 | + |
| 17 | + expect(req.session.applicationData).toEqual({ |
| 18 | + prisonerName: 'John Doe', |
| 19 | + }) |
| 20 | + }) |
| 21 | + |
| 22 | + it('should update an existing field without removing other fields', () => { |
| 23 | + req.session.applicationData = { |
| 24 | + type: { name: 'Swap VOs', value: 'swap-vos' }, |
| 25 | + prisonerName: 'Jane Doe', |
| 26 | + } |
| 27 | + |
| 28 | + updateSessionData(req, { prisonerName: 'John Doe' }) |
| 29 | + |
| 30 | + expect(req.session.applicationData).toEqual({ |
| 31 | + type: { name: 'Swap VOs', value: 'swap-vos' }, |
| 32 | + prisonerName: 'John Doe', |
| 33 | + }) |
| 34 | + }) |
| 35 | + |
| 36 | + it('should add new fields while keeping existing ones', () => { |
| 37 | + req.session.applicationData = { |
| 38 | + type: { name: 'Swap VOs', value: 'swap-vos' }, |
| 39 | + } |
| 40 | + |
| 41 | + updateSessionData(req, { prisonerName: 'John Doe', date: new Date('2024-02-05') }) |
| 42 | + |
| 43 | + expect(req.session.applicationData).toEqual({ |
| 44 | + type: { name: 'Swap VOs', value: 'swap-vos' }, |
| 45 | + prisonerName: 'John Doe', |
| 46 | + date: new Date('2024-02-05'), |
| 47 | + }) |
| 48 | + }) |
| 49 | + |
| 50 | + it('should not overwrite nested objects but merge updates', () => { |
| 51 | + req.session.applicationData = { |
| 52 | + type: { name: 'Swap VOs', value: 'swap-vos' }, |
| 53 | + additionalData: { swapVOsToPinCreditDetails: 'Old value' }, |
| 54 | + } |
| 55 | + |
| 56 | + updateSessionData(req, { |
| 57 | + additionalData: { swapVOsToPinCreditDetails: 'New value' }, |
| 58 | + }) |
| 59 | + |
| 60 | + expect(req.session.applicationData).toEqual({ |
| 61 | + type: { name: 'Swap VOs', value: 'swap-vos' }, |
| 62 | + additionalData: { swapVOsToPinCreditDetails: 'New value' }, |
| 63 | + }) |
| 64 | + }) |
| 65 | + |
| 66 | + it('should handle undefined session gracefully', () => { |
| 67 | + req.session = undefined |
| 68 | + |
| 69 | + expect(() => updateSessionData(req, { prisonerName: 'John Doe' })).toThrow() |
| 70 | + }) |
| 71 | +}) |
0 commit comments