mirror of
https://github.com/Unleash/unleash.git
synced 2025-09-24 17:51:14 +02:00
extract and add test
This commit is contained in:
parent
7f83ea730f
commit
3cce9652df
@ -0,0 +1,17 @@
|
||||
import { calculateRatio } from './calculate-ratio.ts';
|
||||
|
||||
test('A ratio of anything to 0 is 100', () => {
|
||||
expect(calculateRatio(0, 0)).toBe(100);
|
||||
expect(calculateRatio(5, 0)).toBe(100);
|
||||
});
|
||||
|
||||
test('Normal ratios work as expected', () => {
|
||||
expect(calculateRatio(0, 1)).toBe(0);
|
||||
expect(calculateRatio(1, 1)).toBe(100);
|
||||
expect(calculateRatio(1, 2)).toBe(50);
|
||||
expect(calculateRatio(5, 2)).toBe(250);
|
||||
});
|
||||
|
||||
test('Numbers are rounded to the nearest integer', () => {
|
||||
expect(calculateRatio(5, 9)).toBe(56);
|
||||
});
|
@ -0,0 +1,12 @@
|
||||
export const calculateRatio = (
|
||||
antecedent: number,
|
||||
consequent: number,
|
||||
): number => {
|
||||
const rawRatio = Math.round((antecedent / consequent) * 100);
|
||||
|
||||
if (Number.isNaN(rawRatio) || rawRatio === Number.POSITIVE_INFINITY) {
|
||||
return 100;
|
||||
}
|
||||
|
||||
return rawRatio;
|
||||
};
|
@ -3,6 +3,7 @@ import { Paper, Typography, styled } from '@mui/material';
|
||||
import type { TooltipState } from 'component/insights/components/LineChart/ChartTooltip/ChartTooltip';
|
||||
import { ChartTooltipContainer } from 'component/insights/components/LineChart/ChartTooltip/ChartTooltip';
|
||||
import type { WeekData } from './types.ts';
|
||||
import { calculateRatio } from 'component/insights/calculate-ratio/calculate-ratio.ts';
|
||||
|
||||
const StyledTooltipItemContainer = styled(Paper)(({ theme }) => ({
|
||||
padding: theme.spacing(2),
|
||||
@ -50,8 +51,7 @@ export const CreationArchiveRatioTooltip: FC<
|
||||
const archivedCount = rawData.archivedFlags || 0;
|
||||
const createdCount = rawData.totalCreatedFlags || 0;
|
||||
|
||||
const rawRatio = Math.round((archivedCount / createdCount) * 100);
|
||||
const ratio = Number.isNaN(rawRatio) ? 100 : Math.min(rawRatio, 100);
|
||||
const ratio = calculateRatio(archivedCount, createdCount);
|
||||
|
||||
return (
|
||||
<ChartTooltipContainer tooltip={tooltip}>
|
||||
|
@ -7,6 +7,7 @@ import { StatsExplanation } from 'component/insights/InsightsCharts.styles';
|
||||
import type { GroupedDataByProject } from 'component/insights/hooks/useGroupedProjectTrends';
|
||||
import type { InstanceInsightsSchema } from 'openapi';
|
||||
import { Link } from 'react-router-dom';
|
||||
import { calculateRatio } from 'component/insights/calculate-ratio/calculate-ratio';
|
||||
|
||||
function getCurrentArchiveRatio(
|
||||
groupedCreationArchiveData: GroupedDataByProject<
|
||||
@ -34,8 +35,7 @@ function getCurrentArchiveRatio(
|
||||
}
|
||||
});
|
||||
|
||||
const rawRatio = Math.round((totalArchived / totalCreated) * 100);
|
||||
return Number.isNaN(rawRatio) ? 100 : Math.min(rawRatio, 100);
|
||||
return calculateRatio(totalArchived, totalCreated);
|
||||
}
|
||||
|
||||
const StyledRatioContainer = styled(Box)(({ theme }) => ({
|
||||
|
Loading…
Reference in New Issue
Block a user