From e3c2bf4f29b03cfbd924c9196121b791139df6f7 Mon Sep 17 00:00:00 2001 From: Bob Meredith Date: Thu, 16 Jan 2025 10:00:47 +0000 Subject: [PATCH] More unit tests --- server/utils/premises/occupancy.test.ts | 66 ++++++++++++++++++++++++- server/utils/premises/occupancy.ts | 8 ++- 2 files changed, 67 insertions(+), 7 deletions(-) diff --git a/server/utils/premises/occupancy.test.ts b/server/utils/premises/occupancy.test.ts index e48423d30..b9d673cd3 100644 --- a/server/utils/premises/occupancy.test.ts +++ b/server/utils/premises/occupancy.test.ts @@ -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', () => { @@ -48,4 +55,59 @@ describe('apOccupancy utils', () => { expect(durationSelectOptions('27')).toEqual(durationOptions) }) }) + + describe('generateDaySummaryText', () => { + const buildAvailability = (overbookedCharacteristics: Array) => + 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, + }) + }) + }) }) diff --git a/server/utils/premises/occupancy.ts b/server/utils/premises/occupancy.ts index a92e006d4..fcf49cfe6 100644 --- a/server/utils/premises/occupancy.ts +++ b/server/utils/premises/occupancy.ts @@ -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)), ], } }