From 09dff0963fcf6ca9ec37c2634ac9ee70efd1291e Mon Sep 17 00:00:00 2001 From: Oliver Roberts Date: Mon, 10 Feb 2025 11:18:45 +0000 Subject: [PATCH] Reapply site address question changes with some modifications (#7511) * Reapply site address question changes with some modifications Reapplies PR #7486 (commit 382debc6b39c30c5088474c7e2e4980a496f4229). Modifications include keeping the `actual_uk_regions` question and unnesting it. * Remove superfluous return * Test transformation of actual_uk_regions field --- .../MyInvestmentProjects/constants.js | 4 +- .../Details/EditProjectRequirements.jsx | 113 +++-- .../Projects/Details/transformers.js | 63 +-- .../Projects/Details/validators.js | 15 +- .../Investments/Projects/transformers.js | 1 + test/api-schema.json | 10 +- .../Projects/ProjectIncompleteFields.cy.jsx | 13 +- .../cypress/fakers/investment-projects.js | 3 +- .../investment-has-existing-requirements.json | 2 +- .../investment-no-existing-requirements.json | 2 +- .../cypress/specs/investments/constants.js | 13 +- .../investments/project-edit-details-spec.js | 6 + .../project-edit-requirements-spec.js | 385 ++++++++++++++---- .../investments/project-edit-stages-specs.js | 21 +- 14 files changed, 457 insertions(+), 194 deletions(-) diff --git a/src/client/components/MyInvestmentProjects/constants.js b/src/client/components/MyInvestmentProjects/constants.js index 3f03f94c6b7..0595e308dd8 100644 --- a/src/client/components/MyInvestmentProjects/constants.js +++ b/src/client/components/MyInvestmentProjects/constants.js @@ -48,7 +48,7 @@ export const GET_REQUIRED_FIELDS_AFTER_STAGE = { ['non_fdi_r_and_d_budget']: STAGE.VERIFY_WIN_ID, ['new_tech_to_uk']: STAGE.VERIFY_WIN_ID, ['export_revenue']: STAGE.VERIFY_WIN_ID, - ['site_decided']: STAGE.VERIFY_WIN_ID, + ['site_address_is_company_address']: STAGE.VERIFY_WIN_ID, ['actual_uk_regions']: STAGE.VERIFY_WIN_ID, ['delivery_partners']: STAGE.VERIFY_WIN_ID, ['actual_land_date']: STAGE.VERIFY_WIN_ID, @@ -136,6 +136,8 @@ export const INCOMPLETE_FIELDS = { 'Does the project bring ‘New To World’ Technology, IP or Business Model to the UK site?', export_revenue: 'Will the UK company export a significant proportion of their products and services produced in the UK as a result of the FDI project?', + site_address_is_company_address: + "Is the site address the same as the UK recipient company's address?", address_1: 'Street', address_town: 'Town', address_postcode: 'Postcode', diff --git a/src/client/modules/Investments/Projects/Details/EditProjectRequirements.jsx b/src/client/modules/Investments/Projects/Details/EditProjectRequirements.jsx index 268f6ed93a3..f48d82dbf8b 100644 --- a/src/client/modules/Investments/Projects/Details/EditProjectRequirements.jsx +++ b/src/client/modules/Investments/Projects/Details/EditProjectRequirements.jsx @@ -2,6 +2,7 @@ import React from 'react' import { H2 } from 'govuk-react' import { LEVEL_SIZE } from '@govuk-react/constants' import { useParams } from 'react-router-dom' +import InsetText from '@govuk-react/inset-text' import { FieldAddress, @@ -28,6 +29,7 @@ import { import { OPTIONS_YES_NO, OPTION_YES, + OPTION_NO, UNITED_KINGDOM_ID, } from '../../../../../common/constants' @@ -38,7 +40,7 @@ import { isFieldOptionalForStageLabel, validateFieldForStage, } from '../validators' -import { siteDecidedValidator } from './validators' +import { siteAddressIsCompanyAddressValidator } from './validators' const ukObject = { name: 'United Kingdom', @@ -78,6 +80,7 @@ const EditProjectRequirements = () => { transformProjectRequirementsForApi({ projectId, values, + ukCompany: project.ukCompany, }) } > @@ -177,60 +180,88 @@ const EditProjectRequirements = () => { }} /> ({ ...option, ...(option.value === OPTION_YES && { children: ( <> - - { - return validateFieldForStage( - field, - formFields, - project, - 'Select a UK region' - ) - }} - /> + + The address will appear on this form once you have + selected the recipient company + ), }), + ...(option.value === OPTION_YES && + project.ukCompany && { + children: ( + <> + +

{project.ukCompany.address1}

+

{project.ukCompany.address2}

+

{project.ukCompany.addressTown}

+

{project.ukCompany.addressPostcode}

+
+ + ), + }), + ...(option.value === OPTION_NO && { + children: ( + + ), + }), }))} validate={(values, field, formFields) => { - return siteDecidedValidator(field, formFields, project) + return siteAddressIsCompanyAddressValidator( + field, + formFields, + project + ) }} /> + + validateFieldForStage( + field, + formFields, + project, + 'Select a UK region' + ) + } + /> const setConditionalArrayValue = (radioValue, array) => transformRadioOptionToBool(radioValue) ? array.map((x) => x.value) : [] -const setSiteDecidedSubValues = ( - site_decided, +const setSiteAddressValues = ( + siteAddressIsCompanyAddress, + ukCompany, address1, address2, city, postcode ) => { - return transformRadioOptionToBool(site_decided) - ? { - address_1: address1, - address_2: address2, - address_town: city, - address_postcode: postcode, - } - : { - address_1: '', - address_2: '', - address_town: '', - address_postcode: '', - } + const siteAddressIsCompanyAddressBool = transformRadioOptionToBool( + siteAddressIsCompanyAddress + ) + if (siteAddressIsCompanyAddressBool === true && ukCompany) { + return { + address_1: ukCompany.address1, + address_2: ukCompany.address2, + address_town: ukCompany.addressTown, + address_postcode: ukCompany.addressPostcode, + } + } else { + // return entered/existing values if not empty to ensure data is NOT overwritten + return { + address_1: address1 ? address1 : null, + address_2: address2 ? address2 : null, + address_town: city ? city : null, + address_postcode: postcode ? postcode : null, + } + } } const checkLandDate = (estimatedLandDate) => { @@ -73,7 +80,11 @@ export const transformBoolToRadioOptionWithNullCheck = (boolean) => export const transformBoolToInvertedRadioOptionWithNullCheck = (boolean) => boolean === null ? null : transformBoolToInvertedRadioOption(boolean) -export const transformProjectRequirementsForApi = ({ projectId, values }) => { +export const transformProjectRequirementsForApi = ({ + projectId, + values, + ukCompany, +}) => { const { actual_uk_regions, address1, @@ -84,13 +95,14 @@ export const transformProjectRequirementsForApi = ({ projectId, values }) => { competitor_countries, delivery_partners, postcode, - site_decided, + site_address_is_company_address, strategic_drivers, uk_region_locations, } = values - const siteDecidedObject = setSiteDecidedSubValues( - site_decided, + const siteAddressObject = setSiteAddressValues( + site_address_is_company_address, + ukCompany, address1, address2, city, @@ -99,10 +111,9 @@ export const transformProjectRequirementsForApi = ({ projectId, values }) => { const requirementsValues = { id: projectId, - actual_uk_regions: setConditionalArrayValue( - site_decided, - actual_uk_regions - ), + actual_uk_regions: actual_uk_regions + ? actual_uk_regions.map((x) => x.value) + : [], client_considering_other_countries: transformRadioOptionToBoolWithNullCheck( client_considering_other_countries ), @@ -114,7 +125,9 @@ export const transformProjectRequirementsForApi = ({ projectId, values }) => { delivery_partners: delivery_partners ? delivery_partners.map((x) => x.value) : [], - site_decided: transformRadioOptionToBoolWithNullCheck(site_decided), + site_address_is_company_address: transformRadioOptionToBoolWithNullCheck( + site_address_is_company_address + ), strategic_drivers: strategic_drivers ? strategic_drivers.map((x) => x.value) : [], @@ -123,7 +136,7 @@ export const transformProjectRequirementsForApi = ({ projectId, values }) => { : [], } - return { ...siteDecidedObject, ...requirementsValues } + return { ...siteAddressObject, ...requirementsValues } } export const transformProjectSummaryForApi = ({ diff --git a/src/client/modules/Investments/Projects/Details/validators.js b/src/client/modules/Investments/Projects/Details/validators.js index f1265bf7291..7e04a9c289e 100644 --- a/src/client/modules/Investments/Projects/Details/validators.js +++ b/src/client/modules/Investments/Projects/Details/validators.js @@ -1,8 +1,6 @@ import { STAGE } from '../../../../components/MyInvestmentProjects/constants' import { isFieldRequiredForStage } from '../validators' -const { OPTION_YES } = require('../../../../../common/constants') - export const totalInvestmentValidator = (value, foreignEquityInvestment) => { if (parseInt(value) < parseInt(foreignEquityInvestment)) { return 'Total investment must be >= to capital expenditure' @@ -18,13 +16,14 @@ export const capitalExpenditureValidator = (value) => { return null } -export const siteDecidedValidator = (field, formFields, project) => { - if (project?.stage?.id == STAGE.ACTIVE_ID && !formFields.values[field.name]) { - return 'Select a value for UK location decision' - } +export const siteAddressIsCompanyAddressValidator = ( + field, + formFields, + project +) => { return isFieldRequiredForStage(field.name, project) && - formFields.values[field.name] != OPTION_YES - ? 'A UK region is required' + !formFields.values[field.name] + ? "Select if the site address the same as the UK recipient company's address?" : null } diff --git a/src/client/modules/Investments/Projects/transformers.js b/src/client/modules/Investments/Projects/transformers.js index aa31d645cfe..55e9861b011 100644 --- a/src/client/modules/Investments/Projects/transformers.js +++ b/src/client/modules/Investments/Projects/transformers.js @@ -101,6 +101,7 @@ export const mapFieldToUrl = (field, projectId) => { 'Delivery partners', 'Strategic drivers behind this investment', 'Possible UK locations for this investment', + "Is the site address the same as the UK recipient company's address?", 'Street', 'Town', 'Postcode', diff --git a/test/api-schema.json b/test/api-schema.json index 002f816c70d..4d93523bc95 100644 --- a/test/api-schema.json +++ b/test/api-schema.json @@ -7506,7 +7506,7 @@ "type": "string", "nullable": true }, - "site_decided": { + "site_address_is_company_address": { "type": "boolean", "nullable": true }, @@ -9526,13 +9526,7 @@ "nullable": true }, "hiring": { - "enum": [ - "1-5", - "6-50", - "51-100", - "101+", - "NO_PLANS_TO_HIRE_YET" - ], + "enum": ["1-5", "6-50", "51-100", "101+", "NO_PLANS_TO_HIRE_YET"], "type": "string" }, "spend": { diff --git a/test/component/cypress/specs/Investments/Projects/ProjectIncompleteFields.cy.jsx b/test/component/cypress/specs/Investments/Projects/ProjectIncompleteFields.cy.jsx index e39f3a99e8c..d7698d9efaf 100644 --- a/test/component/cypress/specs/Investments/Projects/ProjectIncompleteFields.cy.jsx +++ b/test/component/cypress/specs/Investments/Projects/ProjectIncompleteFields.cy.jsx @@ -49,10 +49,7 @@ const activeIncompleteFields = [ 'non_fdi_r_and_d_budget', 'new_tech_to_uk', 'export_revenue', - 'address_1', - 'address_town', - 'address_postcode', - 'actual_uk_regions', + 'site_address_is_company_address', 'delivery_partners', 'actual_land_date', 'foreign_equity_investment', @@ -226,10 +223,10 @@ describe('ProjectIncompleteFields', () => { 'Will the UK company export a significant proportion of their products and services produced in the UK as a result of the FDI project?', valueLink ) - assertLink('Street', requirementsLink) - assertLink('Town', requirementsLink) - assertLink('Postcode', requirementsLink) - assertLink('UK regions landed', requirementsLink) + assertLink( + "Is the site address the same as the UK recipient company's address?", + requirementsLink + ) assertLink('Delivery partners', requirementsLink) assertLink('Actual land date', detailsLink) assertLink('Foreign equity investment', valueLink) diff --git a/test/functional/cypress/fakers/investment-projects.js b/test/functional/cypress/fakers/investment-projects.js index b8830ae7e7e..5d093b0c34b 100644 --- a/test/functional/cypress/fakers/investment-projects.js +++ b/test/functional/cypress/fakers/investment-projects.js @@ -83,6 +83,7 @@ const investmentProjectFaker = (overrides = {}) => ({ sector_classification_gva_multiplier: 'labour', id: faker.string.uuid(), }, + specific_programmes: [], ...overrides, }) @@ -120,7 +121,7 @@ const investmentProjectEmptyFaker = (overrides = {}) => non_fdi_r_and_d_budget: null, new_tech_to_uk: null, export_revenue: null, - site_decided: null, + site_address_is_company_address: null, address_1: null, address_town: null, address_postcode: null, diff --git a/test/functional/cypress/fixtures/investment/investment-has-existing-requirements.json b/test/functional/cypress/fixtures/investment/investment-has-existing-requirements.json index 25bfe009f11..1b91f6e4657 100644 --- a/test/functional/cypress/fixtures/investment/investment-has-existing-requirements.json +++ b/test/functional/cypress/fixtures/investment/investment-has-existing-requirements.json @@ -122,7 +122,7 @@ "client_cannot_provide_total_investment": false, "client_cannot_provide_foreign_investment": false, "client_requirements": "Anywhere", - "site_decided": true, + "site_address_is_company_address": false, "address_1": "10 Eastings Road", "address_2": null, "address_town": "London", diff --git a/test/functional/cypress/fixtures/investment/investment-no-existing-requirements.json b/test/functional/cypress/fixtures/investment/investment-no-existing-requirements.json index 294d53af1de..808a432afda 100644 --- a/test/functional/cypress/fixtures/investment/investment-no-existing-requirements.json +++ b/test/functional/cypress/fixtures/investment/investment-no-existing-requirements.json @@ -124,7 +124,7 @@ "client_cannot_provide_total_investment": null, "client_cannot_provide_foreign_investment": null, "client_requirements": null, - "site_decided": null, + "site_address_is_company_address": null, "address_1": null, "address_2": null, "address_town": null, diff --git a/test/functional/cypress/specs/investments/constants.js b/test/functional/cypress/specs/investments/constants.js index a9173f9892c..d2a1b5ab1e1 100644 --- a/test/functional/cypress/specs/investments/constants.js +++ b/test/functional/cypress/specs/investments/constants.js @@ -89,10 +89,18 @@ export const FIELDS = { name: 'uk_region_locations', message: 'Select a possible UK location', }, - + SITE_ADDRESS_IS_COMPANY_ADDRESS: { + name: 'site_address_is_company_address', + message: + "Select if the site address the same as the UK recipient company's address", + }, ADDRESS1: { name: 'address1', message: 'Enter an address' }, CITY: { name: 'city', message: 'Enter a town or city' }, POSTCODE: { name: 'postcode', message: 'Enter a postcode' }, + ACTUAL_UK_REGIONS: { + name: 'actual_uk_regions', + message: 'Select a UK region', + }, // Edit value CLIENT_CANNOT_PROVIDE_FOREIGN_INVESTMENT: { @@ -169,7 +177,8 @@ export const EDIT_REQUIREMENTS_BASE_FIELDS = [ export const EDIT_REQUIREMENTS_ADDITIONAL_FIELDS = [ ...EDIT_REQUIREMENTS_BASE_FIELDS, FIELDS.DELIVERY_PARTNERS, - { name: 'site_decided', message: 'A UK region is required' }, + FIELDS.SITE_ADDRESS_IS_COMPANY_ADDRESS, + FIELDS.ACTUAL_UK_REGIONS, ] export const EDIT_VALUE_BASE_FIELDS = [ diff --git a/test/functional/cypress/specs/investments/project-edit-details-spec.js b/test/functional/cypress/specs/investments/project-edit-details-spec.js index 8ed817c74a9..8c91b0c5d86 100644 --- a/test/functional/cypress/specs/investments/project-edit-details-spec.js +++ b/test/functional/cypress/specs/investments/project-edit-details-spec.js @@ -41,6 +41,12 @@ const setupProjectFaker = (overrides) => id: '952232d2-1d25-4c3a-bcac-2f3a30a94da9', }, ], + business_activities: [ + { + name: 'Retail', + id: 'a2dbd807-ae52-421c-8d1d-88adfc7a506b', + }, + ], referral_source_adviser: { name: 'Puck Head', first_name: 'Puck', diff --git a/test/functional/cypress/specs/investments/project-edit-requirements-spec.js b/test/functional/cypress/specs/investments/project-edit-requirements-spec.js index 54f8b8d9325..24a00b9e3f5 100644 --- a/test/functional/cypress/specs/investments/project-edit-requirements-spec.js +++ b/test/functional/cypress/specs/investments/project-edit-requirements-spec.js @@ -1,29 +1,148 @@ +import { INVESTMENT_PROJECT_STAGES } from '../../fakers/constants' + const { assertLocalHeader, assertBreadcrumbs, + assertErrorSummary, assertFieldTextarea, assertFieldRadios, + assertFieldRadiosNotSelected, + assertFieldRadioSelected, assertFieldTypeahead, assertFieldInput, assertFieldUneditable, } = require('../../support/assertions') const { investments } = require('../../../../../src/lib/urls') - +const { investmentProjectFaker } = require('../../fakers/investment-projects') const projectNoExistingRequirements = require('../../fixtures/investment/investment-no-existing-requirements.json') const projectHasExistingRequirements = require('../../fixtures/investment/investment-has-existing-requirements.json') const ukRegions = require('../../../../sandbox/fixtures/metadata/uk-region.json') +const ukCompany = { + name: 'Mars Components Ltd', + address_1: '12 Alpha Street', + address_2: '', + address_town: 'Volcanus', + address_postcode: 'NE28 5AQ', + id: '731bdcc1-f685-4c8e-bd66-b356b2c16995', +} + const navigateToForm = ({ project }, dataTest = 'edit') => { + cy.intercept('GET', `/api-proxy/v3/investment/${project.id}`, { + statusCode: 200, + body: project, + }).as('getProjectDetails') cy.visit(investments.projects.details(project.id)) cy.get(`[data-test="${dataTest}-requirements-button"]`).click() } const checkIfClientConsidering = (valueToCheck) => (valueToCheck ? 3 : 2) -const checkIfSiteDecided = (valueToCheck) => (valueToCheck ? 7 : 2) +const checkIfSiteAddressIsCompanyAddress = (valueToCheck) => { + if (valueToCheck === null) { + return 2 + } else if (valueToCheck === true) { + return 3 + } else if (valueToCheck === false) { + return 6 + } +} const convertBoolToYesNo = (valueToCheck) => (valueToCheck ? 'Yes' : 'No') const convertBoolToYesNoWithNullCheck = (valueToCheck) => valueToCheck === null ? null : convertBoolToYesNo(valueToCheck) +const assertSiteAddressIsCompanyAddressField = (project) => { + it('should display the site address is company address field', () => { + cy.get('[data-test="field-site_address_is_company_address"]').then( + (element) => { + assertFieldRadios({ + element, + label: + "Is the site address the same as the UK recipient company's address?", + optionsCount: checkIfSiteAddressIsCompanyAddress( + project.site_address_is_company_address + ), + value: convertBoolToYesNoWithNullCheck( + project.site_address_is_company_address + ), + }) + } + ) + }) + if (project.site_address_is_company_address === null) { + it('should display the site address is company address field unselected', () => { + assertFieldRadiosNotSelected({ + inputName: 'site_address_is_company_address', + }) + }) + } else if ( + project.site_address_is_company_address === true && + project.uk_company === null + ) { + it('should display the inset text below the yes option', () => { + assertFieldRadioSelected({ + inputName: 'site_address_is_company_address', + selectedIndex: 0, + }) + cy.get('[data-test="site_address_is_company_address"]').contains( + 'The address will appear on this form once you have selected the recipient company' + ) + }) + } else if ( + project.site_address_is_company_address === true && + project.uk_company !== null + ) { + it('should display the inset text below the yes option', () => { + assertFieldRadioSelected({ + inputName: 'site_address_is_company_address', + selectedIndex: 0, + }) + cy.get('[data-test="site_address_is_company_address"]') + .contains(`${project.uk_company.address_1}`) + .contains(`${project.uk_company.address_2}`) + .contains(`${project.uk_company.address_town}`) + .contains(`${project.uk_company.address_postcode}`) + }) + } else if (project.site_address_is_company_address === false) { + it('should display the address field', () => { + cy.get('[data-test="field-country"]').then((element) => { + assertFieldUneditable({ + element, + label: 'Country', + value: 'United Kingdom', + }) + }) + cy.get('[data-test="field-address1"]').then((element) => { + assertFieldInput({ + element, + label: 'Address line 1', + value: project.address_1, + }) + }) + cy.get('[data-test="field-address2"]').then((element) => { + assertFieldInput({ + element, + label: 'Address line 2 (optional)', + value: project.address_2, + }) + }) + cy.get('[data-test="field-city"]').then((element) => { + assertFieldInput({ + element, + label: 'Town or city', + value: project.address_town, + }) + }) + cy.get('[data-test="field-postcode"]').then((element) => { + assertFieldInput({ + element, + label: 'Postcode', + value: project.address_postcode, + }) + }) + }) + } +} + const testProjectRequirementsForm = ({ project }, dataTest) => { beforeEach(() => { navigateToForm({ project }, dataTest) @@ -117,87 +236,20 @@ const testProjectRequirementsForm = ({ project }, dataTest) => { ) }) - it('should display the site decided field', () => { - cy.get('[data-test="field-site_decided"]').then((element) => { - assertFieldRadios({ - element, - label: - 'Has the UK location (site address) for this investment been decided yet?', - optionsCount: checkIfSiteDecided(project.site_decided), - value: convertBoolToYesNoWithNullCheck(project.site_decided), - }) - }) + it('should correctly display the site address is company address field', () => { + assertSiteAddressIsCompanyAddressField(project) }) - project.site_decided - ? it('should display the address field', () => { - cy.get('[data-test="field-country"]').then((element) => { - assertFieldUneditable({ - element, - label: 'Country', - value: 'United Kingdom', - }) - }) - cy.get('[data-test="field-address1"]').then((element) => { - assertFieldInput({ - element, - label: 'Address line 1', - value: project.address_1, - }) - }) - cy.get('[data-test="field-address2"]').then((element) => { - assertFieldInput({ - element, - label: 'Address line 2 (optional)', - value: project.address_2, - }) - }) - cy.get('[data-test="field-city"]').then((element) => { - assertFieldInput({ - element, - label: 'Town or city', - value: project.address_town, - }) - }) - cy.get('[data-test="field-postcode"]').then((element) => { - assertFieldInput({ - element, - label: 'Postcode', - value: project.address_postcode, - }) - }) - }) - : it('should not display the address field', () => { - cy.get('[data-test="field-address"]').should('not.exist') - }) - - if (project.site_decided) { - it('should render the landed regions field', () => { - cy.get('[data-test="field-actual_uk_regions"]').then((element) => { - assertFieldTypeahead({ - element, - label: 'UK regions landed', - placeholder: 'Select a UK region', - values: project.actual_uk_regions, - }) - }) - }) - - it('should only display active UK regions when the landed regions field is selected', () => { - const activeUkRegions = ukRegions.filter((region) => !region.disabled_on) - cy.get('[data-test="field-actual_uk_regions"]').as('typeaheadField') - cy.get('@typeaheadField').find('input').first().click() - cy.get('[data-test="typeahead-menu-option"]').should('be.visible') - cy.get('[data-test="typeahead-menu-option"]').should( - 'have.length', - activeUkRegions.length - ) - }) - } else { - it('should not display the landed regions field', () => { - cy.get('[data-test="field-actual_uk_regions"]').should('not.exist') - }) - } + it('should render the active UK regions field', () => { + const activeUkRegions = ukRegions.filter((region) => !region.disabled_on) + cy.get('[data-test="field-actual_uk_regions"]').as('typeaheadField') + cy.get('@typeaheadField').find('input').first().click() + cy.get('[data-test="typeahead-menu-option"]').should('be.visible') + cy.get('[data-test="typeahead-menu-option"]').should( + 'have.length', + activeUkRegions.length + ) + }) it('should render the delivery partners field', () => { cy.get('[data-test="field-delivery_partners"]').then((element) => { @@ -249,7 +301,7 @@ describe('Edit the requirements of a project', () => { .selectTypeaheadOption('East Midlands') .selectTypeaheadOption('East of England') - cy.get('[data-test="site-decided-yes"]').click() + cy.get('[data-test="site-address-is-company-address-no"]').click() cy.get('[data-test="field-address1"]').type('Street address') cy.get('[data-test="field-address2"]').type('Street address 2') cy.get('[data-test="field-city"]').type('Town') @@ -275,3 +327,172 @@ describe('Edit the requirements of a project', () => { }) }) }) + +describe('Site address fields', () => { + const siteAddressProject = (overrides) => { + return investmentProjectFaker({ + stage: INVESTMENT_PROJECT_STAGES.prospect, // ensures all fields are optional at stage + strategic_drivers: [ + { + name: 'Access to market', + id: '382aa6d1-a362-4166-a09d-f579d9f3be75', + }, + ], // populating this field forces the `edit requirements` button to appear + uk_company: null, + site_address_is_company_address: null, + address_1: null, + address_2: null, + address_town: null, + address_postcode: null, + ...overrides, + }) + } + + context('When viewing the site address fields', () => { + it('should show unselected radios when site address is company address is null', () => { + const project = siteAddressProject() + navigateToForm({ project: project }) + assertSiteAddressIsCompanyAddressField(project) + }) + + it('should show the inset text when site address is company is true and uk company is not set', () => { + const project = siteAddressProject({ + site_address_is_company_address: true, + }) + navigateToForm({ project: project }) + assertSiteAddressIsCompanyAddressField(project) + }) + + it('should show the company address fields when the site address is company address is true and uk company is set', () => { + const project = siteAddressProject({ + uk_company: ukCompany, + site_address_is_company_address: true, + }) + navigateToForm({ project: project }) + assertSiteAddressIsCompanyAddressField(project) + }) + + it('should show the address input fields when site address is company address is false', () => { + const project = siteAddressProject({ + site_address_is_company_address: false, + }) + navigateToForm({ project: project }) + assertSiteAddressIsCompanyAddressField(project) + }) + }) + context('When editing the site address fields and clicking submit', () => { + it('should submit site address is company address is null when user does not modify field', () => { + const project = siteAddressProject() + navigateToForm({ project: project }) + cy.intercept('PATCH', `/api-proxy/v3/investment/${project.id}`, { + statusCode: 200, + }).as('patchProjectRequirements') + cy.get('[data-test="submit-button"]').click() + cy.wait('@patchProjectRequirements') + .its('request.body') + .should('include', { + site_address_is_company_address: null, + }) + }) + it('should submit site address is company address is true and existing site address values when user selects yes and uk company is null', () => { + const project = siteAddressProject() + navigateToForm({ project: project }) + cy.get('[data-test="site-address-is-company-address-yes"]').click() + cy.intercept('PATCH', `/api-proxy/v3/investment/${project.id}`, { + statusCode: 200, + }).as('patchProjectRequirements') + cy.get('[data-test="submit-button"]').click() + cy.wait('@patchProjectRequirements') + .its('request.body') + .should('include', { + site_address_is_company_address: true, + address_1: project.address_1, + address_2: project.address_2, + address_town: project.address_town, + address_postcode: project.address_postcode, + }) + }) + it('should submit site address is company address is true and populate address fields when user selects yes and project has uk company', () => { + const project = siteAddressProject({ uk_company: ukCompany }) + navigateToForm({ project: project }) + cy.get('[data-test="site-address-is-company-address-yes"]').click() + cy.intercept('PATCH', `/api-proxy/v3/investment/${project.id}`, { + statusCode: 200, + }).as('patchProjectRequirements') + cy.get('[data-test="submit-button"]').click() + cy.wait('@patchProjectRequirements') + .its('request.body') + .should('include', { + site_address_is_company_address: true, + address_1: ukCompany.address_1, + address_2: ukCompany.address_2, + address_town: ukCompany.address_town, + address_postcode: ukCompany.address_postcode, + }) + }) + it('should raise validation errors when site address is company address is false and address fields have not been populated', () => { + const project = siteAddressProject() + navigateToForm({ project: project }) + cy.get('[data-test="site-address-is-company-address-no"]').click() + cy.get('[data-test="submit-button"]').click() + assertErrorSummary([ + 'Enter an address', + 'Enter a town or city', + 'Enter a postcode', + ]) + }) + it('should submit site address is company address is false and address fields when user selects no and enters address fields', () => { + const project = siteAddressProject() + const address1 = 'Address line 1' + const address2 = 'Address line 2' + const city = 'City' + const postcode = 'ABC 123' + navigateToForm({ project: project }) + cy.get('[data-test="site-address-is-company-address-no"]').click() + cy.get('[data-test="field-address1"]').type(address1) + cy.get('[data-test="field-address2"]').type(address2) + cy.get('[data-test="field-city"]').type(city) + cy.get('[data-test="field-postcode"]').type(postcode) + cy.intercept('PATCH', `/api-proxy/v3/investment/${project.id}`, { + statusCode: 200, + }).as('patchProjectRequirements') + cy.get('[data-test="submit-button"]').click() + cy.wait('@patchProjectRequirements') + .its('request.body') + .should('include', { + site_address_is_company_address: false, + address_1: address1, + address_2: address2, + address_town: city, + address_postcode: postcode, + }) + }) + }) + context('When editing and submitting the landed uk regions field', () => { + it('should submit an empty list if no regions are selected', () => { + const project = siteAddressProject({ actual_uk_regions: [] }) + navigateToForm({ project: project }) + cy.intercept('PATCH', `/api-proxy/v3/investment/${project.id}`, { + statusCode: 200, + }).as('patchProjectRequirements') + cy.get('[data-test="submit-button"]').click() + cy.wait('@patchProjectRequirements') + .its('request.body.actual_uk_regions') + .should('be.empty') + }) + it('should submit a list if one or more regions are selected', () => { + const project = siteAddressProject({ actual_uk_regions: [] }) + navigateToForm({ project: project }) + cy.get('[data-test="field-actual_uk_regions"]') + .selectTypeaheadOption('East Midlands') + .selectTypeaheadOption('East of England') + cy.intercept('PATCH', `/api-proxy/v3/investment/${project.id}`, { + statusCode: 200, + }).as('patchProjectRequirements') + cy.get('[data-test="submit-button"]').click() + cy.wait('@patchProjectRequirements') + .its('request.body.actual_uk_regions') + .should('have.lengthOf', 2) + }) + }) +}) diff --git a/test/functional/cypress/specs/investments/project-edit-stages-specs.js b/test/functional/cypress/specs/investments/project-edit-stages-specs.js index b4a3bedbe18..274337b0a36 100644 --- a/test/functional/cypress/specs/investments/project-edit-stages-specs.js +++ b/test/functional/cypress/specs/investments/project-edit-stages-specs.js @@ -31,7 +31,7 @@ function assertRequiredFieldsForStages( buttonName ) { stageRequiredFields.forEach((stageRequiredField) => { - const [stage, requiredFields, ukLocationRequired = false] = + const [stage, requiredFields, siteAddressIsCompanyAddress = false] = stageRequiredField context(`In the ${stage.name} stage `, () => { @@ -59,13 +59,12 @@ function assertRequiredFieldsForStages( ) }) - if (ukLocationRequired) { - it('submitting an empty form with site_decide set to Yes should show address validation errors', () => { - cy.get('[data-test="site-decided-yes"]').click() + if (siteAddressIsCompanyAddress) { + it('submitting an empty form with site_address_is_company_address set to No should show address validation errors', () => { + cy.get('[data-test="site-address-is-company-address-no"]').click() cy.get('[data-test="submit-button"]').click() assertValidationMessages([ - FIELDS.SITE_DECIDED, FIELDS.ADDRESS1, FIELDS.CITY, FIELDS.POSTCODE, @@ -102,17 +101,7 @@ describe('Field validation for each stage', () => { EDIT_REQUIREMENTS_BASE_FIELDS, false, ], - [ - INVESTMENT_PROJECT_STAGES.active, - [ - ...EDIT_REQUIREMENTS_BASE_FIELDS, - { - name: 'site_decided', - message: 'Select a value for UK location decision', - }, - ], - false, - ], + [INVESTMENT_PROJECT_STAGES.active, EDIT_REQUIREMENTS_BASE_FIELDS, false], [ INVESTMENT_PROJECT_STAGES.verifyWin, EDIT_REQUIREMENTS_ADDITIONAL_FIELDS,