From 417dd2a65db78cefef395e043e1d6b0733326f17 Mon Sep 17 00:00:00 2001 From: Alan Cruikshanks Date: Fri, 6 Oct 2023 13:50:44 +0100 Subject: [PATCH] Add capitalize() method to base presenter (#451) https://eaflood.atlassian.net/browse/WATER-4152 We are currently working on the SROC two-part tariff bill. The first step of the process in the UI is to review how the returns have been matched. Our superstar design team are working on an improved design for the returns review screen but to do that they need data. This led us to start [Implement mock return review](https://github.com/DEFRA/water-abstraction-system/pull/447). A requirement that came out of that was the ability to capitalize certain values, for example, loss is recorded in the DB as `high` but when presenting it we want to see `High`. As we start to build views and 'present' the data for them we're going to need this in more and more cases. So, this change adds a `capitalize()` method to our base presenter. --- app/presenters/base.presenter.js | 22 +++++++++++ test/presenters/base.presenter.test.js | 52 ++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/app/presenters/base.presenter.js b/app/presenters/base.presenter.js index 68abe6d601..92e89edbe4 100644 --- a/app/presenters/base.presenter.js +++ b/app/presenters/base.presenter.js @@ -1,5 +1,26 @@ 'use strict' +/** + * Capitalize the first letter of each word in a string + * + * Will work for strings containing multiple words or only one. + * + * @param {string} value The string to capitalize + * + * @returns {string} The capitalized string + */ +function capitalize (value) { + const words = value.split(' ') + const capitalizedWords = [] + + words.forEach((word) => { + const capitalizedWord = word.charAt(0).toUpperCase() + word.slice(1) + capitalizedWords.push(capitalizedWord) + }) + + return capitalizedWords.join(' ') +} + /** * Converts a number which represents pence into pounds by dividing it by 100 * @@ -123,6 +144,7 @@ function leftPadZeroes (number, length) { } module.exports = { + capitalize, convertPenceToPounds, formatAbstractionDate, formatAbstractionPeriod, diff --git a/test/presenters/base.presenter.test.js b/test/presenters/base.presenter.test.js index c9e53d51e5..a665d4b459 100644 --- a/test/presenters/base.presenter.test.js +++ b/test/presenters/base.presenter.test.js @@ -11,6 +11,58 @@ const { expect } = Code const BasePresenter = require('../../app/presenters/base.presenter.js') describe('Base presenter', () => { + describe('#capitalize()', () => { + let valueToCapitalize + + describe('when the value is a single word', () => { + beforeEach(() => { + valueToCapitalize = 'high' + }) + + it('correctly returns the value capitalized, for example, High', async () => { + const result = BasePresenter.capitalize(valueToCapitalize) + + expect(result).to.equal('High') + }) + }) + + describe('when the value is multiple words', () => { + beforeEach(() => { + valueToCapitalize = 'spray irrigation' + }) + + it('correctly returns the value capitalized, for example, Spray Irrigation', async () => { + const result = BasePresenter.capitalize(valueToCapitalize) + + expect(result).to.equal('Spray Irrigation') + }) + }) + + describe('when the value contains a symbol', () => { + beforeEach(() => { + valueToCapitalize = 'spray irrigation - direct' + }) + + it('correctly returns the value capitalized, for example, Spray Irrigation - Direct', async () => { + const result = BasePresenter.capitalize(valueToCapitalize) + + expect(result).to.equal('Spray Irrigation - Direct') + }) + }) + + describe('when the value is all capitals', () => { + beforeEach(() => { + valueToCapitalize = 'SPRAY IRRIGATION' + }) + + it('correctly returns the value unchanged, for example, SPRAY IRRIGATION', async () => { + const result = BasePresenter.capitalize(valueToCapitalize) + + expect(result).to.equal('SPRAY IRRIGATION') + }) + }) + }) + describe('#convertPenceToPounds()', () => { let valueInPence