Skip to content

Commit

Permalink
More unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Bob Meredith committed Jan 16, 2025
1 parent 1b533e8 commit e3c2bf4
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 7 deletions.
66 changes: 64 additions & 2 deletions server/utils/premises/occupancy.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import type { SelectOption } from '@approved-premises/ui'
import { cas1PremiseCapacityFactory } from '../../testutils/factories'
import { durationSelectOptions, occupancyCalendar } from './occupancy'
import { type Cas1SpaceBookingCharacteristic } from '@approved-premises/api'
import {
cas1PremiseCapacityFactory,
cas1PremiseCapacityForDayFactory,
cas1PremisesDaySummaryFactory,
} from '../../testutils/factories'
import { daySummaryRows, durationSelectOptions, generateDaySummaryText, occupancyCalendar } from './occupancy'
import { DateFormats } from '../dateUtils'
import { occupancyCriteriaMap } from '../match/occupancy'
import { premiseCharacteristicAvailability } from '../../testutils/factories/cas1PremiseCapacity'

describe('apOccupancy utils', () => {
describe('occupancyCalendar', () => {
Expand Down Expand Up @@ -48,4 +55,59 @@ describe('apOccupancy utils', () => {
expect(durationSelectOptions('27')).toEqual(durationOptions)
})
})

describe('generateDaySummaryText', () => {
const buildAvailability = (overbookedCharacteristics: Array<Cas1SpaceBookingCharacteristic>) =>
Object.keys(occupancyCriteriaMap).map(characteristic =>
overbookedCharacteristics.includes(characteristic as Cas1SpaceBookingCharacteristic)
? premiseCharacteristicAvailability
.overbooked()
.build({ characteristic: characteristic as Cas1SpaceBookingCharacteristic })
: premiseCharacteristicAvailability
.available()
.build({ characteristic: characteristic as Cas1SpaceBookingCharacteristic }),
)

it('should generate the text for an premises day with an overbooking on a single characteristic', () => {
const characteristicAvailability = buildAvailability(['isSingle'])
expect(generateDaySummaryText(characteristicAvailability)).toEqual(
'This AP is overbooked on spaces with the following criteria: Single room',
)
})

it('should generate the text for an premises day with an overbooking on multiple characteristics', () => {
const characteristicAvailability = buildAvailability(['isSingle', 'isArsonSuitable', 'isWheelchairDesignated'])
expect(generateDaySummaryText(characteristicAvailability)).toEqual(
'This AP is overbooked on spaces with the following criteria: Wheelchair accessible, Single room, Designated arson room',
)
})

it('should generate empty text for an premises day with no overbooked characteristics', () => {
const characteristicAvailability = buildAvailability([])
expect(generateDaySummaryText(characteristicAvailability)).toEqual('')
})
})

describe('daySummaryRows', () => {
it('should generate a list of day summary rows', () => {
const capacityForDay = cas1PremiseCapacityForDayFactory.build({
totalBedCount: 20,
availableBedCount: 18,
bookingCount: 6,
})
const daySummary = cas1PremisesDaySummaryFactory.build({
capacity: capacityForDay,
})
const expected = [
{ key: { text: 'Capacity' }, value: { text: '20' } },
{ key: { text: 'Booked spaces' }, value: { text: '6' } },
{ key: { text: 'Out of service beds' }, value: { text: '2' } },
{ key: { text: 'Available spaces' }, value: { text: '12' } },
]

expect(daySummaryRows(daySummary)).toEqual({
rows: expected,
})
})
})
})
8 changes: 3 additions & 5 deletions server/utils/premises/occupancy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,14 @@ const summaryRow = (key: string, value: string): SummaryListItem =>

export const daySummaryRows = (daySummary: Cas1PremisesDaySummary) => {
const {
capacity: { totalBedCount, bookingCount },
outOfServiceBeds,
capacity: { totalBedCount, bookingCount, availableBedCount },
} = daySummary
const outOfServiceBedCount: number = outOfServiceBeds?.length || 0
return {
rows: [
summaryRow('Capacity', String(totalBedCount)),
summaryRow('Booked spaces', String(bookingCount)),
summaryRow('Out of service beds', String(outOfServiceBedCount)),
summaryRow('Available spaces', String(totalBedCount - bookingCount - outOfServiceBedCount)),
summaryRow('Out of service beds', String(totalBedCount - availableBedCount)),
summaryRow('Available spaces', String(availableBedCount - bookingCount)),
],
}
}

0 comments on commit e3c2bf4

Please sign in to comment.