1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Fix unit tests using shared test doubles (#8235)

These tests broke when `sinon.restore()` was called in a separate test
because they setup stubs/spies using `sinon` in the module context.
These were constructed then restored before the tests even ran.

Instead the test doubles are now setup in the `beforeEach` hook, which
in addition to fixing this problem also ensures each unit test is
isolated from the others.
This commit is contained in:
Mark Stacey 2020-03-25 14:45:23 -03:00 committed by GitHub
parent 2e9d41e013
commit 1e93340c16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 119 additions and 114 deletions

View File

@ -7,17 +7,16 @@ import AdvancedTabContent from '../advanced-tab-content.component.js'
import GasPriceChart from '../../../gas-price-chart' import GasPriceChart from '../../../gas-price-chart'
import Loading from '../../../../../ui/loading-screen' import Loading from '../../../../../ui/loading-screen'
const propsMethodSpies = {
updateCustomGasPrice: sinon.spy(),
updateCustomGasLimit: sinon.spy(),
}
sinon.spy(AdvancedTabContent.prototype, 'renderDataSummary')
describe('AdvancedTabContent Component', function () { describe('AdvancedTabContent Component', function () {
let wrapper let wrapper
beforeEach(function () { beforeEach(function () {
const propsMethodSpies = {
updateCustomGasPrice: sinon.spy(),
updateCustomGasLimit: sinon.spy(),
}
sinon.spy(AdvancedTabContent.prototype, 'renderDataSummary')
wrapper = shallow(( wrapper = shallow((
<AdvancedTabContent <AdvancedTabContent
updateCustomGasPrice={propsMethodSpies.updateCustomGasPrice} updateCustomGasPrice={propsMethodSpies.updateCustomGasPrice}
@ -35,9 +34,7 @@ describe('AdvancedTabContent Component', function () {
}) })
afterEach(function () { afterEach(function () {
propsMethodSpies.updateCustomGasPrice.resetHistory() sinon.restore()
propsMethodSpies.updateCustomGasLimit.resetHistory()
AdvancedTabContent.prototype.renderDataSummary.resetHistory()
}) })
describe('render()', function () { describe('render()', function () {

View File

@ -6,7 +6,13 @@ import GasPriceButtonGroup from '../gas-price-button-group.component'
import ButtonGroup from '../../../../ui/button-group' import ButtonGroup from '../../../../ui/button-group'
const mockGasPriceButtonGroupProps = { describe('GasPriceButtonGroup Component', function () {
let mockButtonPropsAndFlags
let mockGasPriceButtonGroupProps
let wrapper
beforeEach(function () {
mockGasPriceButtonGroupProps = {
buttonDataLoading: false, buttonDataLoading: false,
className: 'gas-price-button-group', className: 'gas-price-button-group',
gasButtonInfo: [ gasButtonInfo: [
@ -35,7 +41,7 @@ const mockGasPriceButtonGroupProps = {
showCheck: true, showCheck: true,
} }
const mockButtonPropsAndFlags = Object.assign({}, { mockButtonPropsAndFlags = Object.assign({}, {
className: mockGasPriceButtonGroupProps.className, className: mockGasPriceButtonGroupProps.className,
handleGasPriceSelection: mockGasPriceButtonGroupProps.handleGasPriceSelection, handleGasPriceSelection: mockGasPriceButtonGroupProps.handleGasPriceSelection,
showCheck: mockGasPriceButtonGroupProps.showCheck, showCheck: mockGasPriceButtonGroupProps.showCheck,
@ -44,10 +50,6 @@ const mockButtonPropsAndFlags = Object.assign({}, {
sinon.spy(GasPriceButtonGroup.prototype, 'renderButton') sinon.spy(GasPriceButtonGroup.prototype, 'renderButton')
sinon.spy(GasPriceButtonGroup.prototype, 'renderButtonContent') sinon.spy(GasPriceButtonGroup.prototype, 'renderButtonContent')
describe('GasPriceButtonGroup Component', function () {
let wrapper
beforeEach(function () {
wrapper = shallow(( wrapper = shallow((
<GasPriceButtonGroup <GasPriceButtonGroup
{...mockGasPriceButtonGroupProps} {...mockGasPriceButtonGroupProps}
@ -56,9 +58,7 @@ describe('GasPriceButtonGroup Component', function () {
}) })
afterEach(function () { afterEach(function () {
GasPriceButtonGroup.prototype.renderButton.resetHistory() sinon.restore()
GasPriceButtonGroup.prototype.renderButtonContent.resetHistory()
mockGasPriceButtonGroupProps.handleGasPriceSelection.resetHistory()
}) })
describe('render', function () { describe('render', function () {

View File

@ -11,11 +11,20 @@ function timeout (time) {
}) })
} }
const propsMethodSpies = { describe('GasPriceChart Component', function () {
let GasPriceChart
let gasPriceChartUtilsSpies
let propsMethodSpies
let selectReturnSpies
let testProps
let wrapper
beforeEach(function () {
propsMethodSpies = {
updateCustomGasPrice: sinon.spy(), updateCustomGasPrice: sinon.spy(),
} }
const selectReturnSpies = { selectReturnSpies = {
empty: sinon.spy(), empty: sinon.spy(),
remove: sinon.spy(), remove: sinon.spy(),
style: sinon.spy(), style: sinon.spy(),
@ -33,7 +42,7 @@ const mockSelectReturn = {
...selectReturnSpies, ...selectReturnSpies,
} }
const gasPriceChartUtilsSpies = { gasPriceChartUtilsSpies = {
appendOrUpdateCircle: sinon.spy(), appendOrUpdateCircle: sinon.spy(),
generateChart: sinon.stub().returns({ mockChart: true }), generateChart: sinon.stub().returns({ mockChart: true }),
generateDataUIObj: sinon.spy(), generateDataUIObj: sinon.spy(),
@ -47,7 +56,7 @@ const gasPriceChartUtilsSpies = {
handleMouseMove: sinon.spy(), handleMouseMove: sinon.spy(),
} }
const testProps = { testProps = {
gasPrices: [1.5, 2.5, 4, 8], gasPrices: [1.5, 2.5, 4, 8],
estimatedTimes: [100, 80, 40, 10], estimatedTimes: [100, 80, 40, 10],
gasPricesMax: 9, gasPricesMax: 9,
@ -56,7 +65,7 @@ const testProps = {
updateCustomGasPrice: propsMethodSpies.updateCustomGasPrice, updateCustomGasPrice: propsMethodSpies.updateCustomGasPrice,
} }
const GasPriceChart = proxyquire('../gas-price-chart.component.js', { GasPriceChart = proxyquire('../gas-price-chart.component.js', {
'./gas-price-chart.utils.js': gasPriceChartUtilsSpies, './gas-price-chart.utils.js': gasPriceChartUtilsSpies,
'd3': { 'd3': {
...d3, ...d3,
@ -71,16 +80,15 @@ const GasPriceChart = proxyquire('../gas-price-chart.component.js', {
}, },
}, },
}).default }).default
sinon.spy(GasPriceChart.prototype, 'renderChart') sinon.spy(GasPriceChart.prototype, 'renderChart')
describe('GasPriceChart Component', function () {
let wrapper
beforeEach(function () {
wrapper = shallow(<GasPriceChart {...testProps} />) wrapper = shallow(<GasPriceChart {...testProps} />)
}) })
afterEach(function () {
sinon.restore()
})
describe('render()', function () { describe('render()', function () {
it('should render', function () { it('should render', function () {
assert(wrapper.hasClass('gas-price-chart')) assert(wrapper.hasClass('gas-price-chart'))