Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { DriverRatingBadge } from './DriverRatingBadge';
describe('DriverRatingBadge', () => {
it('renders with default props', () => {
const { container } = render(<DriverRatingBadge />);
expect(container.textContent).toBe('R 0.0k');
expect(container.textContent).toBe('R 0.0 0.0k');
});

it('renders with license A and rating 5000', () => {
Expand Down Expand Up @@ -54,27 +54,41 @@ describe('DriverRatingBadge', () => {
const { container } = render(
<DriverRatingBadge license={undefined} rating={undefined} />
);
expect(container.textContent).toBe('R 0.0k');
expect(container.textContent).toBe('R 0.0 0.0k');
});

it('rounds rating to 1 decimal place', () => {
const { container } = render(
<DriverRatingBadge license="C 3.141592654" rating={5000.123} />
);
expect(container.textContent).toBe('C 3.1 5.0k');
});

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

it('keeps single zero before decimal point', () => {
const { container } = render(
<DriverRatingBadge license="A 0.99" rating={5000} />
);
expect(container.textContent).toBe('A 0.99 5.0k');
expect(container.textContent).toBe('A 1.0 5.0k');
});

it('handles multiple leading zeros', () => {
const { container } = render(
<DriverRatingBadge license="B 0003.45" rating={5000} />
);
expect(container.textContent).toBe('B 3.45 5.0k');
expect(container.textContent).toBe('B 3.5 5.0k');
});

it('should handle invalid license strings', () => {
const { container } = render(
<DriverRatingBadge license="Oh no" rating={5000} />
);
expect(container.textContent).toBe('Oh no 5.0k');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export interface DriverRatingBadgeProps {
}

export const DriverRatingBadge = ({
license = 'R',
license = 'R 0.0',
rating = 0,
}: DriverRatingBadgeProps) => {
const licenseLevel = license?.charAt(0) || 'R';
Expand All @@ -20,8 +20,11 @@ export const DriverRatingBadge = ({
if (rating >= 10000) fixed = 0;
const simplifiedRating = (rating / 1000).toFixed(fixed);

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

return (
<div
Expand Down