Skip to content

Commit

Permalink
Update Accredited Programme history page for refer
Browse files Browse the repository at this point in the history
When raising a referral and adding missing history records, you can now only change and remove the ones you have added as part of that referral.
  • Loading branch information
jsrobertson committed Jan 15, 2025
1 parent a1968a9 commit 21ed9f4
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 106 deletions.
79 changes: 40 additions & 39 deletions integration_tests/e2e/refer/new/programmeHistory.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,18 @@ context('Programme history', () => {
const courseNames = courses.map(course => course.name)
const addedByUser1 = userFactory.build({ username: auth.mockedUser.username })
const addedByUser2 = userFactory.build()

const courseOffering = courseOfferingFactory.build()
const referral = referralFactory.started().build({
offeringId: courseOffering.id,
prisonNumber: person.prisonNumber,
referrerUsername: addedByUser1.username,
})
const courseParticipationWithKnownCourseName = courseParticipationFactory.build({
addedBy: addedByUser1.username,
courseName: courses[0].name,
prisonNumber: person.prisonNumber,
referralId: referral.id,
})
const courseParticipationWithKnownCourseNamePresenter: CourseParticipationPresenter = {
...courseParticipationWithKnownCourseName,
Expand All @@ -52,16 +60,6 @@ context('Programme history', () => {
courseName: 'An course not in our system',
prisonNumber: person.prisonNumber,
})
const courseParticipationWithUnknownCourseNamePresenter: CourseParticipationPresenter = {
...courseParticipationWithUnknownCourseName,
addedByDisplayName: StringUtils.convertToTitleCase(addedByUser2.name),
}
const courseOffering = courseOfferingFactory.build()
const referral = referralFactory.started().build({
offeringId: courseOffering.id,
prisonNumber: person.prisonNumber,
referrerUsername: addedByUser1.username,
})
const programmeHistoryPath = referPaths.new.programmeHistory.index({ referralId: referral.id })
const newParticipationPath = referPaths.new.programmeHistory.new({ referralId: referral.id })
const showProgrammeHistoryPath = referPaths.new.programmeHistory.show({
Expand All @@ -86,36 +84,47 @@ context('Programme history', () => {
const prison = prisonFactory.build({ prisonId: courseOffering.organisationId })
const organisation = OrganisationUtils.organisationFromPrison(prison)

const courseParticipations = [courseParticipationWithKnownCourseName, courseParticipationWithUnknownCourseName]
const courseParticipationsPresenter: Array<CourseParticipationPresenter> = [
courseParticipationWithKnownCourseNamePresenter,
courseParticipationWithUnknownCourseNamePresenter,
]
const existingParticipations = [courseParticipationWithKnownCourseName, courseParticipationWithUnknownCourseName]
const referralParticipations = courseParticipationFactory.buildList(2, {
isDraft: true,
referralId: referral.id,
})

beforeEach(() => {
cy.task('stubCourse', courses[0])
})

describe('when there is an existing programme history', () => {
beforeEach(() => {
cy.task('stubParticipationsByPerson', { courseParticipations, prisonNumber: prisoner.prisonerNumber })
cy.task('stubParticipationsByPerson', {
courseParticipations: existingParticipations,
prisonNumber: prisoner.prisonerNumber,
})
cy.task('stubParticipationsByReferral', {
courseParticipations: referralParticipations,
referralId: referral.id,
})

cy.visit(programmeHistoryPath)
})

it('shows the page with an existing programme history', () => {
const programmeHistoryPage = Page.verifyOnPage(NewReferralProgrammeHistoryPage, {
participations: courseParticipationsPresenter,
person,
referral,
})
programmeHistoryPage.shouldHavePersonDetails(person)
programmeHistoryPage.shouldContainBackLink(referPaths.new.show({ referralId: referral.id }))
programmeHistoryPage.shouldContainHomeLink()
programmeHistoryPage.shouldNotContainSuccessMessage()
programmeHistoryPage.shouldContainPreHistoryText()
programmeHistoryPage.shouldContainPreHistoryParagraph()
programmeHistoryPage.shouldContainHistorySummaryCards(courseParticipationsPresenter, referral.id)
programmeHistoryPage.shouldContainHistoryTable(existingParticipations, referral.id, 'existing-participations')
programmeHistoryPage.shouldContainHistoryTable(
referralParticipations,
referral.id,
'referral-participations',
true,
)
programmeHistoryPage.shouldContainButtonLink('Add a programme', newParticipationPath)
programmeHistoryPage.shouldContainButton('Return to tasklist')
})
Expand All @@ -130,7 +139,6 @@ context('Programme history', () => {

it('updates the referral and redirects to the task list', () => {
const programmeHistoryPage = Page.verifyOnPage(NewReferralProgrammeHistoryPage, {
participations: courseParticipationsPresenter,
person,
referral,
})
Expand All @@ -149,20 +157,22 @@ context('Programme history', () => {

describe('when there is no existing programme history', () => {
const emptyCourseParticipations: Array<CourseParticipation> = []
const emptyCourseParticipationsPresenter: Array<CourseParticipationPresenter> = []

beforeEach(() => {
cy.task('stubParticipationsByPerson', {
courseParticipations: emptyCourseParticipations,
prisonNumber: prisoner.prisonerNumber,
})
cy.task('stubParticipationsByReferral', {
courseParticipations: emptyCourseParticipations,
referralId: referral.id,
})

cy.visit(programmeHistoryPath)
})

it('shows the page without an existing programme history', () => {
const programmeHistoryPage = Page.verifyOnPage(NewReferralProgrammeHistoryPage, {
participations: emptyCourseParticipationsPresenter,
person,
referral,
})
Expand All @@ -186,7 +196,6 @@ context('Programme history', () => {

it('updates the referral and redirects to the task list', () => {
const programmeHistoryPage = Page.verifyOnPage(NewReferralProgrammeHistoryPage, {
participations: emptyCourseParticipationsPresenter,
person,
referral,
})
Expand Down Expand Up @@ -419,16 +428,13 @@ context('Programme history', () => {
status: formValues.outcome.status,
yearCompleted: Number(formValues.outcome),
},
referralId: referral.id,
setting: {
location: formValues.setting.communityLocation,
type: formValues.setting.type,
},
source: formValues.source,
})
const updatedCourseParticipationPresenter: CourseParticipationPresenter = {
...updatedCourseParticipation,
addedByDisplayName: StringUtils.convertToTitleCase(addedByUser1.name),
}

const programmeHistoryDetailsPage = Page.verifyOnPage(NewReferralProgrammeHistoryDetailsPage, {
course: courses[0],
Expand All @@ -444,7 +450,6 @@ context('Programme history', () => {
programmeHistoryDetailsPage.submitDetails()

Page.verifyOnPage(NewReferralProgrammeHistoryPage, {
participations: [{ ...updatedCourseParticipationPresenter, name: courses[0].name }],
person,
referral,
})
Expand Down Expand Up @@ -528,18 +533,20 @@ context('Programme history', () => {
})

describe('success messages', () => {
const courseParticipations = [courseParticipationWithKnownCourseName]
const courseParticipationsPresenter = [courseParticipationWithKnownCourseNamePresenter]
const addedSuccessMessage = 'You have successfully added a programme.'
const updatedSuccessMessage = 'You have successfully updated a programme.'

beforeEach(() => {
cy.task('stubCourseNames', courseNames)
cy.task('stubCourse', courses[0])
cy.task('stubParticipationsByPerson', {
courseParticipations,
prisonNumber: prisoner.prisonerNumber,
courseParticipations: [],
prisonNumber: person.prisonNumber,
})
cy.task('stubParticipationsByReferral', {
courseParticipations: [courseParticipationWithKnownCourseName],
referralId: referral.id,
})
cy.task('stubCourse', courses[0])
})

describe('when adding a new participation', () => {
Expand All @@ -563,7 +570,6 @@ context('Programme history', () => {
programmeHistoryDetailsPage.submitDetails()

const programmeHistoryPage = Page.verifyOnPage(NewReferralProgrammeHistoryPage, {
participations: courseParticipationsPresenter,
person,
referral,
})
Expand All @@ -576,7 +582,6 @@ context('Programme history', () => {
cy.visit(programmeHistoryPath)

const programmeHistoryPage = Page.verifyOnPage(NewReferralProgrammeHistoryPage, {
participations: courseParticipationsPresenter,
person,
referral,
})
Expand All @@ -589,7 +594,6 @@ context('Programme history', () => {
cy.visit(programmeHistoryPath)

Page.verifyOnPage(NewReferralProgrammeHistoryPage, {
participations: courseParticipationsPresenter,
person,
referral,
})
Expand All @@ -609,7 +613,6 @@ context('Programme history', () => {
programmeHistoryDetailsPage.submitDetails()

const programmeHistoryPage = Page.verifyOnPage(NewReferralProgrammeHistoryPage, {
participations: courseParticipationsPresenter,
person,
referral,
})
Expand Down Expand Up @@ -643,7 +646,6 @@ context('Programme history', () => {
cy.visit(programmeHistoryPath)

const programmeHistoryPage = Page.verifyOnPage(NewReferralProgrammeHistoryPage, {
participations: courseParticipationsPresenter,
person,
referral,
})
Expand Down Expand Up @@ -702,7 +704,6 @@ context('Programme history', () => {
deleteProgrammeHistoryPage.confirm()

const programmeHistoryPage = Page.verifyOnPage(NewReferralProgrammeHistoryPage, {
participations: [],
person,
referral,
})
Expand Down
16 changes: 16 additions & 0 deletions integration_tests/mockApis/courseParticipations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ export default {
},
}),

stubParticipationsByReferral: (args: {
courseParticipations: Array<CourseParticipation>
referralId: CourseParticipation['referralId']
}): SuperAgentRequest =>
stubFor({
request: {
method: 'GET',
url: apiPaths.participations.referral({ referralId: args.referralId }),
},
response: {
headers: { 'Content-Type': 'application/json;charset=UTF-8' },
jsonBody: args.courseParticipations,
status: 200,
},
}),

stubUpdateParticipation: (courseParticipation: CourseParticipation): SuperAgentRequest =>
stubFor({
request: {
Expand Down
31 changes: 30 additions & 1 deletion integration_tests/pages/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import type {
MojTimelineItem,
ReferralStatusHistoryPresenter,
} from '@accredited-programmes/ui'
import type { Referral } from '@accredited-programmes-api'
import type { CourseParticipation, Referral } from '@accredited-programmes-api'
import type {
GovukFrontendRadiosItem,
GovukFrontendSummaryListCardTitle,
Expand Down Expand Up @@ -205,6 +205,35 @@ export default abstract class Page {
})
}

shouldContainHistoryTable(
participations: Array<CourseParticipation>,
referralId: Referral['id'],
testId: string,
editable = false,
) {
const { rows } = CourseParticipationUtils.table(participations, referralId, testId, editable)

cy.get(`[data-testid="${testId}"] .govuk-table__body`).within(() => {
cy.get('.govuk-table__row').each((tableRowElement, tableRowElementIndex) => {
const row = rows[tableRowElementIndex]

cy.wrap(tableRowElement).within(() => {
row.forEach((cell, cellIndex) => {
cy.get('.govuk-table__cell')
.eq(cellIndex)
.then(cellElement => {
if ('text' in cell) {
cy.wrap(cellElement).should('contain.text', cell.text)
} else {
cy.wrap(cellElement).should('contain.html', cell.html)
}
})
})
})
})
})
}

shouldContainHomeLink(): void {
cy.get('[data-testid=home-link]').should('have.attr', 'href', '/')
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default class NewReferralDeleteProgrammeHistoryPage extends Page {

confirm() {
cy.task('stubParticipationsByPerson', { courseParticipations: [], prisonNumber: this.person.prisonNumber })
cy.task('stubParticipationsByReferral', { courseParticipations: [], referralId: this.referral.id })
this.shouldContainButton('Confirm').click()
}
}
20 changes: 3 additions & 17 deletions integration_tests/pages/refer/new/programmeHistory.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
import Helpers from '../../../support/helpers'
import Page from '../../page'
import type { Person } from '@accredited-programmes/models'
import type { CourseParticipationPresenter } from '@accredited-programmes/ui'
import type { Referral } from '@accredited-programmes-api'

export default class NewReferralProgrammeHistoryPage extends Page {
participations: Array<CourseParticipationPresenter>

person: Person

referral: Referral

constructor(args: { participations: Array<CourseParticipationPresenter>; person: Person; referral: Referral }) {
constructor(args: { person: Person; referral: Referral }) {
super('Accredited Programme history', {
hideTitleServiceName: true,
pageTitleOverride: "Person's Accredited Programme history",
})

const { participations, person, referral } = args
this.participations = participations
const { person, referral } = args
this.person = person
this.referral = referral
}
Expand Down Expand Up @@ -55,20 +51,10 @@ export default class NewReferralProgrammeHistoryPage extends Page {
shouldContainPreHistoryParagraph() {
cy.get('[data-testid="pre-history-paragraph"]').should(
'have.text',
'Add a programme if you know they completed or started a programme not listed here. Return to the tasklist once you’ve added all known programme history.',
`This is a list of programmes ${this.person.name} has started or completed. You can add missing programme history.`,
)
}

shouldContainPreHistoryText() {
cy.get('[data-testid="history-text"]').then(historyTextElement => {
const { actual, expected } = Helpers.parseHtml(
historyTextElement,
`The history shows ${this.person.name} has previously started or completed an Accredited Programme.`,
)
expect(actual).to.equal(expected)
})
}

shouldContainSuccessMessage(message: string) {
cy.get('[data-testid="success-banner"]').then(successBannerElement => {
const { actual, expected } = Helpers.parseHtml(successBannerElement, message)
Expand Down
6 changes: 5 additions & 1 deletion integration_tests/pages/refer/new/programmeHistoryDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,13 @@ export default class NewReferralProgrammeHistoryDetailsPage extends Page {
cy.task('stubUpdateParticipation', this.courseParticipation)
cy.task('stubCourse', this.course)
cy.task('stubParticipationsByPerson', {
courseParticipations: [this.courseParticipation],
courseParticipations: [],
prisonNumber: this.person.prisonNumber,
})
cy.task('stubParticipationsByReferral', {
courseParticipations: [this.courseParticipation],
referralId: this.courseParticipation.referralId,
})
this.shouldContainButton('Continue').click()
}
}
Loading

0 comments on commit 21ed9f4

Please sign in to comment.