@ -1,7 +1,6 @@
import React from 'react' ;
import PropTypes from 'prop-types' ;
import { shallow , mount } from 'enzyme' ;
import sinon from 'sinon' ;
import { Provider } from 'react-redux' ;
import configureMockStore from 'redux-mock-store' ;
import UnitInput from '../unit-input' ;
@ -207,12 +206,10 @@ describe('TokenInput Component', () => {
} ) ;
describe ( 'handling actions' , ( ) => {
const handleChangeSpy = sinon . spy ( ) ;
const handleBlurSpy = sinon . spy ( ) ;
const handleChangeSpy = jest . fn ( ) ;
afterEach ( ( ) => {
handleChangeSpy . resetHistory ( ) ;
handleBlurSpy . resetHistory ( ) ;
handleChangeSpy . mockClear ( ) ;
} ) ;
it ( 'should call onChange on input changes with the hex value for ETH' , ( ) => {
@ -238,8 +235,7 @@ describe('TokenInput Component', () => {
) ;
expect ( wrapper ) . toHaveLength ( 1 ) ;
expect ( handleChangeSpy . callCount ) . toStrictEqual ( 0 ) ;
expect ( handleBlurSpy . callCount ) . toStrictEqual ( 0 ) ;
expect ( handleChangeSpy . mock . calls ) . toHaveLength ( 0 ) ;
const tokenInputInstance = wrapper . find ( TokenInput ) . at ( 0 ) . instance ( ) ;
expect ( tokenInputInstance . state . decimalValue ) . toStrictEqual ( 0 ) ;
@ -250,13 +246,13 @@ describe('TokenInput Component', () => {
const input = wrapper . find ( 'input' ) ;
expect ( input . props ( ) . value ) . toStrictEqual ( 0 ) ;
input . simulate ( 'change' , { target : { value : 1 } } ) ;
expect ( handleChangeSpy . callCount ) . toStrictEqual ( 1 ) ;
expect ( handleChangeSpy . calledWith ( '2710' ) ) . toStrictEqual ( true ) ;
input . simulate ( 'change' , { target : { value : '1' } } ) ;
expect ( handleChangeSpy . mock . calls ) . toHaveLength ( 1 ) ;
expect ( handleChangeSpy . mock . calls [ 0 ] [ 0 ] ) . toStrictEqual ( '2710' ) ;
expect ( wrapper . find ( '.currency-display-component' ) . text ( ) ) . toStrictEqual (
'2ETH' ,
) ;
expect ( tokenInputInstance . state . decimalValue ) . toStrictEqual ( 1 ) ;
expect ( tokenInputInstance . state . decimalValue ) . toStrictEqual ( '1' ) ;
expect ( tokenInputInstance . state . hexValue ) . toStrictEqual ( '2710' ) ;
} ) ;
@ -285,8 +281,7 @@ describe('TokenInput Component', () => {
) ;
expect ( wrapper ) . toHaveLength ( 1 ) ;
expect ( handleChangeSpy . callCount ) . toStrictEqual ( 0 ) ;
expect ( handleBlurSpy . callCount ) . toStrictEqual ( 0 ) ;
expect ( handleChangeSpy . mock . calls ) . toHaveLength ( 0 ) ;
const tokenInputInstance = wrapper . find ( TokenInput ) . at ( 0 ) . instance ( ) ;
expect ( tokenInputInstance . state . decimalValue ) . toStrictEqual ( 0 ) ;
@ -297,13 +292,13 @@ describe('TokenInput Component', () => {
const input = wrapper . find ( 'input' ) ;
expect ( input . props ( ) . value ) . toStrictEqual ( 0 ) ;
input . simulate ( 'change' , { target : { value : 1 } } ) ;
expect ( handleChangeSpy . callCount ) . toStrictEqual ( 1 ) ;
expect ( handleChangeSpy . calledWith ( '2710' ) ) . toStrictEqual ( true ) ;
input . simulate ( 'change' , { target : { value : '1' } } ) ;
expect ( handleChangeSpy . mock . calls ) . toHaveLength ( 1 ) ;
expect ( handleChangeSpy . mock . calls [ 0 ] [ 0 ] ) . toStrictEqual ( '2710' ) ;
expect ( wrapper . find ( '.currency-display-component' ) . text ( ) ) . toStrictEqual (
'$462.12USD' ,
) ;
expect ( tokenInputInstance . state . decimalValue ) . toStrictEqual ( 1 ) ;
expect ( tokenInputInstance . state . decimalValue ) . toStrictEqual ( '1' ) ;
expect ( tokenInputInstance . state . hexValue ) . toStrictEqual ( '2710' ) ;
} ) ;
@ -345,4 +340,84 @@ describe('TokenInput Component', () => {
) ;
} ) ;
} ) ;
describe ( 'Token Input Decimals Check' , ( ) => {
const handleChangeSpy = jest . fn ( ) ;
afterEach ( ( ) => {
handleChangeSpy . mockClear ( ) ;
} ) ;
it ( 'should render incorrect hex onChange when input decimals is more than token decimals' , ( ) => {
const mockStore = {
metamask : {
currentCurrency : 'usd' ,
conversionRate : 231.06 ,
} ,
} ;
const store = configureMockStore ( ) ( mockStore ) ;
const wrapper = mount (
< Provider store = { store } >
< TokenInput
onChange = { handleChangeSpy }
token = { {
address : '0x1' ,
decimals : 4 ,
symbol : 'ABC' ,
} }
tokenExchangeRates = { { '0x1' : 2 } }
showFiat
currentCurrency = "usd"
/ >
< / P r o v i d e r > ,
) ;
expect ( wrapper ) . toHaveLength ( 1 ) ;
expect ( handleChangeSpy . mock . calls ) . toHaveLength ( 0 ) ;
const input = wrapper . find ( 'input' ) ;
expect ( input . props ( ) . value ) . toStrictEqual ( 0 ) ;
input . simulate ( 'change' , { target : { value : '1.11111' } } ) ;
expect ( handleChangeSpy . mock . calls ) . toHaveLength ( 1 ) ;
expect ( handleChangeSpy . mock . calls [ 0 ] [ 0 ] ) . toStrictEqual (
'2b67.1999999999999999999a' ,
) ;
} ) ;
it ( 'should render correct hex onChange when input decimals is more than token decimals by omitting excess fractional part on blur' , ( ) => {
const mockStore = {
metamask : {
currentCurrency : 'usd' ,
conversionRate : 231.06 ,
} ,
} ;
const store = configureMockStore ( ) ( mockStore ) ;
const wrapper = mount (
< Provider store = { store } >
< TokenInput
onChange = { handleChangeSpy }
token = { {
address : '0x1' ,
decimals : 4 ,
symbol : 'ABC' ,
} }
tokenExchangeRates = { { '0x1' : 2 } }
showFiat
currentCurrency = "usd"
/ >
< / P r o v i d e r > ,
) ;
expect ( wrapper ) . toHaveLength ( 1 ) ;
const input = wrapper . find ( 'input' ) ;
input . simulate ( 'blur' , { target : { value : '1.11111' } } ) ;
expect ( handleChangeSpy . mock . calls ) . toHaveLength ( 1 ) ;
expect ( handleChangeSpy . mock . calls [ 0 ] [ 0 ] ) . toStrictEqual ( '2b67' ) ;
} ) ;
} ) ;
} ) ;