Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
solidovic committed Nov 25, 2024
1 parent 1f8ed96 commit cfedc5b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
30 changes: 15 additions & 15 deletions utils/__tests__/formatBalance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ describe('formatBalance', () => {
it('should format balance with default maxDecimalDigits', () => {
const balance = 1_000_000_000_000_000_000n;
const expected = {
actual: '1',
trimmed: '1',
actual: '1.0',
trimmed: '1.0',
isTrimmed: false,
};
const formattedBalance = formatBalance(balance);
Expand All @@ -16,8 +16,8 @@ describe('formatBalance', () => {
it('should format balance with custom maxDecimalDigits', () => {
const balance = 1_000_000_000_000_000_000n;
const expected = {
actual: '1',
trimmed: '1',
actual: '1.0',
trimmed: '1.0',
isTrimmed: false,
};
const formattedBalance = formatBalance(balance, { maxDecimalDigits: 2 });
Expand All @@ -27,7 +27,7 @@ describe('formatBalance', () => {
it('should format balance with zero maxDecimalDigits', () => {
const balance = 1_000_000_000_000_000_000n;
const expected = {
actual: '1',
actual: '1.0',
trimmed: '1',
isTrimmed: false,
};
Expand Down Expand Up @@ -66,7 +66,7 @@ describe('formatBalance', () => {
const maxDecimalDigits = 4;
const maxTotalLength = 30;
const expected = {
actual: '1000000000000000000000000010000000',
actual: '1000000000000000000000000010000000.0',
trimmed: '100000000000000000000000001...',
isTrimmed: true,
};
Expand All @@ -80,8 +80,8 @@ describe('formatBalance', () => {
const maxDecimalDigits = 4;
const maxTotalLength = 50;
const expected = {
actual: '1000000000000000000000000010000000',
trimmed: '1000000000000000000000000010000000',
actual: '1000000000000000000000000010000000.0',
trimmed: '1000000000000000000000000010000000.0',
isTrimmed: false,
};
const result = formatBalance(balance, { maxDecimalDigits, maxTotalLength });
Expand Down Expand Up @@ -117,8 +117,8 @@ describe('formatBalance', () => {
it('should format balance with default maxDecimalDigits and maxTotalLength', () => {
const balance = 1_000_000_000_000_000_000n;
const expected = {
actual: '1',
trimmed: '1',
actual: '1.0',
trimmed: '1.0',
isTrimmed: false,
};
const result = formatBalance(balance);
Expand All @@ -130,7 +130,7 @@ describe('formatBalance', () => {
const maxDecimalDigits = 2;
const maxTotalLength = 5;
const expected = {
actual: '1',
actual: '1.0',
trimmed: '1',
isTrimmed: false,
};
Expand All @@ -143,7 +143,7 @@ describe('formatBalance', () => {
const maxDecimalDigits = 0;
const maxTotalLength = 2;
const expected = {
actual: '1',
actual: '1.0',
trimmed: '...',
isTrimmed: true,
};
Expand All @@ -157,7 +157,7 @@ describe('formatBalance', () => {
const maxTotalLength = 3;
const result = formatBalance(balance, { maxDecimalDigits, maxTotalLength });
const expected = {
actual: '100000',
actual: '100000.0',
trimmed: '...',
isTrimmed: true,
};
Expand All @@ -168,8 +168,8 @@ describe('formatBalance', () => {
const balance = 0n;
const formattedBalance = formatBalance(balance);
expect(formattedBalance).toEqual({
actual: '0',
trimmed: '0',
actual: '0.0',
trimmed: '0.0',
isTrimmed: false,
});
});
Expand Down
8 changes: 7 additions & 1 deletion utils/formatBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ export const formatBalance = (
trimEllipsis,
}: FormatBalanceArgs = {},
) => {
const actual = formatEther(balance);
let actual = formatEther(balance);
// formatEther from viem returns without '.0'
if (!actual.includes('.')) {
actual += '.0';
}

let trimmed = actual;

let isTrimmed = false;

if (actual.includes('.')) {
Expand Down

0 comments on commit cfedc5b

Please sign in to comment.