Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CDPS-245: Add alert #263

Merged
merged 4 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions assets/js/alerts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function initDynamicDropdowns() {
const alertTypeElement = document.getElementById('alertType')
const alertCodeElement = document.getElementById('alertCode')
const typeCodeMap = JSON.parse(document.getElementById('typeCodeMap').textContent)

alertTypeElement.addEventListener('change', async () => {
alertCodeElement.length = 1
if (alertTypeElement.value === '') return

typeCodeMap[alertTypeElement.value]?.forEach(alertCode => {
alertCodeElement.add(new Option(alertCode.text, alertCode.value))
})
})
}

initDynamicDropdowns()
113 changes: 113 additions & 0 deletions integration_tests/e2e/addAlertPage.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import Page from '../pages/page'
import AlertsPage from '../pages/alertsPage'
import NotFoundPage from '../pages/notFoundPage'
import { Role } from '../../server/data/enums/role'
import AddAlertPage from '../pages/addAlertPage'
import { formatDate } from '../../server/utils/dateHelpers'

const visitAlertsPage = (): AlertsPage => {
cy.signIn({ redirectPath: '/prisoner/G6123VU/alerts/active' })
return Page.verifyOnPageWithTitle(AlertsPage, 'Active alerts')
}

context('Add Alert Page', () => {
beforeEach(() => {
cy.task('reset')
cy.setupUserAuth({ roles: [Role.GlobalSearch, Role.UpdateAlert] })
})

context('As a user with prisoner in their caseload', () => {
let alertsPage: AlertsPage
let addAlertPage: AddAlertPage

beforeEach(() => {
cy.setupAlertsPageStubs({ prisonerNumber: 'G6123VU', bookingId: 1102484 })
cy.task('stubGetAlertTypes')
cy.task('stubCreateAlert')
alertsPage = visitAlertsPage()
alertsPage.addAlertButton().click()
cy.location('pathname').should('eq', '/prisoner/G6123VU/add-alert')
addAlertPage = new AddAlertPage('Create an alert for John Saunders')
})

context('Adding a valid alert', () => {
it('should show correct prisoner number and default field values', () => {
addAlertPage.prisonerNumber().contains('G6123VU')
addAlertPage.typeField().contains('Choose alert type')
addAlertPage.subTypeField().contains('Choose alert code')
addAlertPage.textField().should('have.value', '')
addAlertPage.dateField().should('have.value', formatDate(new Date().toISOString(), 'short'))
})

it('should use the right url for back and cancel', () => {
addAlertPage.backLink().should('have.attr', 'href').and('contain', '/prisoner/G6123VU/alerts/active')
addAlertPage.cancelButton().should('have.attr', 'href').and('contain', '/prisoner/G6123VU/alerts/active')
})

it('should save successfully when valid data is entered', () => {
addAlertPage.typeField().select('AAA')
addAlertPage.typeField().invoke('val').should('eq', 'A')
addAlertPage.subTypeField().children().should('have.length', '2')
addAlertPage.subTypeField().select('AAA111')
addAlertPage.subTypeField().invoke('val').should('eq', 'A1')
addAlertPage.textField().type('This is a test')
addAlertPage.saveButton().click()
cy.location('pathname').should('eq', '/prisoner/G6123VU/alerts/active')
alertsPage.successMessage().should('contain.text', 'Alert added')
})
})

context('Attempting to add an invalid alert', () => {
it('should show validation messages when no data is entered', () => {
addAlertPage.dateField().clear()
addAlertPage.saveButton().click()
cy.location('pathname').should('eq', '/prisoner/G6123VU/add-alert')
addAlertPage.errorBlock().should('exist')
addAlertPage.errorBlock().should('contain.text', 'Select the alert type')
addAlertPage.errorBlock().should('contain.text', 'Select the alert code')
addAlertPage.errorBlock().should('contain.text', 'Enter why you are creating this alert')
addAlertPage.errorBlock().should('contain.text', 'Select the alert start date')
addAlertPage.typeField().should('have.class', 'govuk-select--error')
addAlertPage.subTypeField().should('have.class', 'govuk-select--error')
})

it('should show validation messages when invalid data is entered', () => {
addAlertPage.dateField().clear().type('abc')
addAlertPage.saveButton().click()
cy.location('pathname').should('eq', '/prisoner/G6123VU/add-alert')
addAlertPage.errorBlock().should('exist')
addAlertPage.errorBlock().should('contain.text', 'Select the alert type')
addAlertPage.errorBlock().should('contain.text', 'Select the alert code')
addAlertPage.errorBlock().should('contain.text', 'Enter why you are creating this alert')
addAlertPage
.errorBlock()
.should('contain.text', 'Enter a real date in the format DD/MM/YYYY - for example, 27/03/2023')
})
})
})

context('As a user without prisoner in their caseload', () => {
beforeEach(() => {
cy.task('reset')
cy.setupUserAuth({
roles: [Role.GlobalSearch],
caseLoads: [{ caseloadFunction: '', caseLoadId: 'ZZZ', currentlyActive: true, description: '', type: '' }],
activeCaseLoadId: 'ZZZ',
})
cy.task('stubGetAlertTypes')
})

context('Page Not Found', () => {
beforeEach(() => {
cy.setupBannerStubs({ prisonerNumber: 'G6123VU' })
cy.task('stubInmateDetail', 1102484)
cy.task('stubPrisonerDetail', 'G6123VU')
})

it('Displays Page Not Found', () => {
cy.signIn({ failOnStatusCode: false, redirectPath: '/prisoner/G6123VU/add-alert' })
Page.verifyOnPage(NotFoundPage)
})
})
})
})
37 changes: 35 additions & 2 deletions integration_tests/mockApis/prison.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,19 @@ import { CaseLoad } from '../../server/interfaces/caseLoad'
import { CaseNoteUsage } from '../../server/interfaces/prisonApi/caseNoteUsage'

import {
mainOffenceMock,
fullStatusMock,
fullStatusRemandMock,
mainOffenceMock,
} from '../../server/data/localMockData/offenceOverviewMock'
import { FullStatus } from '../../server/interfaces/prisonApi/fullStatus'
import { identifiersMock } from '../../server/data/localMockData/identifiersMock'
import { StaffRole } from '../../server/interfaces/prisonApi/staffRole'

import {
SentenceSummaryWithSentenceMock,
SentenceSummaryWithoutSentenceMock,
SentenceSummaryWithSentenceMock,
} from '../../server/data/localMockData/sentenceSummaryMock'
import { alertTypesMock } from '../../server/data/localMockData/alertTypesMock'

const placeHolderImagePath = './../../assets/images/average-face.jpg'

Expand Down Expand Up @@ -812,4 +813,36 @@ export default {
},
})
},

stubGetAlertTypes: () => {
return stubFor({
request: {
method: 'GET',
urlPattern: `/prison/api/reference-domains/domains/ALERT\\?withSubCodes=true`,
},
response: {
status: 200,
headers: {
'Content-Type': 'application/json;charset=UTF-8',
},
jsonBody: alertTypesMock,
},
})
},

stubCreateAlert: () => {
return stubFor({
request: {
method: 'POST',
urlPattern: `/prison/api/bookings/1102484/alert`,
},
response: {
status: 200,
headers: {
'Content-Type': 'application/json;charset=UTF-8',
},
jsonBody: pagedActiveAlertsMock.content[0],
},
})
},
}
23 changes: 23 additions & 0 deletions integration_tests/pages/addAlertPage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Page, { PageElement } from './page'

export default class AddAlertPage extends Page {
h1 = (): PageElement => cy.get('h1')

prisonerNumber = (): PageElement => cy.get('[data-qa=prison-number]')

typeField = (): PageElement => cy.get('#alertType')

subTypeField = (): PageElement => cy.get('#alertCode')

textField = (): PageElement => cy.get('#comment')

dateField = (): PageElement => cy.get('#alertDate')

saveButton = (): PageElement => cy.get('[data-qa=add-alert-submit-button]')

cancelButton = (): PageElement => cy.get('[data-qa=add-alert-cancel-button]')

backLink = (): PageElement => cy.get('[data-qa=referer-back-link]')

errorBlock = (): PageElement => cy.get('.govuk-error-summary')
}
2 changes: 2 additions & 0 deletions integration_tests/pages/alertsPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ export default class AlertsPage extends Page {
updateAlertLink = (): PageElement => cy.get('.hmpps-alert-card-list-item__link a')

viewAllLink = (): PageElement => cy.get('.hmpps-pagination-view-all a')

successMessage = (): PageElement => cy.get('.hmpps-flash-message--success > p')
}
Loading