2021-02-04 19:15:23 +01:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2022-01-20 16:42:13 +01:00
|
|
|
import { mount } from 'enzyme';
|
2021-02-04 19:15:23 +01:00
|
|
|
import sinon from 'sinon';
|
|
|
|
import { Provider } from 'react-redux';
|
|
|
|
import configureMockStore from 'redux-mock-store';
|
2022-01-26 18:48:28 +01:00
|
|
|
import UnitInput from '../../ui/unit-input';
|
|
|
|
import CurrencyDisplay from '../../ui/currency-display';
|
2022-01-20 16:42:13 +01:00
|
|
|
import CurrencyInput from './currency-input';
|
2018-10-17 01:03:29 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('CurrencyInput Component', () => {
|
|
|
|
describe('rendering', () => {
|
|
|
|
it('should render properly without a suffix', () => {
|
2022-01-20 16:42:13 +01:00
|
|
|
const mockStore = {
|
|
|
|
metamask: {
|
|
|
|
nativeCurrency: 'ETH',
|
|
|
|
currentCurrency: 'usd',
|
|
|
|
conversionRate: 231.06,
|
|
|
|
provider: {
|
|
|
|
chainId: '0x4',
|
|
|
|
},
|
|
|
|
preferences: {
|
|
|
|
showFiatInTestnets: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const store = configureMockStore()(mockStore);
|
|
|
|
const wrapper = mount(
|
|
|
|
<Provider store={store}>
|
|
|
|
<CurrencyInput />
|
|
|
|
</Provider>,
|
|
|
|
);
|
2018-10-17 01:03:29 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(wrapper).toHaveLength(1);
|
|
|
|
expect(wrapper.find(UnitInput)).toHaveLength(1);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-17 01:03:29 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should render properly with a suffix', () => {
|
2018-10-17 01:03:29 +02:00
|
|
|
const mockStore = {
|
|
|
|
metamask: {
|
2018-10-26 10:26:43 +02:00
|
|
|
nativeCurrency: 'ETH',
|
2018-10-17 01:03:29 +02:00
|
|
|
currentCurrency: 'usd',
|
|
|
|
conversionRate: 231.06,
|
2022-01-20 16:42:13 +01:00
|
|
|
provider: {
|
|
|
|
chainId: '0x4',
|
|
|
|
},
|
|
|
|
preferences: {
|
|
|
|
showFiatInTestnets: true,
|
|
|
|
},
|
2018-10-17 01:03:29 +02:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
const store = configureMockStore()(mockStore);
|
2018-10-17 01:03:29 +02:00
|
|
|
|
|
|
|
const wrapper = mount(
|
|
|
|
<Provider store={store}>
|
2022-01-20 16:42:13 +01:00
|
|
|
<CurrencyInput />
|
2020-07-14 17:20:41 +02:00
|
|
|
</Provider>,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-10-17 01:03:29 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(wrapper).toHaveLength(1);
|
|
|
|
expect(wrapper.find('.unit-input__suffix')).toHaveLength(1);
|
|
|
|
expect(wrapper.find('.unit-input__suffix').text()).toStrictEqual('ETH');
|
|
|
|
expect(wrapper.find(CurrencyDisplay)).toHaveLength(1);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-17 01:03:29 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should render properly with an ETH value', () => {
|
2018-10-17 01:03:29 +02:00
|
|
|
const mockStore = {
|
|
|
|
metamask: {
|
2018-10-26 10:26:43 +02:00
|
|
|
nativeCurrency: 'ETH',
|
2018-10-17 01:03:29 +02:00
|
|
|
currentCurrency: 'usd',
|
|
|
|
conversionRate: 231.06,
|
2022-01-20 16:42:13 +01:00
|
|
|
provider: {
|
|
|
|
chainId: '0x4',
|
|
|
|
},
|
|
|
|
preferences: {
|
|
|
|
showFiatInTestnets: true,
|
|
|
|
},
|
2018-10-17 01:03:29 +02:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
const store = configureMockStore()(mockStore);
|
2018-10-17 01:03:29 +02:00
|
|
|
|
|
|
|
const wrapper = mount(
|
|
|
|
<Provider store={store}>
|
2022-01-20 16:42:13 +01:00
|
|
|
<CurrencyInput hexValue="de0b6b3a7640000" />
|
2020-07-14 17:20:41 +02:00
|
|
|
</Provider>,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(wrapper).toHaveLength(1);
|
|
|
|
expect(wrapper.find('.unit-input__suffix')).toHaveLength(1);
|
|
|
|
expect(wrapper.find('.unit-input__suffix').text()).toStrictEqual('ETH');
|
|
|
|
expect(wrapper.find('.unit-input__input').props().value).toStrictEqual(1);
|
|
|
|
expect(wrapper.find('.currency-display-component').text()).toStrictEqual(
|
2020-11-03 00:41:28 +01:00
|
|
|
'$231.06USD',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
2018-10-17 01:03:29 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should render properly with a fiat value', () => {
|
2018-10-17 01:03:29 +02:00
|
|
|
const mockStore = {
|
|
|
|
metamask: {
|
2018-10-26 10:26:43 +02:00
|
|
|
nativeCurrency: 'ETH',
|
2018-10-17 01:03:29 +02:00
|
|
|
currentCurrency: 'usd',
|
|
|
|
conversionRate: 231.06,
|
2022-01-20 16:42:13 +01:00
|
|
|
provider: {
|
|
|
|
chainId: '0x4',
|
|
|
|
},
|
|
|
|
preferences: {
|
|
|
|
showFiatInTestnets: true,
|
|
|
|
},
|
2018-10-17 01:03:29 +02:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
const store = configureMockStore()(mockStore);
|
2022-06-06 15:11:14 +02:00
|
|
|
const handleChangeSpy = sinon.spy();
|
2018-10-17 01:03:29 +02:00
|
|
|
|
|
|
|
const wrapper = mount(
|
|
|
|
<Provider store={store}>
|
2022-06-06 15:11:14 +02:00
|
|
|
<CurrencyInput
|
|
|
|
onChange={handleChangeSpy}
|
|
|
|
hexValue="f602f2234d0ea"
|
|
|
|
featureSecondary
|
|
|
|
/>
|
2020-07-14 17:20:41 +02:00
|
|
|
</Provider>,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(wrapper).toHaveLength(1);
|
|
|
|
expect(wrapper.find('.unit-input__suffix')).toHaveLength(1);
|
|
|
|
expect(wrapper.find('.unit-input__suffix').text()).toStrictEqual('USD');
|
|
|
|
expect(wrapper.find('.unit-input__input').props().value).toStrictEqual(1);
|
|
|
|
expect(wrapper.find('.currency-display-component').text()).toStrictEqual(
|
2021-11-05 15:52:03 +01:00
|
|
|
'0.00432788ETH',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
2019-02-26 19:30:41 +01:00
|
|
|
|
2022-01-20 16:42:13 +01:00
|
|
|
it('should render properly with a native value when hideSecondary is true', () => {
|
2019-02-26 19:30:41 +01:00
|
|
|
const mockStore = {
|
|
|
|
metamask: {
|
|
|
|
nativeCurrency: 'ETH',
|
|
|
|
currentCurrency: 'usd',
|
|
|
|
conversionRate: 231.06,
|
2022-01-20 16:42:13 +01:00
|
|
|
provider: {
|
|
|
|
chainId: '0x4',
|
|
|
|
},
|
|
|
|
preferences: {
|
|
|
|
showFiatInTestnets: false,
|
|
|
|
},
|
2019-02-26 19:30:41 +01:00
|
|
|
},
|
2022-01-20 16:42:13 +01:00
|
|
|
hideSecondary: true,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
const store = configureMockStore()(mockStore);
|
2022-06-06 15:11:14 +02:00
|
|
|
const handleChangeSpy = sinon.spy();
|
2019-02-26 19:30:41 +01:00
|
|
|
|
|
|
|
const wrapper = mount(
|
|
|
|
<Provider store={store}>
|
2022-06-06 15:11:14 +02:00
|
|
|
<CurrencyInput
|
|
|
|
onChange={handleChangeSpy}
|
|
|
|
hexValue="f602f2234d0ea"
|
|
|
|
featureSecondary
|
|
|
|
/>
|
2019-02-26 19:30:41 +01:00
|
|
|
</Provider>,
|
|
|
|
{
|
2020-08-19 18:27:05 +02:00
|
|
|
context: { t: (str) => `${str}_t` },
|
2019-02-26 19:30:41 +01:00
|
|
|
childContextTypes: { t: PropTypes.func },
|
2020-07-14 17:20:41 +02:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(wrapper).toHaveLength(1);
|
|
|
|
expect(wrapper.find('.unit-input__suffix')).toHaveLength(1);
|
|
|
|
expect(wrapper.find('.unit-input__suffix').text()).toStrictEqual('ETH');
|
|
|
|
expect(wrapper.find('.unit-input__input').props().value).toStrictEqual(
|
2022-03-03 01:28:56 +01:00
|
|
|
0.00432788,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(
|
2020-11-03 00:41:28 +01:00
|
|
|
wrapper.find('.currency-input__conversion-component').text(),
|
2022-01-20 16:42:13 +01:00
|
|
|
).toStrictEqual('[noConversionRateAvailable]');
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2018-10-17 01:03:29 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('handling actions', () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const handleChangeSpy = sinon.spy();
|
|
|
|
const handleBlurSpy = sinon.spy();
|
2022-01-20 16:42:13 +01:00
|
|
|
const handleChangeToggle = sinon.spy();
|
2018-10-17 01:03:29 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
afterEach(() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
handleChangeSpy.resetHistory();
|
|
|
|
handleBlurSpy.resetHistory();
|
2022-01-20 16:42:13 +01:00
|
|
|
handleChangeToggle.resetHistory();
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-17 01:03:29 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should call onChange on input changes with the hex value for ETH', () => {
|
2018-10-17 01:03:29 +02:00
|
|
|
const mockStore = {
|
|
|
|
metamask: {
|
2018-10-26 10:26:43 +02:00
|
|
|
nativeCurrency: 'ETH',
|
2018-10-17 01:03:29 +02:00
|
|
|
currentCurrency: 'usd',
|
|
|
|
conversionRate: 231.06,
|
2022-01-20 16:42:13 +01:00
|
|
|
provider: {
|
|
|
|
chainId: '0x4',
|
|
|
|
},
|
|
|
|
preferences: {
|
|
|
|
showFiatInTestnets: true,
|
|
|
|
},
|
2018-10-17 01:03:29 +02:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
const store = configureMockStore()(mockStore);
|
2018-10-17 01:03:29 +02:00
|
|
|
const wrapper = mount(
|
|
|
|
<Provider store={store}>
|
2022-03-03 01:28:56 +01:00
|
|
|
<CurrencyInput onChange={handleChangeSpy} hexValue="f602f2234d0ea" />
|
2020-07-14 17:20:41 +02:00
|
|
|
</Provider>,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(wrapper).toHaveLength(1);
|
|
|
|
expect(handleChangeSpy.callCount).toStrictEqual(0);
|
|
|
|
expect(handleBlurSpy.callCount).toStrictEqual(0);
|
2021-02-04 19:15:23 +01:00
|
|
|
|
|
|
|
const input = wrapper.find('input');
|
2022-03-03 01:28:56 +01:00
|
|
|
expect(input.props().value).toStrictEqual(0.00432788);
|
2018-10-17 01:03:29 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
input.simulate('change', { target: { value: 1 } });
|
2022-06-06 15:11:14 +02:00
|
|
|
expect(handleChangeSpy.callCount).toStrictEqual(1);
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(handleChangeSpy.calledWith('de0b6b3a7640000')).toStrictEqual(true);
|
|
|
|
expect(wrapper.find('.currency-display-component').text()).toStrictEqual(
|
2020-11-03 00:41:28 +01:00
|
|
|
'$231.06USD',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
2018-10-17 01:03:29 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should call onChange on input changes with the hex value for fiat', () => {
|
2018-10-17 01:03:29 +02:00
|
|
|
const mockStore = {
|
|
|
|
metamask: {
|
2018-10-26 10:26:43 +02:00
|
|
|
nativeCurrency: 'ETH',
|
2018-10-17 01:03:29 +02:00
|
|
|
currentCurrency: 'usd',
|
|
|
|
conversionRate: 231.06,
|
2022-01-20 16:42:13 +01:00
|
|
|
provider: {
|
|
|
|
chainId: '0x4',
|
|
|
|
},
|
|
|
|
preferences: {
|
|
|
|
showFiatInTestnets: true,
|
|
|
|
},
|
2018-10-17 01:03:29 +02:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
const store = configureMockStore()(mockStore);
|
2018-10-17 01:03:29 +02:00
|
|
|
const wrapper = mount(
|
|
|
|
<Provider store={store}>
|
2022-01-20 16:42:13 +01:00
|
|
|
<CurrencyInput onChange={handleChangeSpy} featureSecondary />
|
2020-07-14 17:20:41 +02:00
|
|
|
</Provider>,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(wrapper).toHaveLength(1);
|
2022-06-06 15:11:14 +02:00
|
|
|
expect(handleChangeSpy.callCount).toStrictEqual(1);
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(handleBlurSpy.callCount).toStrictEqual(0);
|
2021-02-04 19:15:23 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(wrapper.find('.currency-display-component').text()).toStrictEqual(
|
2020-12-03 16:46:22 +01:00
|
|
|
'0ETH',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
const input = wrapper.find('input');
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(input.props().value).toStrictEqual(0);
|
2018-10-17 01:03:29 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
input.simulate('change', { target: { value: 1 } });
|
2022-01-20 16:42:13 +01:00
|
|
|
expect(handleChangeSpy.callCount).toStrictEqual(2);
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(handleChangeSpy.calledWith('f602f2234d0ea')).toStrictEqual(true);
|
|
|
|
expect(wrapper.find('.currency-display-component').text()).toStrictEqual(
|
2021-11-05 15:52:03 +01:00
|
|
|
'0.00432788ETH',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
2018-10-17 01:03:29 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should change the state and pass in a new decimalValue when props.value changes', () => {
|
2018-10-17 01:03:29 +02:00
|
|
|
const mockStore = {
|
|
|
|
metamask: {
|
2018-10-26 10:26:43 +02:00
|
|
|
nativeCurrency: 'ETH',
|
2018-10-17 01:03:29 +02:00
|
|
|
currentCurrency: 'usd',
|
|
|
|
conversionRate: 231.06,
|
2022-01-20 16:42:13 +01:00
|
|
|
provider: {
|
|
|
|
chainId: '0x4',
|
|
|
|
},
|
|
|
|
preferences: {
|
|
|
|
showFiatInTestnets: true,
|
|
|
|
},
|
2018-10-17 01:03:29 +02:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
const store = configureMockStore()(mockStore);
|
2022-01-20 16:42:13 +01:00
|
|
|
const wrapper = mount(
|
2018-10-17 01:03:29 +02:00
|
|
|
<Provider store={store}>
|
2022-01-20 16:42:13 +01:00
|
|
|
<CurrencyInput onChange={handleChangeSpy} featureSecondary />
|
2020-07-14 17:20:41 +02:00
|
|
|
</Provider>,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-10-17 01:03:29 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(wrapper).toHaveLength(1);
|
2022-01-20 16:42:13 +01:00
|
|
|
const input = wrapper.find('input');
|
|
|
|
expect(input.props().value).toStrictEqual(0);
|
2018-10-17 01:03:29 +02:00
|
|
|
|
2022-01-20 16:42:13 +01:00
|
|
|
wrapper.setProps({ hexValue: '1ec05e43e72400' });
|
|
|
|
input.update();
|
|
|
|
expect(input.props().value).toStrictEqual(0);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2019-02-06 16:14:17 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should swap selected currency when swap icon is clicked', () => {
|
2019-02-06 16:14:17 +01:00
|
|
|
const mockStore = {
|
|
|
|
metamask: {
|
|
|
|
nativeCurrency: 'ETH',
|
|
|
|
currentCurrency: 'usd',
|
|
|
|
conversionRate: 231.06,
|
2022-01-20 16:42:13 +01:00
|
|
|
provider: {
|
|
|
|
chainId: '0x4',
|
|
|
|
},
|
|
|
|
preferences: {
|
|
|
|
showFiatInTestnets: true,
|
|
|
|
},
|
2019-02-06 16:14:17 +01:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
const store = configureMockStore()(mockStore);
|
2019-02-06 16:14:17 +01:00
|
|
|
const wrapper = mount(
|
|
|
|
<Provider store={store}>
|
|
|
|
<CurrencyInput
|
|
|
|
onChange={handleChangeSpy}
|
2022-01-20 16:42:13 +01:00
|
|
|
onPreferenceToggle={handleChangeToggle}
|
|
|
|
featureSecondary
|
2019-02-06 16:14:17 +01:00
|
|
|
/>
|
2020-07-14 17:20:41 +02:00
|
|
|
</Provider>,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(wrapper).toHaveLength(1);
|
2022-06-06 15:11:14 +02:00
|
|
|
expect(handleChangeSpy.callCount).toStrictEqual(1);
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(handleBlurSpy.callCount).toStrictEqual(0);
|
2021-02-04 19:15:23 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(wrapper.find('.currency-display-component').text()).toStrictEqual(
|
2022-01-20 16:42:13 +01:00
|
|
|
'0ETH',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
const input = wrapper.find('input');
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(input.props().value).toStrictEqual(0);
|
2019-02-06 16:14:17 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
input.simulate('change', { target: { value: 1 } });
|
2022-01-20 16:42:13 +01:00
|
|
|
expect(handleChangeSpy.callCount).toStrictEqual(2);
|
|
|
|
expect(handleChangeSpy.calledWith('de0b6b3a7640000')).toStrictEqual(
|
|
|
|
false,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2022-01-20 16:42:13 +01:00
|
|
|
expect(wrapper.find('.currency-display-component').text()).toStrictEqual(
|
|
|
|
'0.00432788ETH',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-02-06 16:14:17 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const swap = wrapper.find('.currency-input__swap-component');
|
|
|
|
swap.simulate('click');
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(wrapper.find('.currency-display-component').text()).toStrictEqual(
|
2021-11-05 15:52:03 +01:00
|
|
|
'0.00432788ETH',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|