Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions public/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,13 @@
"(Quarter)": "(Quarter)",
"(Sent by {{firstName}} {{lastName}})": "(Sent by {{firstName}} {{lastName}})",
"(Year)": "(Year)",
"Averages will show after your first full month.": "Averages will show after your first full month.",
"Avg. difference": "Avg. difference",
"Dismiss": "Dismiss",
"Monthly averages for the last {{count}} full months ({{range}}). {{month}} is still in progress._one": "Monthly average for the last full month ({{range}}). {{month}} is still in progress.",
"Monthly averages for the last {{count}} full months ({{range}}). {{month}} is still in progress._other": "Monthly averages for the last {{count}} full months ({{range}}). {{month}} is still in progress.",
"Monthly averages for the {{count}} full months on record ({{range}}). {{month}} is still in progress._one": "Monthly average for the {{count}} full month on record ({{range}}). {{month}} is still in progress.",
"Monthly averages for the {{count}} full months on record ({{range}}). {{month}} is still in progress._other": "Monthly averages for the {{count}} full months on record ({{range}}). {{month}} is still in progress.",
"Review your goal, see how to present it to partners, and read your next steps. We also emailed you the full worksheet as a PDF.": "Review your goal, see how to present it to partners, and read your next steps. We also emailed you the full worksheet as a PDF.",
"View my MPD goal": "View my MPD goal",
"Your goal changed since you last looked. Review the updated worksheet, which was also emailed to you as a PDF.": "Your goal changed since you last looked. Review the updated worksheet, which was also emailed to you as a PDF.",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { ThemeProvider } from '@mui/material/styles';
import { render } from '__tests__/util/testingLibraryReactMock';
import theme from 'src/theme';
import { AverageKey } from './AverageKey';
import { IncomeExpensesAverages } from './IncomeExpensesChart';

interface TestComponentProps {
averages?: IncomeExpensesAverages;
note?: string;
}

const TestComponent: React.FC<TestComponentProps> = ({
averages = { income: 56150 / 11, expenses: 53450 / 11 },
note = 'Monthly averages for the last 11 full months.',
}) => (
<ThemeProvider theme={theme}>
<AverageKey averages={averages} note={note} />
</ThemeProvider>
);

describe('AverageKey', () => {
it('lists each series with its average, then the difference', () => {
const { getByTestId } = render(<TestComponent />);

const key = getByTestId('MonthlySummaryAverageKey');

expect(Array.from(key.children).map((item) => item.textContent)).toEqual([
'Contributionsavg $5,104.55',
'Expensesavg $4,859.09',
'Avg. difference+$245.46',
]);
});

it('shows the amounts at 14px, weight 600', () => {
const { getByText } = render(<TestComponent />);

['$5,104.55', '$4,859.09', '+$245.46'].forEach((amount) =>
expect(getByText(amount)).toHaveStyle({
fontSize: '14px',
fontWeight: 600,
}),
);
});

it('signs a negative difference', () => {
const { getByText } = render(
<TestComponent averages={{ income: 4000, expenses: 4100 }} />,
);

expect(getByText('-$100.00')).toBeInTheDocument();
});

it('shows no sign on a zero difference', () => {
const { getByText } = render(
<TestComponent averages={{ income: 4000, expenses: 4000 }} />,
);

expect(getByText('$0.00').previousSibling).toHaveTextContent(
'Avg. difference',
);
});

it('draws a bar swatch and a line swatch in the same pattern as its line', () => {
const { getByTestId } = render(<TestComponent />);

const key = getByTestId('MonthlySummaryAverageKey');

expect(
Array.from(key.querySelectorAll('svg[aria-hidden="true"] rect')).map(
(rect) => rect.getAttribute('fill'),
),
).toEqual(['#43A047', '#B71C1C']);
expect(
Array.from(
key.querySelectorAll('svg[aria-hidden="true"] line[stroke-width="3"]'),
).map((line) => [
line.getAttribute('stroke'),
line.getAttribute('stroke-dasharray'),
]),
).toEqual([
['#1B5E20', '12 6'],
['#B71C1C', '3 4'],
]);
});

it('shows the note under the key in small gray text', () => {
const { getByText, getByTestId } = render(<TestComponent />);

const note = getByText('Monthly averages for the last 11 full months.');

expect(
getByTestId('MonthlySummaryAverageKey').compareDocumentPosition(note) &
Node.DOCUMENT_POSITION_FOLLOWING,
).toBeTruthy();
expect(note).toHaveStyle({ fontSize: '13px', color: '#757575' });
});

it('lists the series without averages or a difference when there are no averages', () => {
const { getByTestId, getByText } = render(
<TestComponent
averages={{}}
note="Averages will show after your first full month."
/>,
);

expect(
Array.from(getByTestId('MonthlySummaryAverageKey').children).map(
(item) => item.textContent,
),
).toEqual(['Contributions', 'Expenses']);
expect(
getByText('Averages will show after your first full month.'),
).toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { Box, Typography } from '@mui/material';
import { useTranslation } from 'react-i18next';
import { useFormatters } from 'src/components/HrTools/Shared/useFormatters';
import theme from 'src/theme';
import { IncomeExpensesAverages } from './IncomeExpensesChart';
import { AverageLine, AverageSeries, averageSeries } from './averageLines';

const Amount: React.FC<{ children: string }> = ({ children }) => (
<Box component="span" sx={{ fontSize: 14, fontWeight: 600 }}>
{children}
</Box>
);

interface SeriesKeyProps {
series: AverageSeries;
}

// The bars' swatch and name, then the line's swatch and the average
const SeriesKey: React.FC<SeriesKeyProps> = ({ series }) => {
const { t } = useTranslation();
const { formatCurrency } = useFormatters();

return (
<Box
display="flex"
justifyContent="space-between"
alignItems="center"
gap={2}
>
<Box display="flex" alignItems="center" gap={1}>
<svg aria-hidden="true" width={14} height={14}>
<rect width={14} height={14} rx={2} fill={series.barColor} />
</svg>
<Typography variant="body2">{series.name}</Typography>
</Box>
{series.value !== undefined && (
<Box display="flex" alignItems="center" gap={1}>
<svg aria-hidden="true" width={28} height={8}>
<AverageLine x1={0} x2={28} y1={4} series={series} start={0} />
</svg>
<Typography variant="body2">
{t('avg')} <Amount>{formatCurrency(series.value)}</Amount>
</Typography>
</Box>
)}
</Box>
);
};

// Each average is rounded to cents first, so the difference matches the two
// amounts shown
const averageDifference = ({ income, expenses }: IncomeExpensesAverages) =>
income === undefined || expenses === undefined
? undefined
: (Math.round(income * 100) - Math.round(expenses * 100)) / 100;

interface AverageKeyProps {
averages: IncomeExpensesAverages;
// Says which months the averages cover
note: string;
}

// One row on wider screens. On a phone each series gets its own row, name on
// the left and average on the right.
export const AverageKey: React.FC<AverageKeyProps> = ({ averages, note }) => {
const { t } = useTranslation();
const { formatCurrency } = useFormatters();
const difference = averageDifference(averages);

return (
<Box mb={1}>
<Box
data-testid="MonthlySummaryAverageKey"
sx={{
display: 'flex',
flexDirection: { xs: 'column', sm: 'row' },
flexWrap: 'wrap',
alignItems: { sm: 'center' },
columnGap: 4,
rowGap: 1,
}}
>
{averageSeries(t, averages).map((series) => (
<SeriesKey key={series.key} series={series} />
))}
{difference !== undefined && (
<Box
sx={{
display: 'flex',
justifyContent: 'space-between',
gap: 2,
borderColor: 'divider',
borderLeft: { sm: 1 },
borderTop: { xs: 1, sm: 0 },
pl: { sm: 4 },
pt: { xs: 1, sm: 0 },
}}
>
<Typography variant="body2">{t('Avg. difference')}</Typography>
<Amount>{`${difference > 0 ? '+' : ''}${formatCurrency(difference)}`}</Amount>
</Box>
)}
</Box>
<Typography mt={1} sx={{ fontSize: 13, color: theme.palette.grey[600] }}>
{note}
</Typography>
</Box>
);
};
Loading
Loading