From 7e605c40585e5f05e8c85c22b500412774b0f0b7 Mon Sep 17 00:00:00 2001 From: Seena Rowhani Date: Mon, 21 May 2018 14:53:33 -0400 Subject: [PATCH] Don't require leading 0 in amount field (#1718) Don't require leading 0 in amount field --- .../sagas/transaction/current/currentValue.ts | 5 ++--- .../transaction/current/currentValue.spec.ts | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/common/sagas/transaction/current/currentValue.ts b/common/sagas/transaction/current/currentValue.ts index fd055ca7..56c1f087 100644 --- a/common/sagas/transaction/current/currentValue.ts +++ b/common/sagas/transaction/current/currentValue.ts @@ -32,8 +32,7 @@ export function* valueHandler( const unit: string = yield select(getUnit); const isEth = yield select(isEtherTransaction); const validNum = isEth ? validNumber : validPositiveNumber; - - if (!validNum(parseInt(payload, 10)) || !validDecimal(payload, decimal)) { + if (!validNum(Number(payload)) || !validDecimal(payload, decimal)) { return yield put(setter({ raw: payload, value: null })); } const value = toTokenBase(payload, decimal); @@ -59,7 +58,7 @@ export function* reparseCurrentValue(value: IInput): SagaIterator { const decimal = yield select(getDecimal); const validNum = isEth ? validNumber : validPositiveNumber; - if (validNum(parseInt(value.raw, 10)) && validDecimal(value.raw, decimal)) { + if (validNum(Number(value.raw)) && validDecimal(value.raw, decimal)) { return { raw: value.raw, value: toTokenBase(value.raw, decimal) diff --git a/spec/sagas/transaction/current/currentValue.spec.ts b/spec/sagas/transaction/current/currentValue.spec.ts index e99d991b..7439f50d 100644 --- a/spec/sagas/transaction/current/currentValue.spec.ts +++ b/spec/sagas/transaction/current/currentValue.spec.ts @@ -45,8 +45,10 @@ describe('valueHandler', () => { setter ); gen.invalidZeroToken = cloneableGenerator(valueHandler)(zeroAction, setTokenValue); + const value = toTokenBase(action.payload, decimal); const zeroValue = toTokenBase(zeroAction.payload, decimal); + const unit = 'eth'; const isEth = true; @@ -76,6 +78,23 @@ describe('valueHandler', () => { ); }); + it('handles floats without lead zero', () => { + const leadZeroValue = { + decimal: 18, + action: { + payload: '.1' + } + }; + const g = cloneableGenerator(valueHandler)(leadZeroValue.action as any, setter); + + expect(g.next().value).toEqual(select(getDecimal)); + expect(g.next(leadZeroValue.decimal).value).toEqual(select(getUnit)); + expect(g.next(unit).value).toEqual(select(isEtherTransaction)); + expect(g.next(isEth).value).not.toEqual( + put(setter({ raw: leadZeroValue.action.payload, value: null })) + ); + }); + it('should fail on invalid number or decimal and put null as value', () => { expect(gen.invalidNumber.next(isEth).value).toEqual( put(setter({ raw: failCases.invalidNumber.action.payload, value: null }))