Skip to content

Commit

Permalink
Merge pull request #142 from DEFRA/BAU-trim-address
Browse files Browse the repository at this point in the history
[746] trim address values
  • Loading branch information
eoin-corr-git authored Jan 23, 2025
2 parents 0b4122b + d385ee9 commit 8e97fe2
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
9 changes: 8 additions & 1 deletion src/server/common/model/answer/address/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,14 @@ export class AddressAnswer extends AnswerModel {
* @returns {AddressData | undefined}
*/
get value() {
return this._data
const trimmedValues = Object.fromEntries(
Object.entries(this._data ?? {})
.map(([key, value]) => [key, value.trim()])
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.filter(([_, value]) => value !== '')
)

return Object.keys(trimmedValues).length === 0 ? undefined : trimmedValues
}

get html() {
Expand Down
19 changes: 17 additions & 2 deletions src/server/common/model/answer/address/address.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { AddressAnswer } from './address.js'
const validAddress = {
addressLine1: 'Starfleet Headquarters',
addressLine2: '24-593 Federation Drive',
addressTown: 'San Francisco',
addressTown: 'San Francisco Bay',
addressCounty: 'San Francisco',
addressPostcode: 'RG24 8RR'
}
Expand All @@ -16,6 +16,21 @@ describe('Address.new', () => {
expect(address._data).toEqual(validAddress)
})

it('should trim any whitespace from the values', () => {
const payload = {
addressLine1: ' Starfleet Headquarters ',
addressTown: ' San Francisco ',
addressPostcode: ' RG24 8RR '
}
const address = new AddressAnswer(payload)

expect(address.value).toEqual({
addressLine1: 'Starfleet Headquarters',
addressTown: 'San Francisco',
addressPostcode: 'RG24 8RR'
})
})

it('should graceully handle optional values', () => {
const validAddressWithOptionalsMissing = {
addressLine1: validAddress.addressLine1,
Expand Down Expand Up @@ -165,7 +180,7 @@ describe('Address.html', () => {
const expectedHtml = [
'Starfleet Headquarters',
'24-593 Federation Drive',
'San Francisco',
'San Francisco Bay',
'San Francisco',
'RG24 8RR'
].join('<br />')
Expand Down
2 changes: 1 addition & 1 deletion user-journey-tests/helpers/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const validateElementVisibleAndText = async (element, text) => {

export const validateHrefOfElement = async (element, href) => {
const hrefValue = await element.getAttribute('href')
expect(hrefValue).toBe(href)
expect(hrefValue).toContain(href)
}

export const typeIntoElement = async (element, text) => {
Expand Down
28 changes: 28 additions & 0 deletions user-journey-tests/specs/origin/newAddress.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ const county = 'West new york'
const postcodeValid = 'SW1A 2AA'
const postcodeInvalid = 'test'

const lineOneWhitespace = ' 37 Made up lane '
const lineTwoWhitespace = ' Not real avenue '
const townOrCityWhitespace = ' Gotham '
const countyWhitespace = ' West new york '
const postcodeValidWhitespace = ' SW1A 2AA '

describe('New address page test', () => {
beforeEach('Reset browser state and navigate to page', async () => {
await browser.reloadSession()
Expand Down Expand Up @@ -107,4 +113,26 @@ describe('New address page test', () => {
postcode: postcodeValid
})
})

it.only('Should verify successful submission when all fields entered', async () => {
await newAddressPage.fillFormFieldsAndSubmit({
lineOne: lineOneWhitespace,
lineTwo: lineTwoWhitespace,
townOrCity: townOrCityWhitespace,
county: countyWhitespace,
postcode: postcodeValidWhitespace
})

await newAddressPage.verifyNoErrorsVisible()
await newAddressPage.selectBackLink()
await browser.refresh()

await newAddressPage.verifyFieldValues({
lineOne,
lineTwo,
townOrCity,
county,
postcode: postcodeValid
})
})
})

0 comments on commit 8e97fe2

Please sign in to comment.