-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(EMS-3877): business - turnover currency - save and back
- Loading branch information
Showing
10 changed files
with
203 additions
and
30 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
125 changes: 125 additions & 0 deletions
125
src/ui/server/controllers/insurance/business/turnover-currency/save-and-back/index.test.ts
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,125 @@ | ||
import { FIELD_IDS } from '..'; | ||
import { post } from '.'; | ||
import { INSURANCE_ROUTES } from '../../../../../constants/routes/insurance'; | ||
import INSURANCE_FIELD_IDS from '../../../../../constants/field-ids/insurance'; | ||
import constructPayload from '../../../../../helpers/construct-payload'; | ||
import generateValidationErrors from '../validation'; | ||
import mapAndSave from '../../map-and-save/turnover'; | ||
import { Request, Response } from '../../../../../../types'; | ||
import { EUR, mockReq, mockRes, mockSpyPromiseRejection, referenceNumber } from '../../../../../test-mocks'; | ||
|
||
const { | ||
CURRENCY: { CURRENCY_CODE }, | ||
} = INSURANCE_FIELD_IDS; | ||
|
||
const { INSURANCE_ROOT, ALL_SECTIONS, PROBLEM_WITH_SERVICE } = INSURANCE_ROUTES; | ||
|
||
describe('controllers/insurance/business/turnover-currency/save-and-back', () => { | ||
let req: Request; | ||
let res: Response; | ||
|
||
jest.mock('../../map-and-save/turnover'); | ||
|
||
let mapAndSaveSpy = jest.fn(() => Promise.resolve(true)); | ||
|
||
const mockFormBody = { | ||
[CURRENCY_CODE]: EUR.isoCode, | ||
}; | ||
|
||
beforeEach(() => { | ||
req = mockReq(); | ||
res = mockRes(); | ||
|
||
req.body = mockFormBody; | ||
|
||
mapAndSave.turnover = mapAndSaveSpy; | ||
}); | ||
|
||
afterAll(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
describe('when the form has data', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
|
||
mapAndSaveSpy = jest.fn(() => Promise.resolve(true)); | ||
mapAndSave.turnover = mapAndSaveSpy; | ||
}); | ||
|
||
it('should call mapAndSave.turnover with data from constructPayload function, application and validationErrors', async () => { | ||
await post(req, res); | ||
|
||
const payload = constructPayload(req.body, FIELD_IDS); | ||
|
||
const validationErrors = generateValidationErrors(payload); | ||
|
||
expect(mapAndSave.turnover).toHaveBeenCalledTimes(1); | ||
expect(mapAndSave.turnover).toHaveBeenCalledWith(payload, res.locals.application, validationErrors); | ||
}); | ||
|
||
it(`should redirect to ${ALL_SECTIONS}`, async () => { | ||
mapAndSave.turnover = mapAndSaveSpy; | ||
|
||
await post(req, res); | ||
|
||
const expected = `${INSURANCE_ROOT}/${referenceNumber}${ALL_SECTIONS}`; | ||
|
||
expect(res.redirect).toHaveBeenCalledWith(expected); | ||
}); | ||
}); | ||
|
||
describe('when the form does not have any data', () => { | ||
it(`should redirect to ${ALL_SECTIONS}`, async () => { | ||
req.body = { _csrf: '1234' }; | ||
|
||
await post(req, res); | ||
|
||
const expected = `${INSURANCE_ROOT}/${referenceNumber}${ALL_SECTIONS}`; | ||
|
||
expect(res.redirect).toHaveBeenCalledWith(expected); | ||
}); | ||
}); | ||
|
||
describe('when there is no application', () => { | ||
beforeEach(() => { | ||
delete res.locals.application; | ||
}); | ||
|
||
it(`should redirect to ${PROBLEM_WITH_SERVICE}`, async () => { | ||
await post(req, res); | ||
|
||
expect(res.redirect).toHaveBeenCalledWith(PROBLEM_WITH_SERVICE); | ||
}); | ||
}); | ||
|
||
describe('api error handling', () => { | ||
describe('when mapAndSave.turnover returns false', () => { | ||
beforeEach(() => { | ||
res.locals = mockRes().locals; | ||
mapAndSaveSpy = jest.fn(() => Promise.resolve(false)); | ||
mapAndSave.turnover = mapAndSaveSpy; | ||
}); | ||
|
||
it(`should redirect to ${PROBLEM_WITH_SERVICE}`, async () => { | ||
await post(req, res); | ||
|
||
expect(res.redirect).toHaveBeenCalledWith(PROBLEM_WITH_SERVICE); | ||
}); | ||
}); | ||
|
||
describe('when mapAndSave.turnover fails', () => { | ||
beforeEach(() => { | ||
res.locals = mockRes().locals; | ||
mapAndSaveSpy = mockSpyPromiseRejection; | ||
mapAndSave.turnover = mapAndSaveSpy; | ||
}); | ||
|
||
it(`should redirect to ${PROBLEM_WITH_SERVICE}`, async () => { | ||
await post(req, res); | ||
|
||
expect(res.redirect).toHaveBeenCalledWith(PROBLEM_WITH_SERVICE); | ||
}); | ||
}); | ||
}); | ||
}); |
53 changes: 53 additions & 0 deletions
53
src/ui/server/controllers/insurance/business/turnover-currency/save-and-back/index.ts
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,53 @@ | ||
import { INSURANCE_ROUTES } from '../../../../../constants/routes/insurance'; | ||
import hasFormData from '../../../../../helpers/has-form-data'; | ||
import { FIELD_IDS } from '..'; | ||
import constructPayload from '../../../../../helpers/construct-payload'; | ||
import generateValidationErrors from '../validation'; | ||
import mapAndSave from '../../map-and-save/turnover'; | ||
import { Request, Response } from '../../../../../../types'; | ||
|
||
const { INSURANCE_ROOT, ALL_SECTIONS, PROBLEM_WITH_SERVICE } = INSURANCE_ROUTES; | ||
|
||
/** | ||
* post | ||
* Save any valid currency of Business - Turnover - Currency form fields and if successful, redirect to the all sections page | ||
* @param {Express.Request} Express request | ||
* @param {Express.Response} Express response | ||
* @returns {Express.Response.redirect} All sections page or error page | ||
*/ | ||
export const post = async (req: Request, res: Response) => { | ||
try { | ||
const { application } = res.locals; | ||
|
||
if (!application) { | ||
return res.redirect(PROBLEM_WITH_SERVICE); | ||
} | ||
|
||
const { referenceNumber } = req.params; | ||
|
||
/** | ||
* If form data is populated: | ||
* 1) generate a payload. | ||
* 2) generate validation errors. | ||
* 3) call mapAndSave | ||
* 4) redirect | ||
*/ | ||
if (hasFormData(req.body)) { | ||
const payload = constructPayload(req.body, FIELD_IDS); | ||
|
||
const validationErrors = generateValidationErrors(payload); | ||
|
||
const saveResponse = await mapAndSave.turnover(payload, application, validationErrors); | ||
|
||
if (!saveResponse) { | ||
return res.redirect(PROBLEM_WITH_SERVICE); | ||
} | ||
} | ||
|
||
return res.redirect(`${INSURANCE_ROOT}/${referenceNumber}${ALL_SECTIONS}`); | ||
} catch (error) { | ||
console.error('Error updating application - business - turnover currency (save and back) %O', error); | ||
|
||
return res.redirect(PROBLEM_WITH_SERVICE); | ||
} | ||
}; |
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