Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add NumberUtil test case #3152

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions packages/common/tests/NumberUtil.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, expect, it } from 'vitest'
import { NumberUtil } from '../src/utils/NumberUtil.js'
import BigNumber from 'bignumber.js'

// -- Tests --------------------------------------------------------------------
describe('NumberUtil', () => {
Expand All @@ -8,3 +9,40 @@ describe('NumberUtil', () => {
expect(isGreaterThan).toBe(true)
})
})


describe('multiply', () => {
it('should return 0 if either value is undefined', () => {
expect(NumberUtil.multiply(undefined, '10')).toEqual(new BigNumber(0))
expect(NumberUtil.multiply('10', undefined)).toEqual(new BigNumber(0))
})

it('should multiply two numbers represented as strings', () => {
const result = NumberUtil.multiply('2', '3')
expect(result).toEqual(new BigNumber(6))
})

it('should handle decimals correctly', () => {
const result = NumberUtil.multiply('1.5', '2.5')
expect(result).toEqual(new BigNumber(3.75))
})
})

describe('formatNumberToLocalString', () => {
it('should return "0.00" if value is undefined', () => {
expect(NumberUtil.formatNumberToLocalString(undefined)).toBe('0.00')
})

it('should format a number to a local string with specified decimals', () => {
expect(NumberUtil.formatNumberToLocalString(1234.5678, 2)).toBe('1,234.57')
expect(NumberUtil.formatNumberToLocalString(1234.5, 2)).toBe('1,234.50')
})

it('should format a string representation of a number to a local string', () => {
expect(NumberUtil.formatNumberToLocalString('1234.5678', 2)).toBe('1,234.57')
})

it('should handle invalid string input gracefully', () => {
expect(NumberUtil.formatNumberToLocalString('invalid', 2)).toBe('NaN')
})
})
Loading