ghostfolio/libs/common/src/lib/helper.spec.ts
Guillermo Tomás Fernández Martín 512b84016c
Bugfix/fix issue with detection of thousand separator by locale (#4243)
* Fix issue with detection of thousand separator by locale

* Update changelog
2025-01-24 19:39:24 +01:00

120 lines
3.6 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {
extractNumberFromString,
getNumberFormatGroup
} from '@ghostfolio/common/helper';
describe('Helper', () => {
describe('Extract number from string', () => {
it('Get decimal number', () => {
expect(extractNumberFromString({ value: '999.99' })).toEqual(999.99);
});
it('Get decimal number (with spaces)', () => {
expect(extractNumberFromString({ value: ' 999.99 ' })).toEqual(999.99);
});
it('Get decimal number (with currency)', () => {
expect(extractNumberFromString({ value: '999.99 CHF' })).toEqual(999.99);
});
it('Get decimal number (comma notation)', () => {
expect(
extractNumberFromString({ locale: 'de-DE', value: '999,99' })
).toEqual(999.99);
});
it('Get decimal number with group (dot notation)', () => {
expect(
extractNumberFromString({ locale: 'de-CH', value: '99999.99' })
).toEqual(99999.99);
});
it('Get decimal number with group (comma notation)', () => {
expect(
extractNumberFromString({ locale: 'de-DE', value: '99.999,99' })
).toEqual(99999.99);
});
it('Get decimal number (comma notation) for locale where currency is not grouped by default', () => {
expect(
extractNumberFromString({ locale: 'es-ES', value: '999,99' })
).toEqual(999.99);
});
it('Not a number', () => {
expect(extractNumberFromString({ value: 'X' })).toEqual(NaN);
});
});
describe('Get number format group', () => {
let languageGetter: jest.SpyInstance<string, [], any>;
beforeEach(() => {
languageGetter = jest.spyOn(window.navigator, 'language', 'get');
});
it('Get de-CH number format group', () => {
expect(getNumberFormatGroup('de-CH')).toEqual('');
});
it('Get de-CH number format group when it is default', () => {
languageGetter.mockReturnValue('de-CH');
expect(getNumberFormatGroup()).toEqual('');
});
it('Get de-DE number format group', () => {
expect(getNumberFormatGroup('de-DE')).toEqual('.');
});
it('Get de-DE number format group when it is default', () => {
languageGetter.mockReturnValue('de-DE');
expect(getNumberFormatGroup()).toEqual('.');
});
it('Get en-GB number format group', () => {
expect(getNumberFormatGroup('en-GB')).toEqual(',');
});
it('Get en-GB number format group when it is default', () => {
languageGetter.mockReturnValue('en-GB');
expect(getNumberFormatGroup()).toEqual(',');
});
it('Get en-US number format group', () => {
expect(getNumberFormatGroup('en-US')).toEqual(',');
});
it('Get en-US number format group when it is default', () => {
languageGetter.mockReturnValue('en-US');
expect(getNumberFormatGroup()).toEqual(',');
});
it('Get es-ES number format group', () => {
expect(getNumberFormatGroup('es-ES')).toEqual('.');
});
it('Get es-ES number format group when it is default', () => {
languageGetter.mockReturnValue('es-ES');
expect(getNumberFormatGroup()).toEqual('.');
});
it('Get ru-RU number format group', () => {
expect(getNumberFormatGroup('ru-RU')).toEqual(' ');
});
it('Get ru-RU number format group when it is default', () => {
languageGetter.mockReturnValue('ru-RU');
expect(getNumberFormatGroup()).toEqual(' ');
});
it('Get zh-CN number format group', () => {
expect(getNumberFormatGroup('zh-CN')).toEqual(',');
});
it('Get zh-CN number format group when it is default', () => {
languageGetter.mockReturnValue('zh-CN');
expect(getNumberFormatGroup()).toEqual(',');
});
});
});