Skip to content

Commit

Permalink
feat(EMS-3973): broker details - redirects (#3321)
Browse files Browse the repository at this point in the history
* chore(EMS-3966): update dependabot to run during non working hours (#3278)

* chore(EMS-3966): update dependabot to run during non working hours

* chore(EMS-3966): update dependabot to run weekly

* chore(EMS-3966): update dependabot to run weekly

* chore(EMS-3966): update dependabot to run monthly

* feat(EMS-3972): broker details - based in the uk form fields

* feat(EMS-3972): remove todo comment

* feat(EMS-3972): update todo comment

* fix(EMS-3988): quote tool - type of policy - hint text

* chore(main): release 2.36.1 (#3300)

* fix(EMS-3988): regenerate api schema (#3301)

* chore(main): release 2.36.2 (#3302)

* feat(EMS-3972): revert test mock changes

* feat(EMS-3972): update/disable e2e tests

* feat(EMS-3977): broker details - form validation

* feat(EMS-3977): update sql dump

* feat(EMS-3977): remove todo comments

* feat(EMS-3973): broker details - redirects

* chore(EMS-3973): fix merge issue

* chore(EMS-3973): update broker manual address template

* feat(EMS-3973): fix/update e2e tests

---------

Co-authored-by: Abhi Markan <32125108+abhi-markan@users.noreply.github.com>
  • Loading branch information
ttbarnes and abhi-markan authored Dec 5, 2024
1 parent 47db077 commit 754632e
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 35 deletions.
3 changes: 1 addition & 2 deletions e2e-tests/commands/insurance/complete-policy-section.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ const completePolicySection = ({
if (usingBroker) {
cy.completeAndSubmitBrokerDetailsForm({});

// submit the "confirm broker address" form
cy.clickSubmitButton();
cy.completeAndSubmitBrokerManualAddressForm({});
}

cy.completeAndSubmitLossPayeeForm({ isAppointingLossPayee });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const {

const {
ROOT,
POLICY: { BROKER_ROOT, BROKER_DETAILS_ROOT, BROKER_CONFIRM_ADDRESS_ROOT },
POLICY: { BROKER_ROOT, BROKER_DETAILS_ROOT, BROKER_ADDRESSES_ROOT, BROKER_MANUAL_ADDRESS_ROOT },
} = INSURANCE_ROUTES;

const { BROKER_DETAILS: FIELD_STRINGS } = FIELDS;
Expand All @@ -26,7 +26,8 @@ context(
() => {
let referenceNumber;
let url;
let brokerConfirmAddressUrl;
let brokerAddressesUrl;
let brokerManualAddressUrl;

before(() => {
cy.completeSignInAndGoToApplication({}).then(({ referenceNumber: refNumber }) => {
Expand All @@ -36,7 +37,8 @@ context(
cy.completeAndSubmitPolicyForms({ stopSubmittingAfter: 'broker', usingBroker: true });

url = `${baseUrl}${ROOT}/${referenceNumber}${BROKER_DETAILS_ROOT}`;
brokerConfirmAddressUrl = `${baseUrl}${ROOT}/${referenceNumber}${BROKER_CONFIRM_ADDRESS_ROOT}`;
brokerAddressesUrl = `${baseUrl}${ROOT}/${referenceNumber}${BROKER_ADDRESSES_ROOT}`;
brokerManualAddressUrl = `${baseUrl}${ROOT}/${referenceNumber}${BROKER_MANUAL_ADDRESS_ROOT}`;

cy.assertUrl(url);
});
Expand Down Expand Up @@ -156,29 +158,35 @@ context(
cy.navigateToUrl(url);
});

it(`should redirect to ${BROKER_CONFIRM_ADDRESS_ROOT} page`, () => {
cy.completeAndSubmitBrokerDetailsForm({});

cy.assertUrl(brokerConfirmAddressUrl);
});
describe(`when submitting ${IS_BASED_IN_UK} as "yes"`, () => {
it(`should redirect to ${BROKER_ADDRESSES_ROOT} page`, () => {
cy.completeAndSubmitBrokerDetailsForm({ isBasedInUk: true });

describe(`when submitting ${IS_BASED_IN_UK} as "no" and going back to the page`, () => {
it('should have the submitted values', () => {
cy.completeAndSubmitBrokerDetailsForm({ isBasedInUk: false });
cy.assertUrl(brokerAddressesUrl);
});

cy.navigateToUrl(url);
describe('when going back to the page', () => {
it('should have the submitted values', () => {
cy.navigateToUrl(url);

cy.assertBrokerDetailsFieldValues({ isBasedInUk: false });
cy.assertBrokerDetailsFieldValues({ isBasedInUk: true });
});
});
});

describe(`when submitting ${IS_BASED_IN_UK} as "yes" and going back to the page`, () => {
it('should have the submitted values', () => {
cy.completeAndSubmitBrokerDetailsForm({ isBasedInUk: true });
describe(`when submitting ${IS_BASED_IN_UK} as "no"`, () => {
it(`should redirect to ${BROKER_MANUAL_ADDRESS_ROOT} page`, () => {
cy.completeAndSubmitBrokerDetailsForm({ isBasedInUk: false });

cy.navigateToUrl(url);
cy.assertUrl(brokerManualAddressUrl);
});

cy.assertBrokerDetailsFieldValues({ isBasedInUk: true });
describe('when going back to the page', () => {
it('should have the submitted values', () => {
cy.navigateToUrl(url);

cy.assertBrokerDetailsFieldValues({ isBasedInUk: false });
});
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ const { NAME, EMAIL, IS_BASED_IN_UK, POSTCODE, BUILDING_NUMBER_OR_NAME } = POLIC

const {
INSURANCE_ROOT,
POLICY: { BROKER_DETAILS_SAVE_AND_BACK, BROKER_CONFIRM_ADDRESS_ROOT, BROKER_DETAILS_CHANGE, BROKER_DETAILS_CHECK_AND_CHANGE, CHECK_YOUR_ANSWERS },
POLICY: {
BROKER_DETAILS_SAVE_AND_BACK,
BROKER_ADDRESSES_ROOT,
BROKER_MANUAL_ADDRESS_ROOT,
BROKER_DETAILS_CHANGE,
BROKER_DETAILS_CHECK_AND_CHANGE,
CHECK_YOUR_ANSWERS,
},
CHECK_YOUR_ANSWERS: { TYPE_OF_POLICY: CHECK_AND_CHANGE_ROUTE },
PROBLEM_WITH_SERVICE,
} = INSURANCE_ROUTES;
Expand Down Expand Up @@ -135,12 +142,22 @@ describe('controllers/insurance/policy/broker-details', () => {
});

describe('post', () => {
const validBody = {
const genericValidBody = {
[NAME]: broker[NAME],
[EMAIL]: broker[EMAIL],
[IS_BASED_IN_UK]: 'true',
[POSTCODE]: broker[POSTCODE],
[BUILDING_NUMBER_OR_NAME]: broker[BUILDING_NUMBER_OR_NAME],
};

const validBody = {
basedInUk: {
...genericValidBody,
[IS_BASED_IN_UK]: 'true',
[POSTCODE]: broker[POSTCODE],
[BUILDING_NUMBER_OR_NAME]: broker[BUILDING_NUMBER_OR_NAME],
},
notBasedInUk: {
...genericValidBody,
[IS_BASED_IN_UK]: 'false',
},
};

mapAndSave.broker = jest.fn(() => Promise.resolve(true));
Expand Down Expand Up @@ -173,19 +190,35 @@ describe('controllers/insurance/policy/broker-details', () => {
});

describe('when there are no validation errors', () => {
beforeEach(async () => {
req.body = validBody;
describe(`when ${IS_BASED_IN_UK} is true`, () => {
it(`should redirect to ${BROKER_ADDRESSES_ROOT}`, async () => {
req.body = validBody.basedInUk;

await post(req, res);
await post(req, res);

const expected = `${INSURANCE_ROOT}/${referenceNumber}${BROKER_ADDRESSES_ROOT}`;

expect(res.redirect).toHaveBeenCalledWith(expected);
});
});

it(`should redirect to ${BROKER_CONFIRM_ADDRESS_ROOT}`, () => {
const expected = `${INSURANCE_ROOT}/${referenceNumber}${BROKER_CONFIRM_ADDRESS_ROOT}`;
describe(`when ${IS_BASED_IN_UK} is false`, () => {
it(`should redirect to ${BROKER_MANUAL_ADDRESS_ROOT}`, async () => {
req.body = validBody.notBasedInUk;

expect(res.redirect).toHaveBeenCalledWith(expected);
await post(req, res);

const expected = `${INSURANCE_ROOT}/${referenceNumber}${BROKER_MANUAL_ADDRESS_ROOT}`;

expect(res.redirect).toHaveBeenCalledWith(expected);
});
});

it('should call mapAndSave.broker once with data from constructPayload function', () => {
it('should call mapAndSave.broker once with data from constructPayload function', async () => {
req.body = validBody.notBasedInUk;

await post(req, res);

const payload = constructPayload(req.body, FIELD_IDS);

expect(mapAndSave.broker).toHaveBeenCalledTimes(1);
Expand All @@ -195,6 +228,8 @@ describe('controllers/insurance/policy/broker-details', () => {

describe("when the url's last substring is `change`", () => {
it(`should redirect to ${CHECK_YOUR_ANSWERS}`, async () => {
req.body = validBody.notBasedInUk;

req.originalUrl = BROKER_DETAILS_CHANGE;

await post(req, res);
Expand All @@ -206,6 +241,8 @@ describe('controllers/insurance/policy/broker-details', () => {

describe("when the url's last substring is `check-and-change`", () => {
it(`should redirect to ${CHECK_AND_CHANGE_ROUTE}`, async () => {
req.body = validBody.notBasedInUk;

req.originalUrl = BROKER_DETAILS_CHECK_AND_CHANGE;

await post(req, res);
Expand All @@ -232,7 +269,7 @@ describe('controllers/insurance/policy/broker-details', () => {
describe('api error handling', () => {
describe('mapAndSave.broker call', () => {
beforeEach(() => {
req.body = validBody;
req.body = validBody.notBasedInUk;
});

describe('when no application is returned', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const { NAME, EMAIL, IS_BASED_IN_UK, POSTCODE, BUILDING_NUMBER_OR_NAME } = POLIC

const {
INSURANCE_ROOT,
POLICY: { BROKER_DETAILS_SAVE_AND_BACK, BROKER_CONFIRM_ADDRESS_ROOT, CHECK_YOUR_ANSWERS },
POLICY: { BROKER_DETAILS_SAVE_AND_BACK, BROKER_ADDRESSES_ROOT, BROKER_MANUAL_ADDRESS_ROOT, CHECK_YOUR_ANSWERS },
CHECK_YOUR_ANSWERS: { TYPE_OF_POLICY: CHECK_AND_CHANGE_ROUTE },
PROBLEM_WITH_SERVICE,
} = INSURANCE_ROUTES;
Expand Down Expand Up @@ -154,7 +154,13 @@ export const post = async (req: Request, res: Response) => {
return res.redirect(`${INSURANCE_ROOT}/${referenceNumber}${CHECK_AND_CHANGE_ROUTE}`);
}

return res.redirect(`${INSURANCE_ROOT}/${referenceNumber}${BROKER_CONFIRM_ADDRESS_ROOT}`);
const isBasedInUk = payload[IS_BASED_IN_UK] === 'true';

if (isBasedInUk) {
return res.redirect(`${INSURANCE_ROOT}/${referenceNumber}${BROKER_ADDRESSES_ROOT}`);
}

return res.redirect(`${INSURANCE_ROOT}/${referenceNumber}${BROKER_MANUAL_ADDRESS_ROOT}`);
} catch (error) {
console.error('Error updating application - policy - broker details %o', error);

Expand Down
8 changes: 8 additions & 0 deletions src/ui/templates/insurance/policy/broker-manual-address.njk
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{% extends 'index.njk' %}
{% from "govuk/components/back-link/macro.njk" import govukBackLink %}
{% from 'govuk/components/error-summary/macro.njk' import govukErrorSummary %}
{% from "govuk/components/character-count/macro.njk" import govukCharacterCount %}
{% import '../../components/form-buttons.njk' as formButtons %}

Expand All @@ -17,6 +18,13 @@
}
}) }}

{% if validationErrors.summary %}
{{ govukErrorSummary({
titleText: CONTENT_STRINGS.ERROR_MESSAGES.THERE_IS_A_PROBLEM,
errorList: validationErrors.summary
}) }}
{% endif %}

<form method="POST" data-cy="form" enctype="application/x-www-form-urlencoded" novalidate>

<input type="hidden" name="_csrf" value="{{ csrfToken }}">
Expand Down

0 comments on commit 754632e

Please sign in to comment.