Skip to content

Commit

Permalink
Add capitalize() method to base presenter (#451)
Browse files Browse the repository at this point in the history
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](#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.
  • Loading branch information
Cruikshanks authored Oct 6, 2023
1 parent 8cd3399 commit 417dd2a
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
22 changes: 22 additions & 0 deletions app/presenters/base.presenter.js
Original file line number Diff line number Diff line change
@@ -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
*
Expand Down Expand Up @@ -123,6 +144,7 @@ function leftPadZeroes (number, length) {
}

module.exports = {
capitalize,
convertPenceToPounds,
formatAbstractionDate,
formatAbstractionPeriod,
Expand Down
52 changes: 52 additions & 0 deletions test/presenters/base.presenter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 417dd2a

Please sign in to comment.