Skip to content

Commit

Permalink
Merge pull request #2345 from IntersectMBO/fix/2305--ada-quantities-f…
Browse files Browse the repository at this point in the history
…ormat-incorrect-on-governance-actions

fix(#2305): change displaying ada to en-US format in votes
  • Loading branch information
MSzalowski authored Nov 7, 2024
2 parents 2c8787b + fa2a33e commit 7d714e3
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ changes.
- Change multilanguage support to use json file [Issue 2325](https://github.com/IntersectMBO/govtool/issues/2325)
- Display full Governance Action IDs on desktop
- Support space on given name [Issue 2276](https://github.com/IntersectMBO/govtool/issues/2276)
- Display ADA in 'en-US' format [Issue 2305](https://github.com/IntersectMBO/govtool/issues/2305)

### Removed

Expand Down
6 changes: 4 additions & 2 deletions govtool/frontend/src/components/molecules/VotesSubmitted.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Box } from "@mui/material";
import { IMAGES } from "@consts";
import { Typography, VotePill } from "@atoms";
import { useTranslation } from "@hooks";
import { correctAdaFormat } from "@utils";
import { correctVoteAdaFormat } from "@utils";
import { SubmittedVotesData } from "@models";

type Props = {
Expand Down Expand Up @@ -148,9 +148,11 @@ const Vote = ({ type, vote, value }: VoteProps) => (
sx={{
fontSize: 16,
wordBreak: "break-all",
lineHeight: "24px",
fontWeight: "500",
}}
>
{type !== "ccCommittee" ? `₳ ${correctAdaFormat(value)}` : value}
{type !== "ccCommittee" ? `₳ ${correctVoteAdaFormat(value)}` : value}
</Typography>
</Box>
);
14 changes: 14 additions & 0 deletions govtool/frontend/src/utils/adaFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ export const correctAdaFormat = (lovelace: number | undefined) => {
return 0;
};

export const correctVoteAdaFormat = (
lovelace: number | undefined,
locale: string | undefined = undefined,
) => {
if (lovelace) {
const ada = lovelace / LOVELACE;
return ada.toLocaleString(locale, {
minimumFractionDigits: 3,
maximumFractionDigits: 3,
});
}
return "0,000";
};

export const correctDRepDirectoryFormat = (lovelace: number | undefined) => {
if (lovelace) {
return Number((lovelace / LOVELACE).toFixed(0))?.toLocaleString("en-US");
Expand Down
52 changes: 50 additions & 2 deletions govtool/frontend/src/utils/tests/adaFormat.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { correctAdaFormat, correctDRepDirectoryFormat } from "..";
import { vi, beforeAll, afterAll } from "vitest";

import {
correctAdaFormat,
correctVoteAdaFormat,
correctDRepDirectoryFormat,
} from "..";

describe("correctAdaFormat", () => {
const LOVELACE = 1000000;
const DECIMALS = 6;

it("converts lovelace to ada for a given number", () => {
const lovelace = 15000000;
const expectedAda = 15;
Expand Down Expand Up @@ -35,6 +40,49 @@ describe("correctAdaFormat", () => {
});
});

describe("correctVoteAdaFormat", () => {
beforeAll(() => {
vi.spyOn(Intl, "NumberFormat").mockImplementation(
((_locales?: string | string[], options?: Intl.NumberFormatOptions) =>
new Intl.NumberFormat(
"en-US",
options,
)) as unknown as typeof Intl.NumberFormat,
);
});

afterAll(() => {
vi.restoreAllMocks();
});
test("Correctly formats lovelace value to ada format", () => {
const lovelace = 123456789012345;
const expectedResult = "123,456,789.012";

const result = correctVoteAdaFormat(lovelace, "en-US");

// Normalize spaces in both result and expectedResult for comparison
expect(result.replace(/\s/g, " ")).toBe(expectedResult);
});

test("Returns 0 for undefined lovelace value", () => {
const lovelace = undefined;
const expectedResult = "0,000";
expect(correctVoteAdaFormat(lovelace, "en-US")).toBe(expectedResult);
});

test("Returns 0 for zero lovelace value", () => {
const lovelace = 0;
const expectedResult = "0,000";
expect(correctVoteAdaFormat(lovelace, "en-US")).toBe(expectedResult);
});

test("Returns 0 for small lovelace value", () => {
const lovelace = 123;
const expectedResult = "0.000";
expect(correctVoteAdaFormat(lovelace, "en-US")).toBe(expectedResult);
});
});

describe("correctDRepDirectoryFormat", () => {
test("Correctly formats lovelace value to directory format", () => {
const lovelace = 143500000000;
Expand Down

0 comments on commit 7d714e3

Please sign in to comment.