generated from ministryofjustice/hmpps-template-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
StatisticsReportUtils
and associated njk macro
- Loading branch information
1 parent
23151ff
commit 1d0b938
Showing
4 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import StatisticsReportUtils from './statisticsReportUtils' | ||
import type { ReportContent } from '@accredited-programmes-api' | ||
|
||
describe('StatisticsReportUtils', () => { | ||
describe('reportContentDataBlock', () => { | ||
const reportContent: ReportContent = { | ||
content: { | ||
count: 1, | ||
courseCounts: [ | ||
{ | ||
audience: 'audience', | ||
count: 1, | ||
name: 'name', | ||
}, | ||
], | ||
}, | ||
parameters: { | ||
endDate: '2022-01-01', | ||
startDate: '2022-01-31', | ||
}, | ||
reportType: 'REFERRAL_COUNT', | ||
} | ||
it('returns the report content data block', () => { | ||
expect(StatisticsReportUtils.reportContentDataBlock(reportContent, new Date('2022-01-01'))).toEqual({ | ||
date: 'January 2022', | ||
testId: 'referral-count', | ||
title: 'Total referrals submitted', | ||
value: '1', | ||
}) | ||
}) | ||
|
||
describe('when report content is `null`', () => { | ||
it('returns the correct values', () => { | ||
expect(StatisticsReportUtils.reportContentDataBlock(null, new Date('2022-01-01'))).toEqual( | ||
expect.objectContaining({ | ||
testId: 'unknown', | ||
value: '0', | ||
}), | ||
) | ||
}) | ||
}) | ||
|
||
describe('when there is no count value', () => { | ||
it('returns the correct values', () => { | ||
reportContent.content.count = undefined | ||
expect(StatisticsReportUtils.reportContentDataBlock(reportContent, new Date('2022-01-01'))).toEqual( | ||
expect.objectContaining({ | ||
value: '0', | ||
}), | ||
) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('reportContentTitle', () => { | ||
it('returns the title for the given report type', () => { | ||
expect(StatisticsReportUtils.reportContentTitle('REFERRAL_COUNT')).toEqual('Total referrals submitted') | ||
expect(StatisticsReportUtils.reportContentTitle('PROGRAMME_COMPLETE_COUNT')).toEqual('Total programmes completed') | ||
expect(StatisticsReportUtils.reportContentTitle('NOT_ELIGIBLE_COUNT')).toEqual('Total referrals not eligible') | ||
expect(StatisticsReportUtils.reportContentTitle('WITHDRAWN_COUNT')).toEqual('Total referrals withdrawn') | ||
expect(StatisticsReportUtils.reportContentTitle('DESELECTED_COUNT')).toEqual('Total referrals deselected') | ||
}) | ||
|
||
describe('when the report type is unknown', () => { | ||
it('returns "Unknown"', () => { | ||
expect(StatisticsReportUtils.reportContentTitle('UNKNOWN')).toEqual('Unknown') | ||
}) | ||
}) | ||
|
||
describe('when the report type is undefined', () => { | ||
it('returns "Unknown"', () => { | ||
expect(StatisticsReportUtils.reportContentTitle(undefined)).toEqual('Unknown') | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import type { ReportContent } from '@accredited-programmes-api' | ||
|
||
interface ReportContentDataBlock { | ||
date: string | ||
testId: string | ||
title: string | ||
value: string | ||
} | ||
|
||
export default class StatisticsReportUtils { | ||
static reportContentDataBlock(reportContent: ReportContent | null, date: Date): ReportContentDataBlock { | ||
const monthAndYear = date.toLocaleDateString('en-GB', { month: 'long', year: 'numeric' }) | ||
|
||
return { | ||
date: monthAndYear, | ||
testId: reportContent?.reportType.replace(/_/g, '-').toLowerCase() || 'unknown', | ||
title: this.reportContentTitle(reportContent?.reportType), | ||
value: reportContent?.content.count?.toString() || '0', | ||
} | ||
} | ||
|
||
static reportContentTitle(reportType?: ReportContent['reportType']): string { | ||
switch (reportType) { | ||
case 'REFERRAL_COUNT': | ||
return 'Total referrals submitted' | ||
case 'PROGRAMME_COMPLETE_COUNT': | ||
return 'Total programmes completed' | ||
case 'NOT_ELIGIBLE_COUNT': | ||
return 'Total referrals not eligible' | ||
case 'WITHDRAWN_COUNT': | ||
return 'Total referrals withdrawn' | ||
case 'DESELECTED_COUNT': | ||
return 'Total referrals deselected' | ||
default: | ||
return 'Unknown' | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{% macro reportDataBlock(data) %} | ||
<div data-testid={{ data.testId }}> | ||
<h3 class="govuk-heading-s">{{ data.title }}</h3> | ||
<p class="govuk-body"> | ||
<span class="govuk-heading-xl govuk-!-display-inline">{{ data.value }}</span> in {{ data.date }} | ||
</p> | ||
</div> | ||
{% endmacro %} |