diff --git a/web/src/utils/chessTableCellFormatValue.test.ts b/web/src/utils/chessTableCellFormatValue.test.ts new file mode 100644 index 0000000..5696c8d --- /dev/null +++ b/web/src/utils/chessTableCellFormatValue.test.ts @@ -0,0 +1,50 @@ +import { describe, expect, it } from 'vitest'; +import { chessTableCellFormatValue as fmt } from './chessTableCellFormatValue'; + +const NBSP = ' '; + +describe('chessTableCellFormatValue', () => { + it('руб.: разряды, запятая, заданное число знаков', () => { + expect(fmt('4454151.93', 'rub', 2)).toBe(`4${NBSP}454${NBSP}151,93`); + expect(fmt('100', 'rub', 2)).toBe('100,00'); + expect(fmt('123.456', 'rub', 0)).toBe('123'); + expect(fmt('123.4', 'rub', 3)).toBe('123,400'); + }); + + it('тыс./млн/млрд — сдвиг запятой в строке', () => { + expect(fmt('2346459.05', 'thousand', 2)).toBe(`2${NBSP}346,46`); + expect(fmt('2346459.05', 'million', 1)).toBe('2,3'); + expect(fmt('2346459.05', 'billion', 3)).toBe('0,002'); + expect(fmt('12', 'thousand', 3)).toBe('0,012'); + }); + + it('округление точное, без двоичной погрешности float', () => { + // 1005 руб. = 1,005 тыс.: через Number(...).toFixed(2) вышло бы «1,00» + expect(fmt('1005', 'thousand', 2)).toBe('1,01'); + expect(fmt('0.005', 'rub', 2)).toBe('0,01'); + }); + + it('большие суммы не теряют копейки', () => { + expect(fmt('12345678901234567.89', 'rub', 2)).toBe(`12${NBSP}345${NBSP}678${NBSP}901${NBSP}234${NBSP}567,89`); + }); + + it('отрицательные', () => { + expect(fmt('-1234567.1', 'rub', 2)).toBe(`-1${NBSP}234${NBSP}567,10`); + expect(fmt('-2346459.05', 'thousand', 2)).toBe(`-2${NBSP}346,46`); + }); + + it('отрицательная сумма, округлённая до нуля, — без минуса', () => { + expect(fmt('-4.00', 'thousand', 2)).toBe('0,00'); + expect(fmt('-0.001', 'rub', 0)).toBe('0'); + }); + + it('пустое значение — пустая строка', () => { + expect(fmt(null, 'rub', 2)).toBe(''); + expect(fmt(undefined, 'thousand', 2)).toBe(''); + expect(fmt('', 'million', 2)).toBe(''); + }); + + it('нечисловое значение в руб. — как пришло', () => { + expect(fmt('abc', 'rub', 2)).toBe('abc'); + }); +});