Skip to content

Commit

Permalink
Added component tests for ExportWins/Details
Browse files Browse the repository at this point in the history
  • Loading branch information
peterhudec committed Feb 28, 2024
1 parent 3b34ae1 commit 74ef749
Show file tree
Hide file tree
Showing 5 changed files with 465 additions and 120 deletions.
239 changes: 120 additions & 119 deletions src/client/modules/ExportWins/Details.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/* eslint-disable prettier/prettier */
import React from 'react'
import { Route } from 'react-router-dom'
import { Link } from 'govuk-react'
import { Link as ReactRouterLink } from 'react-router-dom/cjs/react-router-dom'
import styled from 'styled-components'
import { SPACING } from '@govuk-react/constants'
import pluralize from 'pluralize'

import { DefaultLayout, SummaryTable } from '../../components'
import urls from '../../../lib/urls'
Expand All @@ -29,131 +28,133 @@ const ExportWinTitle = (props) => (
<ExportWin.Inline {...props}>
{(exportWin) => (
<>
{exportWin.name_of_export} to {exportWin.country?.name}
{exportWin.nameOfExport} to {exportWin.country?.name}
</>
)}
</ExportWin.Inline>
)

const Detail = () => (
<Route>
{({
match: {
params: { winId },
},
}) => (
<DefaultLayout
heading={<ExportWinTitle id={winId} />}
pageTitle={<ExportWinTitle id={winId} />}
breadcrumbs={[
{
link: urls.dashboard.index(),
text: 'Home',
const groupBreakdowns = (breakdowns) => {
const result =
breakdowns.reduce(
({ groups: groups, totalAmount, yearRange }, breakdown) => {
const group = groups[breakdown.type.name]

return {
totalAmount: totalAmount + breakdown.value,
yearRange: {
min: Math.min(yearRange.min, breakdown.year),
max: Math.max(yearRange.max, breakdown.year),
},
{
link: urls.companies.exportWins.index(),
text: 'Export wins',
groups: {
...groups,
[breakdown.type.name]: {
yearRange: {
min: Math.min(group?.yearRange.min || Infinity, breakdown.year),
max: Math.max(group?.yearRange.min || 0, breakdown.year),
},
total: (group?.total || 0) + breakdown.value,
},
},
{ text: <ExportWinTitle id={winId} /> },
]}
>
<ExportWin id={winId} progressBox={true}>
{(exportWin) => {
// TODO: Same / similar computation is used in the add export win form so it should be reused
const {
groupedBreakdowns = [],
totalAmount,
yearRange,
totalYears = yearRange?.max - yearRange?.min + 1,
} = exportWin?.breakdowns.reduce(
({ groupedBreakdowns, totalAmount, yearRange }, x) => ({
totalAmount: totalAmount + x.value,
yearRange: {
min: Math.min(yearRange.min, x.year),
max: Math.max(yearRange.max, x.year),
},
groupedBreakdowns: {
...groupedBreakdowns,
[x.type.name]: {
yearCount:
(groupedBreakdowns[x.type.name]?.yearCount || 0) + 1,
total:
(groupedBreakdowns[x.type.name]?.total || 0) + x.value,
},
},
}),
{
groupedBreakdowns: {},
totalAmount: 0,
yearRange: { min: Infinity, max: 0 },
}
) || {}
}
},
{
groups: {},
totalAmount: 0,
yearRange: { min: Infinity, max: 0 },
}
) || {}

return (
<>
<SummaryTable>
<SummaryTable.Row heading="Goods or services">
{exportWin?.goodsVsServices.name}
</SummaryTable.Row>
<SummaryTable.Row heading="Destination country">
{exportWin?.country.name}
</SummaryTable.Row>
{exportWin &&
Object.keys(groupedBreakdowns).length > 1 &&
Object.entries(groupedBreakdowns).map(
([k, { total, yearCount }]) => (
<NormalFontWeightRow key={k} heading={k}>
{`${currencyGBP(total)} over ${yearCount} years`}
</NormalFontWeightRow>
)
)}
<SummaryTable.Row heading="Total value">
{exportWin &&
`${currencyGBP(totalAmount)} over ${totalYears} years`}
</SummaryTable.Row>
<SummaryTable.Row heading="Date won">
{exportWin && formatMediumDate(exportWin.date)}
</SummaryTable.Row>
<SummaryTable.Row heading="Lead officer name">
{exportWin?.leadOfficer.name}
</SummaryTable.Row>
<SummaryTable.Row heading="Comments">
{exportWin?.customerResponse?.comments}
</SummaryTable.Row>
<SummaryTable.Row heading="Export win confirmed">
{exportWin &&
(exportWin.isPersonallyConfirmed ? 'Yes' : 'No')}
</SummaryTable.Row>
{exportWin?.isPersonallyConfirmed &&
exportWin?.breakdowns.length === 0 && (
<SummaryTable.Row heading="What value do you estimate you would have achieved without our support?">
???
</SummaryTable.Row>
)}
</SummaryTable>
<VerticalSpacer>
{exportWin?.isLineManagerConfirmed && (
<Link
as={ReactRouterLink}
to={urls.companies.exportWins.customerFeedback(winId)}
>
View customer feedback
</Link>
)}
<Link
as={ReactRouterLink}
to={urls.companies.exportWins.index()}
>
Export wins
</Link>
</VerticalSpacer>
</>
)
}}
</ExportWin>
</DefaultLayout>
)}
</Route>
return {
...result,
totalYears: result.yearRange.max - result.yearRange.min + 1,
}
}

const Detail = ({
match: {
params: { winId },
},
}) => (
<DefaultLayout
heading={<ExportWinTitle id={winId} />}
pageTitle={<ExportWinTitle id={winId} />}
breadcrumbs={[
{
link: urls.dashboard.index(),
text: 'Home',
},
{
link: urls.companies.exportWins.index(),
text: 'Export wins',
},
{ text: <ExportWinTitle id={winId} /> },
]}
>
<ExportWin id={winId} progressBox={true}>
{(exportWin) => {
const { groups, totalAmount, totalYears } = exportWin
? groupBreakdowns(exportWin.breakdowns)
: {}

return (
<>
<SummaryTable data-test="export-wins-details-table">
<SummaryTable.Row heading="Goods or services">
{exportWin?.goodsVsServices.name}
</SummaryTable.Row>
<SummaryTable.Row heading="Destination country">
{exportWin?.country.name}
</SummaryTable.Row>
{exportWin &&
Object.keys(groups).length > 1 &&
Object.entries(groups).map(([k, { total, yearRange }]) => {
const years = yearRange.max - yearRange.min + 1
return (
<NormalFontWeightRow key={k} heading={k}>
{`${currencyGBP(total)} over ${years} ${pluralize('year', years)}`}
</NormalFontWeightRow>
)
})}
<SummaryTable.Row heading="Total value">
{exportWin &&
`${currencyGBP(totalAmount)} over ${totalYears} ${pluralize('year', totalYears)}`}
</SummaryTable.Row>
<SummaryTable.Row heading="Date won">
{exportWin && formatMediumDate(exportWin.date)}
</SummaryTable.Row>
<SummaryTable.Row heading="Lead officer name">
{exportWin?.leadOfficer.name}
</SummaryTable.Row>
<SummaryTable.Row heading="Comments">
{exportWin?.comments}
</SummaryTable.Row>
<SummaryTable.Row heading="Export win confirmed">
{exportWin && (exportWin.isPersonallyConfirmed ? 'Yes' : 'No')}
</SummaryTable.Row>
</SummaryTable>
<VerticalSpacer>
{exportWin?.isPersonallyConfirmed && (
<Link
as={ReactRouterLink}
to={urls.companies.exportWins.customerFeedback(winId)}
>
View customer feedback
</Link>
)}
<Link
data-test="export-wins-link"
as={ReactRouterLink}
to={urls.companies.exportWins.index()}
>
Export wins
</Link>
</VerticalSpacer>
</>
)
}}
</ExportWin>
</DefaultLayout>
)

export default Detail
Loading

0 comments on commit 74ef749

Please sign in to comment.