Skip to content

Commit

Permalink
Merge branch 'main' into jest
Browse files Browse the repository at this point in the history
  • Loading branch information
MJ B authored Oct 7, 2023
2 parents 931f99f + 417dd2a commit 470f7db
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 470f7db

Please sign in to comment.