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

Add filtering and pagination to OOS bed list for a given premises #1963

Merged
merged 8 commits into from
Jul 5, 2024
130 changes: 88 additions & 42 deletions integration_tests/mockApis/outOfServiceBed.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { SuperAgentRequest } from 'superagent'

import { Cas1OutOfServiceBedSortField as OutOfServiceBedSortField, SortDirection } from '@approved-premises/api'
import {
Cas1OutOfServiceBed as OutOfServiceBed,
Cas1OutOfServiceBedSortField as OutOfServiceBedSortField,
SortDirection,
Temporality,
} from '@approved-premises/api'
import { getMatchingRequests, stubFor } from './setup'
import { bedspaceConflictResponseBody, errorStub } from './utils'
import paths from '../../server/paths/api'
Expand Down Expand Up @@ -52,30 +57,51 @@ export default {
},
}),

stubOutOfServiceBedsListForAPremises: ({ premisesId, outOfServiceBeds }): SuperAgentRequest =>
stubFor({
request: {
method: 'GET',
url: paths.manage.premises.outOfServiceBeds.premisesIndex({ premisesId }),
},
response: {
status: 200,
headers,
jsonBody: outOfServiceBeds,
},
}),

stubOutOfServiceBedsList: ({
outOfServiceBeds,
premisesId,
page = 1,
sortBy = 'startDate',
sortDirection = 'asc',
sortBy,
sortDirection,
temporality = 'current',
}): SuperAgentRequest =>
stubFor({
perPage = 10,
}: {
outOfServiceBeds: Array<OutOfServiceBed>
premisesId?: string
page: number
temporality?: string
sortBy?: OutOfServiceBedSortField
sortDirection?: SortDirection
perPage?: number
}): SuperAgentRequest => {
const queryParameters = {
page: {
equalTo: page.toString(),
},

temporality: {
equalTo: temporality,
},
} as Record<string, unknown>

if (premisesId) {
queryParameters.premisesId = { equalTo: premisesId }
}
if (sortBy) {
queryParameters.sortBy = { equalTo: sortBy }
}
if (sortDirection) {
queryParameters.sortDirection = { equalTo: sortDirection }
}
if (perPage) {
queryParameters.perPage = { equalTo: perPage.toString() }
}

return stubFor({
request: {
method: 'GET',
url: `${paths.manage.outOfServiceBeds.index.pattern}?page=${page}&sortBy=${sortBy}&sortDirection=${sortDirection}&temporality=${temporality}`,
urlPathPattern: paths.manage.outOfServiceBeds.index.pattern,
queryParameters,
},
response: {
status: 200,
Expand All @@ -87,7 +113,8 @@ export default {
},
jsonBody: outOfServiceBeds,
},
}),
})
},

stubOutOfServiceBed: ({ premisesId, outOfServiceBed }): SuperAgentRequest =>
stubFor({
Expand Down Expand Up @@ -154,29 +181,48 @@ export default {
})
).body.requests,
verifyOutOfServiceBedsDashboard: async ({
page = '1',
sortBy = 'startDate',
sortDirection = 'asc',
page = 1,
sortBy,
sortDirection,
temporality = 'current',
premisesId,
perPage = 10,
}: {
page: string
page: number
patrickjfl marked this conversation as resolved.
Show resolved Hide resolved
sortBy: OutOfServiceBedSortField
sortDirection: SortDirection
}) =>
(
await getMatchingRequests({
method: 'GET',
urlPathPattern: paths.manage.outOfServiceBeds.index({}),
queryParameters: {
page: {
equalTo: page,
},
sortBy: {
equalTo: sortBy,
},
sortDirection: {
equalTo: sortDirection,
},
},
})
).body.requests,
temporality: Temporality
premisesId: string
perPage?: number
}) => {
const queryParameters = {
page: {
equalTo: page.toString(),
},
temporality: {
equalTo: temporality,
},
} as Record<string, unknown>

if (premisesId) {
queryParameters.premisesId = { equalTo: premisesId }
}
if (sortBy) {
queryParameters.sortBy = { equalTo: sortBy }
}
if (sortDirection) {
queryParameters.sortDirection = { equalTo: sortDirection }
}
if (perPage) {
queryParameters.perPage = { equalTo: perPage.toString() }
}

const requests = await getMatchingRequests({
method: 'GET',
urlPathPattern: paths.manage.outOfServiceBeds.index.pattern,
queryParameters,
})

return requests.body.requests
},
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Cas1OutOfServiceBed as OutOfServiceBed, Premises } from '@approved-premises/api'
import type { Cas1OutOfServiceBed as OutOfServiceBed, Premises, Temporality } from '@approved-premises/api'
import paths from '../../../../server/paths/manage'

import Page from '../../page'
Expand All @@ -8,8 +8,8 @@ export class OutOfServiceBedListPage extends Page {
super('Manage out of service beds')
}

static visit(premisesId: Premises['id']): OutOfServiceBedListPage {
cy.visit(paths.v2Manage.outOfServiceBeds.premisesIndex({ premisesId }))
static visit(premisesId: Premises['id'], temporality: Temporality): OutOfServiceBedListPage {
cy.visit(paths.v2Manage.outOfServiceBeds.premisesIndex({ premisesId, temporality }))
return new OutOfServiceBedListPage()
}

Expand Down
99 changes: 78 additions & 21 deletions integration_tests/tests/v2Manage/outOfServiceBeds.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,17 @@ context('OutOfServiceBeds', () => {
// And there is a out of service bed in the database
const outOfServiceBed = outOfServiceBedFactory.build()
cy.task('stubOutOfServiceBed', { premisesId, outOfServiceBed })
cy.task('stubOutOfServiceBedsListForAPremises', { premisesId, outOfServiceBeds: [outOfServiceBed] })
cy.task('stubOutOfServiceBedsList', {
premisesId,
outOfServiceBeds: [outOfServiceBed],
perPage: 50,
})
cy.task('stubOutOfServiceBedUpdate', { premisesId, outOfServiceBed })

// When I visit the out of service bed index page
const outOfServiceBedListPage = OutOfServiceBedListPage.visit(premisesId)
const outOfServiceBedListPage = OutOfServiceBedListPage.visit(premisesId, 'current')

// Then I see the out of service beds for that premises
outOfServiceBedListPage.shouldShowOutOfServiceBeds([outOfServiceBed])

// // When I click manage on a bed
// // And I click manage on a bed
outOfServiceBedListPage.clickManageBed(outOfServiceBed)

// // Then I should see the out of service bed manage form
Expand Down Expand Up @@ -193,7 +194,7 @@ context('OutOfServiceBeds', () => {
// And there is a out of service bed in the database
const outOfServiceBed = outOfServiceBedFactory.build()
cy.task('stubOutOfServiceBed', { premisesId, outOfServiceBed })
cy.task('stubOutOfServiceBedsListForAPremises', { premisesId, outOfServiceBeds: [outOfServiceBed] })
cy.task('stubOutOfServiceBedsList', { premisesId, outOfServiceBeds: [outOfServiceBed] })

// And I miss required fields
cy.task('stubUpdateOutOfServiceBedErrors', {
Expand Down Expand Up @@ -225,7 +226,7 @@ context('OutOfServiceBeds', () => {
const outOfServiceBed = outOfServiceBedFactory.build()
const outOfServiceBedCancellation = outOfServiceBedCancellationFactory.build()
cy.task('stubOutOfServiceBed', { premisesId, outOfServiceBed })
cy.task('stubOutOfServiceBedsListForAPremises', { premisesId, outOfServiceBeds: [outOfServiceBed] })
cy.task('stubOutOfServiceBedsList', { premisesId, outOfServiceBeds: [outOfServiceBed], perPage: 50 })
cy.task('stubCancelOutOfServiceBed', {
premisesId,
outOfServiceBedId: outOfServiceBed.id,
Expand All @@ -251,6 +252,53 @@ context('OutOfServiceBeds', () => {
})
})

describe('list all OOS beds for a given AP', () => {
const premisesId = 'abc123'
const outOfServiceBeds = outOfServiceBedFactory.buildList(10)

beforeEach(() => {
cy.task('reset')
// Given I am signed in as a future manager
signIn(['future_manager'])
})

it('allows me to view all out of service beds for a premises', () => {
// And there are out of service beds in the database
cy.task('stubOutOfServiceBedsList', { premisesId, outOfServiceBeds, page: 1, perPage: 50 })

// When I visit the out of service bed index page for a premises
const outOfServiceBedListPage = OutOfServiceBedListPage.visit(premisesId, 'current')

// Then I see the out of service beds for that premises
outOfServiceBedListPage.shouldShowOutOfServiceBeds(outOfServiceBeds)
})

it('supports pagination', () => {
cy.task('stubOutOfServiceBedsList', { outOfServiceBeds, page: 1, premisesId, perPage: 50 })
cy.task('stubOutOfServiceBedsList', { outOfServiceBeds, page: 2, premisesId, perPage: 50 })
cy.task('stubOutOfServiceBedsList', { outOfServiceBeds, page: 9, premisesId, perPage: 50 })

// When I visit the OOS beds index page for a premises
const page = OutOfServiceBedListPage.visit(premisesId, 'current')

// And I click next
page.clickNext()

// Then the API should have received a request for the next page
cy.task('verifyOutOfServiceBedsDashboard', { page: 2, premisesId, perPage: 50 }).then(requests => {
expect(requests).to.have.length(1)
})

// When I click on a page number
page.clickPageNumber('9')

// Then the API should have received a request for the that page number
cy.task('verifyOutOfServiceBedsDashboard', { page: 9, premisesId, perPage: 50 }).then(requests => {
expect(requests).to.have.length(1)
})
})
})

describe('CRU Member lists all OOS beds', () => {
beforeEach(() => {
cy.task('reset')
Expand All @@ -277,9 +325,9 @@ context('OutOfServiceBeds', () => {
})

it('supports pagination', () => {
cy.task('stubOutOfServiceBedsList', { outOfServiceBeds, page: '1' })
cy.task('stubOutOfServiceBedsList', { outOfServiceBeds, page: '2' })
cy.task('stubOutOfServiceBedsList', { outOfServiceBeds, page: '9' })
cy.task('stubOutOfServiceBedsList', { outOfServiceBeds, page: 1 })
cy.task('stubOutOfServiceBedsList', { outOfServiceBeds, page: 2 })
cy.task('stubOutOfServiceBedsList', { outOfServiceBeds, page: 9 })

// When I visit the OOS beds index page
const page = OutOfServiceBedIndexPage.visit('current')
Expand All @@ -288,15 +336,15 @@ context('OutOfServiceBeds', () => {
page.clickNext()

// Then the API should have received a request for the next page
cy.task('verifyOutOfServiceBedsDashboard', { page: '2' }).then(requests => {
cy.task('verifyOutOfServiceBedsDashboard', { page: 2, temporality: 'current' }).then(requests => {
expect(requests).to.have.length(1)
})

// When I click on a page number
page.clickPageNumber('9')

// Then the API should have received a request for the that page number
cy.task('verifyOutOfServiceBedsDashboard', { page: '9' }).then(requests => {
cy.task('verifyOutOfServiceBedsDashboard', { page: 9, temporality: 'current' }).then(requests => {
expect(requests).to.have.length(1)
})
})
Expand All @@ -307,17 +355,17 @@ context('OutOfServiceBeds', () => {

cy.task('stubOutOfServiceBedsList', {
outOfServiceBeds,
page: '1',
page: 1,
temporality: 'current',
})
cy.task('stubOutOfServiceBedsList', {
outOfServiceBeds: futureBeds,
page: '1',
page: 1,
temporality: 'future',
})
cy.task('stubOutOfServiceBedsList', {
outOfServiceBeds: historicBeds,
page: '1',
page: 1,
temporality: 'historic',
})

Expand Down Expand Up @@ -373,14 +421,14 @@ context('OutOfServiceBeds', () => {
cy.task('stubOutOfServiceBedsList', { outOfServiceBeds, page: 1 })
cy.task('stubOutOfServiceBedsList', {
outOfServiceBeds,
page: '1',
page: 1,
sortBy: field,
sortDirection: 'asc',
temporality: 'current',
})
cy.task('stubOutOfServiceBedsList', {
outOfServiceBeds,
page: '1',
page: 1,
sortBy: field,
sortDirection: 'desc',
temporality: 'current',
Expand All @@ -396,8 +444,12 @@ context('OutOfServiceBeds', () => {
page.clickSortBy(field)

// Then the API should have received a request for the sort
cy.task('verifyOutOfServiceBedsDashboard', { page: '1', sortBy: field, sortDirection: 'asc' }).then(requests => {
expect(requests).to.have.length(field === 'startDate' ? 2 : 1)
cy.task('verifyOutOfServiceBedsDashboard', {
page: 1,
sortBy: field,
temporality: 'current',
}).then(requests => {
expect(requests).to.have.length(1)
})

// And the page should show the sorted items
Expand All @@ -407,7 +459,12 @@ context('OutOfServiceBeds', () => {
page.clickSortBy(field)

// Then the API should have received a request for the sort
cy.task('verifyOutOfServiceBedsDashboard', { page: '1', sortBy: field, sortDirection: 'desc' }).then(requests => {
cy.task('verifyOutOfServiceBedsDashboard', {
page: 1,
sortBy: field,
sortDirection: 'desc',
temporality: 'current',
}).then(requests => {
expect(requests).to.have.length(1)
})

Expand Down
Loading