Skip to content

Commit

Permalink
test: update CreditsAmount tests and add utility function tests
Browse files Browse the repository at this point in the history
  • Loading branch information
r41ph committed Dec 12, 2024
1 parent 6b28c95 commit 5d55063
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ describe('CreditsAmount', () => {
jotaiDefaultValues: [[paymentOptionAtom, 'card']],
});

const currencyInput = screen.getByLabelText(/Currency Input/i);
userEvent.clear(currencyInput);
await userEvent.type(currencyInput, '50');
expect(currencyInput).toHaveValue(50);
const currencyInput = await screen.findByLabelText(/Currency Input/i);
if (currencyInput) {
userEvent.clear(currencyInput);
await userEvent.type(currencyInput, '50');
expect(currencyInput).toHaveValue(50);
}
});

it('updates currency amount when credits amount changes', async () => {
Expand All @@ -63,7 +65,7 @@ describe('CreditsAmount', () => {
});

const creditsInput = screen.getByLabelText(/Credits Input/i);
const currencyInput = screen.getByLabelText(/Currency Input/i);
const currencyInput = await screen.findByLabelText(/Currency Input/i);

userEvent.clear(creditsInput);
await userEvent.type(creditsInput, '101');
Expand All @@ -78,7 +80,7 @@ describe('CreditsAmount', () => {
});

const creditsInput = screen.getByLabelText(/Credits Input/i);
const currencyInput = screen.getByLabelText(/Currency Input/i);
const currencyInput = await screen.findByLabelText(/Currency Input/i);

userEvent.clear(currencyInput);
await userEvent.type(currencyInput, '102');
Expand All @@ -96,7 +98,7 @@ describe('CreditsAmount', () => {
name: /Max Credits/i,
});
const creditsInput = screen.getByLabelText(/Credits Input/i);
const currencyInput = screen.getByLabelText(/Currency Input/i);
const currencyInput = await screen.findByLabelText(/Currency Input/i);

await userEvent.click(maxCreditsButton);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { formatCurrencyAmount } from './CreditsAmount.utils';

describe('formatCurrencyAmount', () => {
it('should format a number to max two decimals', () => {
expect(formatCurrencyAmount(123.456)).toBe(123.45);
expect(formatCurrencyAmount(123)).toBe(123);
expect(formatCurrencyAmount(123.4)).toBe(123.4);
});

it('should format a string to two decimals', () => {
expect(formatCurrencyAmount('123.456')).toBe(123.45);
expect(formatCurrencyAmount('123')).toBe(123);
expect(formatCurrencyAmount('123.4')).toBe(123.4);
});

it('should round up to two decimals if roundUpDecimal is true', () => {
expect(formatCurrencyAmount(123.456, true)).toBe(123.46);
expect(formatCurrencyAmount(123.451, true)).toBe(123.46);
expect(formatCurrencyAmount(123.4, true)).toBe(123.4);
});

it('should return 0 for invalid numeric values', () => {
expect(formatCurrencyAmount('abc')).toBe(0);
expect(formatCurrencyAmount(NaN)).toBe(0);
});

it('should handle edge cases', () => {
expect(formatCurrencyAmount(0)).toBe(0);
expect(formatCurrencyAmount('0')).toBe(0);
expect(formatCurrencyAmount('')).toBe(0);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export const formatCurrencyAmount = (
// the first two decimals only, if any, or rounding them up.
const formattedValue = roundUpDecimal
? +(Math.ceil(numericValue * 100) / 100).toFixed(2)
: decimalPart?.length > 2
: decimalPart
? +`${integerPart}.${decimalPart.slice(0, 2)}`
: +`${integerPart}`;

Expand Down

0 comments on commit 5d55063

Please sign in to comment.