Skip to content

Commit 27a9157

Browse files
authored
feat: round driver licence rating (#35)
1 parent a4661aa commit 27a9157

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

src/frontend/components/Standings/components/DriverRatingBadge/DriverRatingBadge.spec.tsx

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { DriverRatingBadge } from './DriverRatingBadge';
55
describe('DriverRatingBadge', () => {
66
it('renders with default props', () => {
77
const { container } = render(<DriverRatingBadge />);
8-
expect(container.textContent).toBe('R 0.0k');
8+
expect(container.textContent).toBe('R 0.0 0.0k');
99
});
1010

1111
it('renders with license A and rating 5000', () => {
@@ -54,27 +54,41 @@ describe('DriverRatingBadge', () => {
5454
const { container } = render(
5555
<DriverRatingBadge license={undefined} rating={undefined} />
5656
);
57-
expect(container.textContent).toBe('R 0.0k');
57+
expect(container.textContent).toBe('R 0.0 0.0k');
58+
});
59+
60+
it('rounds rating to 1 decimal place', () => {
61+
const { container } = render(
62+
<DriverRatingBadge license="C 3.141592654" rating={5000.123} />
63+
);
64+
expect(container.textContent).toBe('C 3.1 5.0k');
5865
});
5966

6067
it('removes leading zeros from license number when before non-zero digit', () => {
6168
const { container } = render(
6269
<DriverRatingBadge license="A 02.99" rating={5000} />
6370
);
64-
expect(container.textContent).toBe('A 2.99 5.0k');
71+
expect(container.textContent).toBe('A 3.0 5.0k');
6572
});
6673

6774
it('keeps single zero before decimal point', () => {
6875
const { container } = render(
6976
<DriverRatingBadge license="A 0.99" rating={5000} />
7077
);
71-
expect(container.textContent).toBe('A 0.99 5.0k');
78+
expect(container.textContent).toBe('A 1.0 5.0k');
7279
});
7380

7481
it('handles multiple leading zeros', () => {
7582
const { container } = render(
7683
<DriverRatingBadge license="B 0003.45" rating={5000} />
7784
);
78-
expect(container.textContent).toBe('B 3.45 5.0k');
85+
expect(container.textContent).toBe('B 3.5 5.0k');
86+
});
87+
88+
it('should handle invalid license strings', () => {
89+
const { container } = render(
90+
<DriverRatingBadge license="Oh no" rating={5000} />
91+
);
92+
expect(container.textContent).toBe('Oh no 5.0k');
7993
});
8094
});

src/frontend/components/Standings/components/DriverRatingBadge/DriverRatingBadge.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export interface DriverRatingBadgeProps {
44
}
55

66
export const DriverRatingBadge = ({
7-
license = 'R',
7+
license = 'R 0.0',
88
rating = 0,
99
}: DriverRatingBadgeProps) => {
1010
const licenseLevel = license?.charAt(0) || 'R';
@@ -20,8 +20,11 @@ export const DriverRatingBadge = ({
2020
if (rating >= 10000) fixed = 0;
2121
const simplifiedRating = (rating / 1000).toFixed(fixed);
2222

23-
// Format the license string to remove leading zeros only when they're before a non-zero digit
24-
const formattedLicense = license?.replace(/([A-Z]) 0+([1-9]\d*\.\d+)/, '$1 $2') || 'R';
23+
// Format the license string to show license level and single decimal rating
24+
const formattedLicense = license?.replace(/([A-Z])\s*(\d+)\.(\d+)/, (_, level, whole, decimal) => {
25+
const parsedRating = parseFloat(`${whole}.${decimal}`);
26+
return `${level} ${parsedRating.toFixed(1)}`;
27+
}) || 'R 0.0';
2528

2629
return (
2730
<div

0 commit comments

Comments
 (0)