You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
310 lines
9.6 KiB
310 lines
9.6 KiB
4 years ago
|
import React from 'react';
|
||
|
import sinon from 'sinon';
|
||
4 years ago
|
import { shallowWithContext } from '../../../../../test/lib/render-helpers';
|
||
4 years ago
|
import { GAS_ESTIMATE_TYPES } from '../../../../helpers/constants/common';
|
||
6 years ago
|
|
||
4 years ago
|
import ButtonGroup from '../../../ui/button-group';
|
||
|
import GasPriceButtonGroup from './gas-price-button-group.component';
|
||
6 years ago
|
|
||
4 years ago
|
describe('GasPriceButtonGroup Component', () => {
|
||
4 years ago
|
let mockButtonPropsAndFlags;
|
||
|
let mockGasPriceButtonGroupProps;
|
||
|
let wrapper;
|
||
6 years ago
|
|
||
4 years ago
|
beforeEach(() => {
|
||
5 years ago
|
mockGasPriceButtonGroupProps = {
|
||
|
buttonDataLoading: false,
|
||
|
className: 'gas-price-button-group',
|
||
|
gasButtonInfo: [
|
||
|
{
|
||
4 years ago
|
gasEstimateType: GAS_ESTIMATE_TYPES.SLOW,
|
||
5 years ago
|
feeInPrimaryCurrency: '$0.52',
|
||
|
feeInSecondaryCurrency: '0.0048 ETH',
|
||
|
timeEstimate: '~ 1 min 0 sec',
|
||
|
priceInHexWei: '0xa1b2c3f',
|
||
|
},
|
||
|
{
|
||
4 years ago
|
gasEstimateType: GAS_ESTIMATE_TYPES.AVERAGE,
|
||
5 years ago
|
feeInPrimaryCurrency: '$0.39',
|
||
|
feeInSecondaryCurrency: '0.004 ETH',
|
||
|
timeEstimate: '~ 1 min 30 sec',
|
||
|
priceInHexWei: '0xa1b2c39',
|
||
|
},
|
||
|
{
|
||
4 years ago
|
gasEstimateType: GAS_ESTIMATE_TYPES.FAST,
|
||
5 years ago
|
feeInPrimaryCurrency: '$0.30',
|
||
|
feeInSecondaryCurrency: '0.00354 ETH',
|
||
|
timeEstimate: '~ 2 min 1 sec',
|
||
|
priceInHexWei: '0xa1b2c30',
|
||
|
},
|
||
|
],
|
||
|
handleGasPriceSelection: sinon.spy(),
|
||
|
noButtonActiveByDefault: true,
|
||
|
defaultActiveButtonIndex: 2,
|
||
|
showCheck: true,
|
||
4 years ago
|
};
|
||
5 years ago
|
|
||
4 years ago
|
mockButtonPropsAndFlags = {
|
||
5 years ago
|
className: mockGasPriceButtonGroupProps.className,
|
||
4 years ago
|
handleGasPriceSelection:
|
||
|
mockGasPriceButtonGroupProps.handleGasPriceSelection,
|
||
5 years ago
|
showCheck: mockGasPriceButtonGroupProps.showCheck,
|
||
4 years ago
|
};
|
||
5 years ago
|
|
||
4 years ago
|
sinon.spy(GasPriceButtonGroup.prototype, 'renderButton');
|
||
|
sinon.spy(GasPriceButtonGroup.prototype, 'renderButtonContent');
|
||
5 years ago
|
|
||
4 years ago
|
wrapper = shallowWithContext(
|
||
4 years ago
|
<GasPriceButtonGroup {...mockGasPriceButtonGroupProps} />,
|
||
|
);
|
||
|
});
|
||
6 years ago
|
|
||
4 years ago
|
afterEach(() => {
|
||
4 years ago
|
sinon.restore();
|
||
|
});
|
||
6 years ago
|
|
||
4 years ago
|
describe('render', () => {
|
||
|
it('should render a ButtonGroup', () => {
|
||
|
expect(wrapper.is(ButtonGroup)).toStrictEqual(true);
|
||
4 years ago
|
});
|
||
6 years ago
|
|
||
4 years ago
|
it('should render the correct props on the ButtonGroup', () => {
|
||
6 years ago
|
const {
|
||
|
className,
|
||
|
defaultActiveButtonIndex,
|
||
|
noButtonActiveByDefault,
|
||
4 years ago
|
} = wrapper.props();
|
||
4 years ago
|
expect(className).toStrictEqual('gas-price-button-group');
|
||
|
expect(defaultActiveButtonIndex).toStrictEqual(2);
|
||
|
expect(noButtonActiveByDefault).toStrictEqual(true);
|
||
4 years ago
|
});
|
||
6 years ago
|
|
||
4 years ago
|
function renderButtonArgsTest(i, mockPropsAndFlags) {
|
||
4 years ago
|
expect(
|
||
6 years ago
|
GasPriceButtonGroup.prototype.renderButton.getCall(i).args,
|
||
4 years ago
|
).toStrictEqual([
|
||
|
{ ...mockGasPriceButtonGroupProps.gasButtonInfo[i] },
|
||
|
mockPropsAndFlags,
|
||
|
i,
|
||
|
]);
|
||
6 years ago
|
}
|
||
|
|
||
4 years ago
|
it('should call this.renderButton 3 times, with the correct args', () => {
|
||
|
expect(
|
||
4 years ago
|
GasPriceButtonGroup.prototype.renderButton.callCount,
|
||
4 years ago
|
).toStrictEqual(3);
|
||
4 years ago
|
renderButtonArgsTest(0, mockButtonPropsAndFlags);
|
||
|
renderButtonArgsTest(1, mockButtonPropsAndFlags);
|
||
|
renderButtonArgsTest(2, mockButtonPropsAndFlags);
|
||
|
});
|
||
6 years ago
|
|
||
4 years ago
|
it('should show loading if buttonDataLoading', () => {
|
||
4 years ago
|
wrapper.setProps({ buttonDataLoading: true });
|
||
4 years ago
|
expect(wrapper.is('div')).toStrictEqual(true);
|
||
|
expect(
|
||
|
wrapper.hasClass('gas-price-button-group__loading-container'),
|
||
|
).toStrictEqual(true);
|
||
|
expect(wrapper.text()).toStrictEqual('loading');
|
||
4 years ago
|
});
|
||
|
});
|
||
6 years ago
|
|
||
4 years ago
|
describe('renderButton', () => {
|
||
4 years ago
|
let wrappedRenderButtonResult;
|
||
6 years ago
|
|
||
4 years ago
|
beforeEach(() => {
|
||
4 years ago
|
GasPriceButtonGroup.prototype.renderButtonContent.resetHistory();
|
||
4 years ago
|
const renderButtonResult = wrapper
|
||
|
.instance()
|
||
|
.renderButton(
|
||
|
{ ...mockGasPriceButtonGroupProps.gasButtonInfo[0] },
|
||
|
mockButtonPropsAndFlags,
|
||
4 years ago
|
);
|
||
4 years ago
|
wrappedRenderButtonResult = shallowWithContext(renderButtonResult);
|
||
4 years ago
|
});
|
||
6 years ago
|
|
||
4 years ago
|
it('should render a button', () => {
|
||
|
expect(wrappedRenderButtonResult.type()).toStrictEqual('button');
|
||
4 years ago
|
});
|
||
6 years ago
|
|
||
4 years ago
|
it('should call the correct method when clicked', () => {
|
||
|
expect(
|
||
4 years ago
|
mockGasPriceButtonGroupProps.handleGasPriceSelection.callCount,
|
||
4 years ago
|
).toStrictEqual(0);
|
||
4 years ago
|
wrappedRenderButtonResult.props().onClick();
|
||
4 years ago
|
expect(
|
||
4 years ago
|
mockGasPriceButtonGroupProps.handleGasPriceSelection.callCount,
|
||
4 years ago
|
).toStrictEqual(1);
|
||
|
expect(
|
||
6 years ago
|
mockGasPriceButtonGroupProps.handleGasPriceSelection.getCall(0).args,
|
||
4 years ago
|
).toStrictEqual([
|
||
|
{
|
||
|
gasPrice: mockGasPriceButtonGroupProps.gasButtonInfo[0].priceInHexWei,
|
||
|
gasEstimateType:
|
||
|
mockGasPriceButtonGroupProps.gasButtonInfo[0].gasEstimateType,
|
||
|
},
|
||
|
]);
|
||
4 years ago
|
});
|
||
6 years ago
|
|
||
4 years ago
|
it('should call this.renderButtonContent with the correct args', () => {
|
||
|
expect(
|
||
4 years ago
|
GasPriceButtonGroup.prototype.renderButtonContent.callCount,
|
||
4 years ago
|
).toStrictEqual(1);
|
||
6 years ago
|
const {
|
||
|
feeInPrimaryCurrency,
|
||
|
feeInSecondaryCurrency,
|
||
|
timeEstimate,
|
||
4 years ago
|
gasEstimateType,
|
||
4 years ago
|
} = mockGasPriceButtonGroupProps.gasButtonInfo[0];
|
||
|
const { showCheck, className } = mockGasPriceButtonGroupProps;
|
||
4 years ago
|
expect(
|
||
6 years ago
|
GasPriceButtonGroup.prototype.renderButtonContent.getCall(0).args,
|
||
4 years ago
|
).toStrictEqual([
|
||
|
{
|
||
|
gasEstimateType,
|
||
|
feeInPrimaryCurrency,
|
||
|
feeInSecondaryCurrency,
|
||
|
timeEstimate,
|
||
|
},
|
||
|
{
|
||
|
showCheck,
|
||
|
className,
|
||
|
},
|
||
|
]);
|
||
4 years ago
|
});
|
||
|
});
|
||
6 years ago
|
|
||
4 years ago
|
describe('renderButtonContent', () => {
|
||
|
it('should render a label if passed a gasEstimateType', () => {
|
||
4 years ago
|
const renderButtonContentResult = wrapper.instance().renderButtonContent(
|
||
|
{
|
||
|
gasEstimateType: 'SLOW',
|
||
|
},
|
||
|
{
|
||
|
className: 'someClass',
|
||
|
},
|
||
4 years ago
|
);
|
||
4 years ago
|
const wrappedRenderButtonContentResult = shallowWithContext(
|
||
4 years ago
|
renderButtonContentResult,
|
||
4 years ago
|
);
|
||
4 years ago
|
expect(
|
||
|
wrappedRenderButtonContentResult.childAt(0).children(),
|
||
|
).toHaveLength(1);
|
||
|
expect(
|
||
4 years ago
|
wrappedRenderButtonContentResult.find('.someClass__label').text(),
|
||
4 years ago
|
).toStrictEqual('slow');
|
||
4 years ago
|
});
|
||
6 years ago
|
|
||
4 years ago
|
it('should render a feeInPrimaryCurrency if passed a feeInPrimaryCurrency', () => {
|
||
4 years ago
|
const renderButtonContentResult = GasPriceButtonGroup.prototype.renderButtonContent(
|
||
|
{
|
||
|
feeInPrimaryCurrency: 'mockFeeInPrimaryCurrency',
|
||
|
},
|
||
|
{
|
||
|
className: 'someClass',
|
||
|
},
|
||
4 years ago
|
);
|
||
4 years ago
|
const wrappedRenderButtonContentResult = shallowWithContext(
|
||
4 years ago
|
renderButtonContentResult,
|
||
4 years ago
|
);
|
||
4 years ago
|
expect(
|
||
|
wrappedRenderButtonContentResult.childAt(0).children(),
|
||
|
).toHaveLength(1);
|
||
|
expect(
|
||
4 years ago
|
wrappedRenderButtonContentResult
|
||
|
.find('.someClass__primary-currency')
|
||
|
.text(),
|
||
4 years ago
|
).toStrictEqual('mockFeeInPrimaryCurrency');
|
||
4 years ago
|
});
|
||
6 years ago
|
|
||
4 years ago
|
it('should render a feeInSecondaryCurrency if passed a feeInSecondaryCurrency', () => {
|
||
4 years ago
|
const renderButtonContentResult = GasPriceButtonGroup.prototype.renderButtonContent(
|
||
|
{
|
||
|
feeInSecondaryCurrency: 'mockFeeInSecondaryCurrency',
|
||
|
},
|
||
|
{
|
||
|
className: 'someClass',
|
||
|
},
|
||
4 years ago
|
);
|
||
4 years ago
|
const wrappedRenderButtonContentResult = shallowWithContext(
|
||
4 years ago
|
renderButtonContentResult,
|
||
4 years ago
|
);
|
||
4 years ago
|
expect(
|
||
|
wrappedRenderButtonContentResult.childAt(0).children(),
|
||
|
).toHaveLength(1);
|
||
|
expect(
|
||
4 years ago
|
wrappedRenderButtonContentResult
|
||
|
.find('.someClass__secondary-currency')
|
||
|
.text(),
|
||
4 years ago
|
).toStrictEqual('mockFeeInSecondaryCurrency');
|
||
4 years ago
|
});
|
||
6 years ago
|
|
||
4 years ago
|
it('should render a timeEstimate if passed a timeEstimate', () => {
|
||
4 years ago
|
const renderButtonContentResult = GasPriceButtonGroup.prototype.renderButtonContent(
|
||
|
{
|
||
|
timeEstimate: 'mockTimeEstimate',
|
||
|
},
|
||
|
{
|
||
|
className: 'someClass',
|
||
|
},
|
||
4 years ago
|
);
|
||
4 years ago
|
const wrappedRenderButtonContentResult = shallowWithContext(
|
||
4 years ago
|
renderButtonContentResult,
|
||
4 years ago
|
);
|
||
4 years ago
|
expect(
|
||
|
wrappedRenderButtonContentResult.childAt(0).children(),
|
||
|
).toHaveLength(1);
|
||
|
expect(
|
||
4 years ago
|
wrappedRenderButtonContentResult
|
||
|
.find('.someClass__time-estimate')
|
||
|
.text(),
|
||
4 years ago
|
).toStrictEqual('mockTimeEstimate');
|
||
4 years ago
|
});
|
||
6 years ago
|
|
||
4 years ago
|
it('should render a check if showCheck is true', () => {
|
||
4 years ago
|
const renderButtonContentResult = GasPriceButtonGroup.prototype.renderButtonContent(
|
||
|
{},
|
||
|
{
|
||
|
className: 'someClass',
|
||
|
showCheck: true,
|
||
|
},
|
||
4 years ago
|
);
|
||
4 years ago
|
const wrappedRenderButtonContentResult = shallowWithContext(
|
||
4 years ago
|
renderButtonContentResult,
|
||
4 years ago
|
);
|
||
4 years ago
|
expect(wrappedRenderButtonContentResult.find('.fa-check')).toHaveLength(
|
||
4 years ago
|
1,
|
||
4 years ago
|
);
|
||
|
});
|
||
6 years ago
|
|
||
4 years ago
|
it('should render all elements if all args passed', () => {
|
||
4 years ago
|
const renderButtonContentResult = wrapper.instance().renderButtonContent(
|
||
|
{
|
||
|
gasEstimateType: 'SLOW',
|
||
|
feeInPrimaryCurrency: 'mockFeeInPrimaryCurrency',
|
||
|
feeInSecondaryCurrency: 'mockFeeInSecondaryCurrency',
|
||
|
timeEstimate: 'mockTimeEstimate',
|
||
|
},
|
||
|
{
|
||
|
className: 'someClass',
|
||
|
showCheck: true,
|
||
|
},
|
||
4 years ago
|
);
|
||
4 years ago
|
const wrappedRenderButtonContentResult = shallowWithContext(
|
||
4 years ago
|
renderButtonContentResult,
|
||
4 years ago
|
);
|
||
4 years ago
|
expect(wrappedRenderButtonContentResult.children()).toHaveLength(5);
|
||
4 years ago
|
});
|
||
6 years ago
|
|
||
4 years ago
|
it('should render no elements if all args passed', () => {
|
||
4 years ago
|
const renderButtonContentResult = GasPriceButtonGroup.prototype.renderButtonContent(
|
||
|
{},
|
||
|
{},
|
||
4 years ago
|
);
|
||
4 years ago
|
const wrappedRenderButtonContentResult = shallowWithContext(
|
||
4 years ago
|
renderButtonContentResult,
|
||
4 years ago
|
);
|
||
4 years ago
|
expect(wrappedRenderButtonContentResult.children()).toHaveLength(0);
|
||
4 years ago
|
});
|
||
|
});
|
||
|
});
|