Skip to content

Commit

Permalink
chore: Adds test coverage and fixes for negative value formatters (#1…
Browse files Browse the repository at this point in the history
…22272)

* Adds tests for negative value formatters

* Adds tests for negative duration formatters
  • Loading branch information
claudiopro authored Jan 24, 2022
1 parent a6853ec commit fd340b9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('duration formatters', () => {
[1000, '1,000.0 tpm'],
[1000000, '1,000,000.0 tpm'],
])(
'displays the correct label when the number is integer and has zero decimals',
'displays the correct label when the number is a positive integer and has zero decimals',
(value, formattedValue) => {
expect(asTransactionRate(value)).toBe(formattedValue);
}
Expand All @@ -89,12 +89,39 @@ describe('duration formatters', () => {
expect(asTransactionRate(value)).toBe(formattedValue);
}
);

it.each([
[-1, '< 0.1 tpm'],
[-10, '< 0.1 tpm'],
[-100, '< 0.1 tpm'],
[-1000, '< 0.1 tpm'],
[-1000000, '< 0.1 tpm'],
])(
'displays the correct label when the number is a negative integer and has zero decimals',
(value, formattedValue) => {
expect(asTransactionRate(value)).toBe(formattedValue);
}
);

it.each([
[-1.23, '< 0.1 tpm'],
[-12.34, '< 0.1 tpm'],
[-123.45, '< 0.1 tpm'],
[-1234.56, '< 0.1 tpm'],
[-1234567.89, '< 0.1 tpm'],
])(
'displays the correct label when the number is negative and has decimal part',
(value, formattedValue) => {
expect(asTransactionRate(value)).toBe(formattedValue);
}
);
});

describe('asMilliseconds', () => {
it('converts to formatted decimal milliseconds', () => {
expect(asMillisecondDuration(0)).toEqual('0 ms');
});

it('formats correctly with undefined values', () => {
expect(asMillisecondDuration(undefined)).toEqual('N/A');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,29 @@ describe('formatters', () => {
])('formats as decimal when number is below 10 ', (value, formattedValue) => {
expect(asDecimalOrInteger(value)).toBe(formattedValue);
});

it.each([
[-0.123, '-0.1'],
[-1.234, '-1.2'],
[-9.876, '-9.9'],
])(
'formats as decimal when number is negative and below 10 in absolute value',
(value, formattedValue) => {
expect(asDecimalOrInteger(value)).toEqual(formattedValue);
}
);

it.each([
[-12.34, '-12'],
[-123.45, '-123'],
[-1234.56, '-1,235'],
[-12345.67, '-12,346'],
[-12345678.9, '-12,345,679'],
])(
'formats as integer when number is negative and above or equals 10 in absolute value',
(value, formattedValue) => {
expect(asDecimalOrInteger(value)).toEqual(formattedValue);
}
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export type AsPercent = typeof asPercent;

export function asDecimalOrInteger(value: number) {
// exact 0 or above 10 should not have decimal
if (value === 0 || value >= 10) {
if (value === 0 || Math.abs(value) >= 10) {
return asInteger(value);
}
return asDecimal(value);
Expand Down

0 comments on commit fd340b9

Please sign in to comment.