Skip to content

Commit

Permalink
fix(amount-input): fix float in amount input
Browse files Browse the repository at this point in the history
  • Loading branch information
stepancar committed Apr 20, 2021
1 parent 9da4d8d commit a9e1802
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
51 changes: 51 additions & 0 deletions packages/amount-input/src/Component.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,57 @@ describe('AmountInput', () => {
expect(input.value).toBe('0,01');
});

describe('should emit value in minority on change event', () => {
const dataTestId = 'test-id';

const testCases = [
{
minority: 100,
userInput: '1',
expectedValue: 100,
},
{
minority: 100,
userInput: '1,1',
expectedValue: 110,
},
{
minority: 1000,
userInput: '2',
expectedValue: 2000,
},
{
minority: 1000,
userInput: '2,2',
expectedValue: 2200,
},
{
minority: 100,
userInput: '9,12',
expectedValue: 912,
},
];

testCases.forEach(({ minority, userInput, expectedValue }) => {
it(`should emit event with value=${expectedValue} when minority=${minority} and userInput=${userInput}`, async () => {
const handleChangeMock = jest.fn();
const { getByTestId } = render(
<AmountInput
minority={minority}
dataTestId={dataTestId}
onChange={handleChangeMock}
/>,
);
const input = getByTestId(dataTestId) as HTMLInputElement;
await userEvent.paste(input, userInput);
expect(handleChangeMock).toBeCalledWith(expect.anything(), {
value: expectedValue,
valueString: userInput,
});
});
});
});

/**
* + тест на адекватность (снапшот)
* + тест на дефолтные значения (нужно разобраться про label)
Expand Down
4 changes: 3 additions & 1 deletion packages/amount-input/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,7 @@ export function getFormattedValue(enteredValue: string, currency: CurrencyCodes,
}

export function getAmountValueFromStr(str: string, minority: number) {
return str === '' ? null : Number(str.replace(',', '.').replace(/[^0-9.]/g, '')) * minority;
return str === ''
? null
: Math.round(Number(str.replace(',', '.').replace(/[^0-9.]/g, '')) * minority);
}

0 comments on commit a9e1802

Please sign in to comment.