Skip to content

Commit

Permalink
refactor: page title pagination logic into presenter
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathangoulding committed Jan 24, 2025
1 parent 78ba9f3 commit 507d3cf
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 23 deletions.
13 changes: 11 additions & 2 deletions app/presenters/notifications/setup/review.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ const { titleCase } = require('../../base.presenter.js')
*
* @param {object[]} recipients - List of recipient objects, each containing recipient details like email or name.
* @param {number|string} page - The currently selected page
* @param {object} pagination -
*
* @returns {object} - The data formatted for the view template
*/
function go(recipients, page) {
function go(recipients, page, pagination) {
return {
defaultPageSize,
pageTitle: 'Send returns invitations',
pageTitle: _pageTitle(page, pagination),
recipients: _recipients(recipients, page),
recipientsAmount: recipients.length
}
Expand Down Expand Up @@ -54,6 +55,14 @@ function _licences(licences) {
return licences.split(',')
}

function _pageTitle(page, pagination) {
if (pagination.numberOfPages > 1) {
return `Send returns invitations (page ${page} of ${pagination.numberOfPages})`
}

return 'Send returns invitations'
}

/**
* Due to the complexity of the query to get the recipients data, we handle pagination in the presenter.
*
Expand Down
6 changes: 3 additions & 3 deletions app/services/notifications/setup/review.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ async function go(sessionId, page = 1) {

const dedupeRecipients = DedupeRecipientsService.go(recipients)

const formattedData = ReviewPresenter.go(dedupeRecipients, page)

const pagination = PaginatorPresenter.go(
formattedData.recipientsAmount,
dedupeRecipients.length,
Number(page),
`/system/notifications/setup/${sessionId}/review`
)

const formattedData = ReviewPresenter.go(dedupeRecipients, page, pagination)

return {
activeNavBar: 'manage',
...formattedData,
Expand Down
2 changes: 1 addition & 1 deletion app/views/notifications/setup/review.njk
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

{% block content %}
<div class="govuk-body">
<h1 class="govuk-heading-l">{{ pageTitle }} {% if pagination.numberOfPages > 1 %} (page {{ page }} of {{ pagination.numberOfPages }}) {% endif %}</h1>
<h1 class="govuk-heading-l"> {{ pageTitle }} </h1>

<p> Returns invitations are ready to send.</p>

Expand Down
58 changes: 41 additions & 17 deletions test/presenters/notifications/setup/review.presenter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,24 @@ const RecipientsFixture = require('../../../fixtures/recipients.fixtures.js')
const ReviewPresenter = require('../../../../app/presenters/notifications/setup/review.presenter.js')

describe('Notifications Setup - Review presenter', () => {
let testRecipients
let testInput
let page
let pagination
let testInput
let testRecipients

beforeEach(() => {
page = 1
pagination = {
numberOfPages: 1
}

testRecipients = RecipientsFixture.recipients()
testInput = Object.values(testRecipients)

page = 1
})

describe('when provided with "recipients"', () => {
it('correctly presents the data', () => {
const result = ReviewPresenter.go(testInput, page)
const result = ReviewPresenter.go(testInput, page, pagination)

expect(result).to.equal({
defaultPageSize: 25,
Expand Down Expand Up @@ -66,7 +70,7 @@ describe('Notifications Setup - Review presenter', () => {
describe('the "recipients" property', () => {
describe('format all recipient types', () => {
it('should return the formatted recipients', () => {
const result = ReviewPresenter.go(testInput, page)
const result = ReviewPresenter.go(testInput, page, pagination)

expect(result.recipients).to.equal([
{
Expand Down Expand Up @@ -100,7 +104,7 @@ describe('Notifications Setup - Review presenter', () => {
describe('the "contact" property', () => {
describe('when the contact is an email', () => {
it('should return the email address', () => {
const result = ReviewPresenter.go(testInput, page)
const result = ReviewPresenter.go(testInput, page, pagination)

const testRecipient = result.recipients.find((recipient) =>
recipient.licences.includes(testRecipients.primaryUser.all_licences)
Expand All @@ -116,7 +120,7 @@ describe('Notifications Setup - Review presenter', () => {

describe('when the contact is an address', () => {
it('should convert the contact into an array', () => {
const result = ReviewPresenter.go(testInput, page)
const result = ReviewPresenter.go(testInput, page, pagination)

const testRecipient = result.recipients.find((recipient) =>
recipient.licences.includes(testRecipients.licenceHolder.all_licences)
Expand All @@ -134,7 +138,7 @@ describe('Notifications Setup - Review presenter', () => {
describe('the "licences" property', () => {
describe('when the recipient has a single licence number', () => {
it('should return licence numbers as an array', () => {
const result = ReviewPresenter.go(testInput, page)
const result = ReviewPresenter.go(testInput, page, pagination)

const testRecipient = result.recipients.find((recipient) =>
recipient.licences.includes(testRecipients.licenceHolder.all_licences)
Expand All @@ -146,7 +150,7 @@ describe('Notifications Setup - Review presenter', () => {

describe('when the recipient has multiple licence numbers', () => {
it('should return licence numbers as an array', () => {
const result = ReviewPresenter.go(testInput, page)
const result = ReviewPresenter.go(testInput, page, pagination)

const testRecipient = result.recipients.find(
(recipient) =>
Expand All @@ -164,7 +168,7 @@ describe('Notifications Setup - Review presenter', () => {

describe('and there are <= 25 recipients ', () => {
it('returns all the recipients', () => {
const result = ReviewPresenter.go(testInput, page)
const result = ReviewPresenter.go(testInput, page, pagination)

expect(result.recipients.length).to.equal(testInput.length)
})
Expand All @@ -173,21 +177,41 @@ describe('Notifications Setup - Review presenter', () => {
describe('and there are >= 25 recipients', () => {
beforeEach(() => {
testInput = [...testInput, ...testInput, ...testInput, ...testInput, ...testInput, ...testInput]

pagination = {
numberOfPages: 2
}
})

describe('and the page is 1', () => {
it('returns the first 25 recipients', () => {
const result = ReviewPresenter.go(testInput, page)
const result = ReviewPresenter.go(testInput, page, pagination)

expect(result.recipients.length).to.equal(25)
})

describe('and there is more than one page', () => {
it('returns the remaining recipients', () => {
const result = ReviewPresenter.go(testInput, '2')
it('returns the updated "pageTitle"', () => {
const result = ReviewPresenter.go(testInput, page, pagination)

expect(result.recipients.length).to.equal(5)
})
expect(result.pageTitle).to.equal('Send returns invitations (page 1 of 2)')
})
})

describe('and there is more than one page', () => {
beforeEach(() => {
page = '2'
})

it('returns the remaining recipients', () => {
const result = ReviewPresenter.go(testInput, page, pagination)

expect(result.recipients.length).to.equal(5)
})

it('returns the updated "pageTitle"', () => {
const result = ReviewPresenter.go(testInput, page, pagination)

expect(result.pageTitle).to.equal('Send returns invitations (page 2 of 2)')
})
})
})
Expand Down

0 comments on commit 507d3cf

Please sign in to comment.