1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 01:47:00 +01:00

Use nock to intercept gas call for gas-duck test (#10334)

* Use nock in to intercept gas call for gas-duck test

Use nock instead of stubbing out the window fetch. My suspicion is that when the test runs the window sometimes wouldn't be intialized/referenced, which could explain the intermittent test failures in those particular tests that call window.fetch.
Using nock allows the call to be intercepted more reliably.
This commit is contained in:
Thomas Huang 2021-02-02 13:14:03 -08:00 committed by GitHub
parent 96933b3faf
commit df5f789391
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,5 @@
import assert from 'assert'
import nock from 'nock'
import sinon from 'sinon'
import proxyquire from 'proxyquire'
@ -20,35 +21,24 @@ const {
const GasReducer = GasDuck.default
describe('Gas Duck', function () {
let tempFetch
let tempDateNow
const mockGasPriceApiResponse = {
SafeGasPrice: 10,
ProposeGasPrice: 20,
FastGasPrice: 30,
}
const fakeFetch = () =>
new Promise((resolve) => {
const dataToResolve = mockGasPriceApiResponse
resolve({
json: () => Promise.resolve(dataToResolve),
})
})
beforeEach(function () {
tempFetch = window.fetch
tempDateNow = global.Date.now
fakeStorage.getStorageItem = sinon.stub()
fakeStorage.setStorageItem = sinon.spy()
window.fetch = sinon.stub().callsFake(fakeFetch)
global.Date.now = () => 2000000
})
afterEach(function () {
sinon.restore()
window.fetch = tempFetch
global.Date.now = tempDateNow
})
@ -166,6 +156,12 @@ describe('Gas Duck', function () {
it('should call fetch with the expected params', async function () {
const mockDistpatch = sinon.spy()
const windowFetchSpy = sinon.spy(window, 'fetch')
nock('https://api.metaswap.codefi.network')
.get('/gasPrices')
.reply(200, mockGasPriceApiResponse)
await fetchBasicGasEstimates()(mockDistpatch, () => ({
gas: { ...initState, basicPriceAEstimatesLastRetrieved: 1000000 },
}))
@ -173,7 +169,7 @@ describe('Gas Duck', function () {
{ type: BASIC_GAS_ESTIMATE_LOADING_STARTED },
])
assert.ok(
window.fetch
windowFetchSpy
.getCall(0)
.args[0].startsWith('https://api.metaswap.codefi.network/gasPrices'),
'should fetch metaswap /gasPrices',
@ -198,6 +194,9 @@ describe('Gas Duck', function () {
it('should fetch recently retrieved estimates from storage', async function () {
const mockDistpatch = sinon.spy()
const windowFetchSpy = sinon.spy(window, 'fetch')
fakeStorage.getStorageItem
.withArgs('BASIC_PRICE_ESTIMATES_LAST_RETRIEVED')
.returns(2000000 - 1) // one second ago from "now"
@ -213,7 +212,7 @@ describe('Gas Duck', function () {
assert.deepStrictEqual(mockDistpatch.getCall(0).args, [
{ type: BASIC_GAS_ESTIMATE_LOADING_STARTED },
])
assert.ok(window.fetch.notCalled)
assert.ok(windowFetchSpy.notCalled)
assert.deepStrictEqual(mockDistpatch.getCall(1).args, [
{
type: SET_BASIC_GAS_ESTIMATE_DATA,
@ -231,6 +230,13 @@ describe('Gas Duck', function () {
it('should fallback to network if retrieving estimates from storage fails', async function () {
const mockDistpatch = sinon.spy()
const windowFetchSpy = sinon.spy(window, 'fetch')
nock('https://api.metaswap.codefi.network')
.get('/gasPrices')
.reply(200, mockGasPriceApiResponse)
fakeStorage.getStorageItem
.withArgs('BASIC_PRICE_ESTIMATES_LAST_RETRIEVED')
.returns(2000000 - 1) // one second ago from "now"
@ -242,11 +248,12 @@ describe('Gas Duck', function () {
{ type: BASIC_GAS_ESTIMATE_LOADING_STARTED },
])
assert.ok(
window.fetch
windowFetchSpy
.getCall(0)
.args[0].startsWith('https://api.metaswap.codefi.network/gasPrices'),
'should fetch metaswap /gasPrices',
)
assert.deepStrictEqual(mockDistpatch.getCall(1).args, [
{ type: SET_BASIC_PRICE_ESTIMATES_LAST_RETRIEVED, value: 2000000 },
])