From 8431111f1c1769e667321de6ea428e831c38185d Mon Sep 17 00:00:00 2001 From: Marino Skalnik Date: Thu, 19 Dec 2024 15:07:29 +0100 Subject: [PATCH] Fix currency formatting with MachineCurrencyFormat The machineFormatCurrency method incorrectly assumed commas as group separators, causing issues with formats using dots or no decimals. Introduce MachineCurrencyFormat to ensure consistent parsing. Update tests to cover all formats and fix validation tests for isIntegerCreator. Build process now runs tests, so the build step was removed from yarn test. --- typescript-react/package.json | 5 +- typescript-react/scripts/postinstall.js | 10 ++++ .../src/components/View/FiltersForm.tsx | 3 +- .../__tests__/__snapshots__/type.spec.ts.snap | 10 ++-- .../src/util/Formatters/CurrencyFormatter.ts | 4 +- .../__tests__/CurrencyFormatter.spec.ts | 51 +++++++++++++++++++ 6 files changed, 72 insertions(+), 11 deletions(-) diff --git a/typescript-react/package.json b/typescript-react/package.json index 5bf40a9e..3733c917 100644 --- a/typescript-react/package.json +++ b/typescript-react/package.json @@ -90,12 +90,11 @@ "predeploy": "cd example && yarn install && yarn run build", "prepare": "run-s build", "start": "microbundle-crl watch --no-compress --format modern,cjs", - "test": "run-s test:unit test:lint test:build", - "test:build": "run-s build", + "test": "run-s test:unit test:lint", "test:lint": "eslint .", "test:unit": "cross-env CI=1 react-scripts test --env=jsdom", "test:watch": "react-scripts test --env=jsdom" }, "source": "src/index.ts", - "version": "1.9.14" + "version": "1.9.15" } diff --git a/typescript-react/scripts/postinstall.js b/typescript-react/scripts/postinstall.js index da0e2648..0711384e 100644 --- a/typescript-react/scripts/postinstall.js +++ b/typescript-react/scripts/postinstall.js @@ -1,5 +1,6 @@ const fs = require('fs'); const path = require('path'); +const { execSync } = require('child_process'); const problematicIndexJsPath = path.resolve( __dirname, @@ -7,6 +8,15 @@ const problematicIndexJsPath = path.resolve( ); const fixedPath = problematicIndexJsPath.replace(/\.js$/, '.mjs'); +try { + console.log('Running tests before proceeding with prebuild script...'); + execSync('yarn test', { stdio: 'inherit' }); + console.log('Tests passed successfully.'); +} catch (error) { + console.error('Tests failed. Exiting prebuild script.'); + process.exit(1); +} + console.log('Revenj: running prebuild script'); if (fs.existsSync(problematicIndexJsPath)) { console.log( diff --git a/typescript-react/src/components/View/FiltersForm.tsx b/typescript-react/src/components/View/FiltersForm.tsx index 1cf394ea..420f4741 100644 --- a/typescript-react/src/components/View/FiltersForm.tsx +++ b/typescript-react/src/components/View/FiltersForm.tsx @@ -30,13 +30,14 @@ const processOnSubmit = (onSubmit: (data: R) => Promise, formUnderKey?: export class FiltersForm extends React.PureComponent> { public render() { - const { children, configuration, formUnderKey, hideSubmit, initialValues } = this.props; + const { children, configuration, formUnderKey, hideSubmit, initialValues, ...rest } = this.props; const child = ( { (ctx) => (
{ - const format = (this.currencyFormat || CurrencyFormat).replace(',', ''); - return formatNumber(new MachineBigNumber(number).toString(), format); + return formatNumber(new MachineBigNumber(number).toString(), MachineCurrencyFormat); } formatNumber = (number: NumberUtils.Numeric): string => { diff --git a/typescript-react/src/util/Formatters/__tests__/CurrencyFormatter.spec.ts b/typescript-react/src/util/Formatters/__tests__/CurrencyFormatter.spec.ts index 7c3b20b0..5cbc6cfb 100644 --- a/typescript-react/src/util/Formatters/__tests__/CurrencyFormatter.spec.ts +++ b/typescript-react/src/util/Formatters/__tests__/CurrencyFormatter.spec.ts @@ -71,4 +71,55 @@ describe('Currency Formatter', () => { expect(machineFormatter('1')).toBe('1.00'); }); }); + + describe('machineFormatCurrency', () => { + const testCases = [ + // Valid integers + { input: '1', output: '1.00' }, + { input: '12', output: '12.00' }, + { input: '123', output: '123.00' }, + { input: '1234', output: '1234.00' }, + { input: '12345', output: '12345.00' }, + { input: '123456789', output: '123456789.00' }, + // Valid zero and negative numbers + { input: '0', output: '0.00' }, + { input: '-1', output: '-1.00' }, + { input: '-12345', output: '-12345.00' }, + // Valid decimals + { input: '1.1', output: '1.10' }, + { input: '1.123', output: '1.12' }, + { input: '1234.5678', output: '1234.57' }, + { input: '1000000000000.23', output: '1000000000000.23' }, + // Input with leading zeros + { input: '000123', output: '123.00' }, + { input: '0000', output: '0.00' }, + // Input with trailing dot + { input: '123.', output: '123.00' }, + { input: '123.0', output: '123.00' }, + // Invalid inputs + { input: '', output: '' }, + { input: 'abc', output: '' }, + { input: '12abc', output: '' }, + { input: '1.2.3', output: '' }, + ]; + + const formats = [ + '#0', + '#,##0', + '#.##0', + '#,##0.00', + '#.##0,00', + '#,##,##0', + ]; + + formats.forEach((format) => { + it(`should format correctly according to the ${format} format`, () => { + CurrencyFormatter.setFormat(format); + + testCases.forEach(({ input, output }) => { + expect(CurrencyFormatter.machineFormatCurrency(input)).toBe(output); + }); + }); + }); + }); });