Skip to content

Commit

Permalink
tests(numbers-util): cover number utils with additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ArturHoncharuk committed Dec 19, 2024
1 parent b89a388 commit 2fd9f09
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
import { NumberUtils } from '@utils/number';

describe('NumberUtils', () => {
it('formats a number with commas and decimal places', () => {
expect(NumberUtils.formatNumber(10000, 2)).toBe('10,000.00');
expect(NumberUtils.formatNumber(10000, 3)).toBe('10,000.000');
expect(NumberUtils.formatNumber(10000, 4)).toBe('10,000.0000');
expect(NumberUtils.formatNumber(100000, 2)).toBe('100,000.00');
expect(NumberUtils.formatNumber(1000000, 2)).toBe('1,000,000.00');
expect(NumberUtils.formatNumber(1000000000, 2)).toBe('1,000,000,000.00');
expect(NumberUtils.formatNumber(1000000000000, 2)).toBe(
'1,000,000,000,000.00'
);
});

it('handles undefined and null input', () => {
it('should format positive numbers correctly', () => {
expect(NumberUtils.formatNumber(10000, 2)).toBe('10,000');
expect(NumberUtils.formatNumber(30363.564, 2)).toBe('30,363.56');
expect(NumberUtils.formatNumber(0.3634552345262345, 2)).toBe('0.36');
});

it('should format negative numbers correctly', () => {
expect(NumberUtils.formatNumber(-10000, 2)).toBe('-10,000');
expect(NumberUtils.formatNumber(-30363.564, 2)).toBe('-30,363.56');
});

it('should return empty string for undefined or null', () => {
// @ts-ignore
expect(NumberUtils.formatNumber(undefined)).toBe('');
// @ts-ignore
expect(NumberUtils.formatNumber(null)).toBe('');
});

it('should handle zero correctly', () => {
expect(NumberUtils.formatNumber(0, 2)).toBe('0');
});

it('should limit decimal places correctly', () => {
expect(NumberUtils.formatNumber(1234.5678, 3)).toBe('1,234.567');
expect(NumberUtils.formatNumber(1234.5, 1)).toBe('1,234.5');
});

it('adds a plus sign to positive numbers', () => {
expect(NumberUtils.addSignToNumber(100)).toBe('+100');
});

it('adds a minus sign to negative numbers', () => {
expect(NumberUtils.addSignToNumber(-100)).toBe('--100');
expect(NumberUtils.addSignToNumber(-100)).toBe('-100');
});

it('doesnt add a sign to zero', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ jest.mock('react-native', () => ({
describe('ScalingUtils', () => {
it('should correctly calculate the scaled value', () => {
const result = scale(10);
expect(result).toBe(57.142857142857146);
expect(result).toBeCloseTo(53.3333333333333);
});
it('should correctly calculate the vertical scaled value', () => {
const result = verticalScale(20);
expect(result).toBeCloseTo(35.94117647058824);
expect(result).toBeCloseTo(30.098522167487687);
});

it('should correctly calculate the moderate scaled value with default factor', () => {
const result = moderateScale(15);
expect(result).toBeCloseTo(50.35714285714286);
expect(result).toBeCloseTo(47.5);
});

it('should correctly calculate the moderate scaled value with custom factor', () => {
const result = moderateScale(15, 0.3);
expect(result).toBeCloseTo(36.214285714285715);
expect(result).toBeCloseTo(34.5);
});
});
File renamed without changes.

0 comments on commit 2fd9f09

Please sign in to comment.