Skip to content

Commit

Permalink
refactor following PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottDormand96 committed Dec 18, 2023
1 parent 98a85da commit bd176a2
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('choose payment > result function', () => {
expect(result).toBe(CommonResults.OK)
})

it.only('returns common result as recurring when payment is recurring payment', async () => {
it('returns common result as recurring when payment is recurring payment', async () => {
const mockRequest = getMockRequest({ payload: { 'recurring-payment': 'yes' } })

const result = await resultFunction(mockRequest)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`route getData returns expected values 1`] = `
Object {
"cost": Symbol(cost value),
"errorMap": Object {
"agree": Object {
"any.required": Object {
"ref": "#agree",
"text": "recurring payment error",
},
},
},
"reminder": Symbol(reminder value),
"type": Symbol(type value),
"uri": Object {
"single": Symbol(uri value),
"terms": Symbol(uri value),
},
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jest.mock('../../../../processors/uri-helper.js')
jest.mock('../../../../processors/recurring-pay-reminder-display.js')
jest.mock('../../../../processors/price-display.js')

const getSampleRequest = (permission = {}, catalog = {}) => ({
const getSampleRequest = (permission = {}, catalog = getCatalog()) => ({
cache: () => ({
helpers: {
transaction: {
Expand All @@ -26,6 +26,10 @@ const getSampleRequest = (permission = {}, catalog = {}) => ({
}
})

const getCatalog = () => ({
recurring_payment_set_up_error: 'recurring payment error'
})

describe('route', () => {
describe('validator', () => {
it('validator should validate "yes" as a valid choice', () => {
Expand All @@ -46,79 +50,32 @@ describe('route', () => {
})

describe('getData', () => {
it('cost equals return of displayPermissionPrice', async () => {
const returnValue = Symbol('return value')
displayPermissionPrice.mockReturnValueOnce(returnValue)
it('returns expected values', async () => {
const cost = Symbol('cost value')
const type = Symbol('type value')
const reminder = Symbol('reminder value')
const uri = Symbol('uri value')

const result = await getData(getSampleRequest())

expect(result.cost).toEqual(returnValue)
})

it('displayPermissionPrice is called with permission and getCatalog', async () => {
const catalog = Symbol('mock catalog')
const permission = Symbol('mock permission')
const request = getSampleRequest(permission, catalog)

await getData(request)

expect(displayPermissionPrice).toHaveBeenCalledWith(permission, catalog)
})

it('type equals return of recurringLicenceTypeDisplay', async () => {
const returnValue = Symbol('return value')
recurringLicenceTypeDisplay.mockReturnValueOnce(returnValue)
displayPermissionPrice.mockReturnValueOnce(cost)
recurringLicenceTypeDisplay.mockReturnValueOnce(type)
recurringPayReminderDisplay.mockReturnValueOnce(reminder)
addLanguageCodeToUri.mockReturnValue(uri)

const result = await getData(getSampleRequest())

expect(result.type).toEqual(returnValue)
expect(result).toMatchSnapshot()
})

it('recurringLicenceTypeDisplay is called with permission and getCatalog', async () => {
const catalog = Symbol('mock catalog')
const permission = Symbol('mock permission')
it.each([
[displayPermissionPrice, Symbol('mock catalog'), Symbol('mock permission')],
[recurringLicenceTypeDisplay, Symbol('mock catalog'), Symbol('mock permission')],
[recurringPayReminderDisplay, Symbol('mock catalog'), Symbol('mock permission')]
])('%s is called with permission and getCatalog', async (func, catalog, permission) => {
const request = getSampleRequest(permission, catalog)

await getData(request)

expect(recurringLicenceTypeDisplay).toHaveBeenCalledWith(permission, catalog)
})

it('reminder equals return of recurringPayReminderDisplay', async () => {
const returnValue = Symbol('return value')
recurringPayReminderDisplay.mockReturnValueOnce(returnValue)

const result = await getData(getSampleRequest())

expect(result.reminder).toEqual(returnValue)
})

it('recurringPayReminderDisplay is called with permission and getCatalog', async () => {
const catalog = Symbol('mock catalog')
const permission = Symbol('mock permission')
const request = getSampleRequest(permission, catalog)

await getData(request)

expect(recurringPayReminderDisplay).toHaveBeenCalledWith(permission, catalog)
})

it('single equals return of addLanguageCodeToUri with choose payment', async () => {
const returnValue = Symbol('return value')
addLanguageCodeToUri.mockReturnValue(returnValue)

const result = await getData(getSampleRequest())

expect(result.uri.single).toEqual(returnValue)
})

it('terms equals return of addLanguageCodeToUri with terms and conditions', async () => {
const returnValue = Symbol('return value')
addLanguageCodeToUri.mockReturnValue(returnValue)

const result = await getData(getSampleRequest())

expect(result.uri.single).toEqual(returnValue)
expect(func).toHaveBeenCalledWith(permission, catalog)
})

it.each([[CHOOSE_PAYMENT.uri], [TERMS_AND_CONDITIONS.uri]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@ export const validator = Joi.object({

export const getData = async request => {
const permission = await request.cache().helpers.transaction.getCurrentPermission()
const errorMsg = request.i18n.getCatalog().recurring_payment_set_up_error

return {
errorMap: {
agree: {
'any.required': { ref: '#agree', text: errorMsg }
}
},
cost: displayPermissionPrice(permission, request.i18n.getCatalog()),
type: recurringLicenceTypeDisplay(permission, request.i18n.getCatalog()),
reminder: recurringPayReminderDisplay(permission, request.i18n.getCatalog()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,11 @@
{% set title = mssgs.recurring_payment_set_up_title %}
{% block pageTitle %}{{ title }}{{ mssgs.gov_uk }}{% endblock %}

{% set errorMsg = mssgs.recurring_payment_set_up_error %}

{% set errorMap = {
'agree': {
'any.required': { ref: '#agree', text: errorMsg }
}
}
%}

{% block content %}
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">

{{ errorSummary(error, errorMap, mssgs.there_is_a_problem) }}
{{ errorSummary(error, data.errorMap, mssgs.there_is_a_problem) }}

<form method="post">

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ describe('recurringLicenceTypeDisplay', () => {
['Salmon and sea trout', null, ' salmon and sea trout'],
['Trout and coarse', '2', ' trout and coarse (2 rod)'],
['Trout and coarse', '3', ' trout and coarse (3 rod)']
])('returns correct licence type', (licenceType, numberOfRods, expected) => {
])('when licence type is %s and number of rods is: %s. recurringLicenceTypeDisplay will return "%s"', (licenceType, numberOfRods, expected) => {
const permission = getPermission({ licenceType, numberOfRods })
const result = recurringLicenceTypeDisplay(permission, getCatalog())
expect(result).toEqual(expected)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('recurringPayReminderDisplay', () => {
['Email', 'we will send you an email showing the cost before the next payment is taken'],
['Letter', 'we will send you a letter showing the cost before the next payment is taken'],
['Text', 'we will send you a text showing the cost before the next payment is taken']
])('returns correct wording for reminder', (reminder, expected) => {
])('when reminder is %s, recurringPayReminderDisplay will return "%s"', (reminder, expected) => {
const permission = getPermission(reminder)
const result = recurringPayReminderDisplay(permission, getCatalog())
expect(result).toEqual(expected)
Expand Down

0 comments on commit bd176a2

Please sign in to comment.