// SPDX-FileCopyrightText: © 2024 LEDGER SAS // SPDX-License-Identifier: Apache-2.0 import type { Unit } from '../api/types' import { BigNumber } from 'bignumber.js' // Forked from @ledgerhq/live-currency-format/src/formatCurrencyUnit.test.ts // Snapshot tests using @ledgerhq/cryptoassets removed (not available in this repo) import { formatCurrencyUnit, formatCurrencyUnitFragment } from './formatCurrencyUnit' describe('formatCurrencyUnit', () => { describe('error handling', () => { const unit: Unit = { name: 'Test', code: 'TST', magnitude: 2, showAllDigits: false, prefixCode: false, } test('handles non-BigNumber', () => { const consoleSpy = jest.spyOn(console, 'warn').mockImplementation() // @ts-expect-error intentionally passing null for negative-path test expect(formatCurrencyUnit(unit, null)).toBe('') consoleSpy.mockRestore() }) test('handles NaN', () => { const consoleSpy = jest.spyOn(console, 'warn').mockImplementation() expect(formatCurrencyUnit(unit, new BigNumber(NaN))).toBe('') consoleSpy.mockRestore() }) test('handles Infinity', () => { const consoleSpy = jest.spyOn(console, 'warn').mockImplementation() expect(formatCurrencyUnit(unit, new BigNumber(Infinity))).toBe('') consoleSpy.mockRestore() }) }) describe('formatCurrencyUnit options', () => { const unit: Unit = { name: 'Test', code: 'USD', magnitude: 2, showAllDigits: false, prefixCode: false, } test('prefixCode true', () => { const prefixUnit: Unit = { ...unit, prefixCode: true } expect(formatCurrencyUnit(prefixUnit, new BigNumber('12345'), { showCode: true })).toBe( 'USD123.45' ) }) test('prefixCode false', () => { expect(formatCurrencyUnit(unit, new BigNumber('12345'), { showCode: true })).toBe( '123.45\u00A0USD' ) }) test('negative with prefixCode', () => { const prefixUnit: Unit = { ...unit, prefixCode: true } expect(formatCurrencyUnit(prefixUnit, new BigNumber('-12345'), { showCode: true })).toBe( '-USD123.45' ) }) test('negative with suffixCode', () => { expect(formatCurrencyUnit(unit, new BigNumber('-12345'), { showCode: true })).toBe( '-123.45\u00A0USD' ) }) test('alwaysShowSign', () => { expect(formatCurrencyUnit(unit, new BigNumber('12345'), { alwaysShowSign: true })).toBe( '+123.45' ) }) test('discreet mode', () => { expect(formatCurrencyUnit(unit, new BigNumber('12345'), { discreet: true })).toBe('***') }) test('joinFragmentsSeparator', () => { const prefixUnit: Unit = { ...unit, prefixCode: true } expect( formatCurrencyUnit(prefixUnit, new BigNumber('12345'), { showCode: true, joinFragmentsSeparator: '-', }) ).toBe('USD-123.45') }) test('useGrouping false', () => { expect( formatCurrencyUnit(unit, new BigNumber('12345678'), { useGrouping: false }) ).not.toContain(',') }) test('showAllDigits', () => { const highMagUnit: Unit = { ...unit, magnitude: 4 } expect(formatCurrencyUnit(highMagUnit, new BigNumber('12345'), { showAllDigits: true })).toBe( '1.2345' ) }) test('filters undefined options', () => { expect( formatCurrencyUnit(unit, new BigNumber('12345'), { showCode: undefined, locale: 'en-US' }) ).toBe('123.45') }) }) describe('formatCurrencyUnitFragment', () => { const unit: Unit = { name: 'Test', code: 'USD', magnitude: 2, showAllDigits: false, prefixCode: false, } test('basic structure', () => { const result = formatCurrencyUnitFragment(unit, new BigNumber('12345')) expect(result.integerPart).toBe('123') expect(result.decimalPart).toBe('45') expect(result.decimalSeparator).toBe('.') expect(result.currencyText).toBe('') expect(result.currencyPosition).toBe('end') }) test('negative with prefixCode false', () => { const result = formatCurrencyUnitFragment(unit, new BigNumber('-12345'), { showCode: true }) expect(result.integerPart).toBe('-123') expect(result.currencyText).toBe('USD') }) test('negative with prefixCode true', () => { const prefixUnit: Unit = { ...unit, prefixCode: true } const result = formatCurrencyUnitFragment(prefixUnit, new BigNumber('-12345'), { showCode: true, }) expect(result.integerPart).toBe('123') expect(result.currencyText).toBe('-USD') }) test('negative with prefixCode true but no code', () => { const prefixUnit: Unit = { ...unit, prefixCode: true } const result = formatCurrencyUnitFragment(prefixUnit, new BigNumber('-12345'), { showCode: false, }) expect(result.integerPart).toBe('-123') expect(result.currencyText).toBe('') }) test('alwaysShowSign', () => { const result = formatCurrencyUnitFragment(unit, new BigNumber('12345'), { alwaysShowSign: true, }) expect(result.integerPart).toBe('+123') }) test('no decimals', () => { const result = formatCurrencyUnitFragment(unit, new BigNumber('10000')) expect(result.integerPart).toBe('100') expect(result.decimalPart).toBe('') }) test('french locale', () => { const result = formatCurrencyUnitFragment(unit, new BigNumber('12345'), { locale: 'fr-FR' }) expect(result.decimalSeparator).toBe(',') }) test('error handling', () => { const consoleSpy = jest.spyOn(console, 'warn').mockImplementation() // @ts-expect-error intentionally passing null for negative-path test const result = formatCurrencyUnitFragment(unit, null) expect(result.integerPart).toBe('') expect(result.decimalPart).toBe('') consoleSpy.mockRestore() }) test('error handling with prefixCode true', () => { const prefixUnit: Unit = { ...unit, prefixCode: true } const consoleSpy = jest.spyOn(console, 'warn').mockImplementation() const result = formatCurrencyUnitFragment(prefixUnit, new BigNumber(NaN)) expect(result.currencyPosition).toBe('start') consoleSpy.mockRestore() }) }) describe('coverage edge cases', () => { test('disableRounding with subMagnitude', () => { const unit: Unit = { name: 'Test', code: 'TST', magnitude: 2, showAllDigits: false, prefixCode: false, } const value = new BigNumber('12345') const result = formatCurrencyUnit(unit, value, { disableRounding: true, subMagnitude: 1 }) expect(result).toContain('123.45') }) test('prefixCode true without code', () => { const unit: Unit = { name: 'Test', code: 'USD', magnitude: 2, showAllDigits: false, prefixCode: true, } const value = new BigNumber('12345') const result = formatCurrencyUnit(unit, value, { showCode: false }) expect(result).toBe('123.45') }) }) })