Skip to content

Commit

Permalink
Remove eslint overrides, add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmire committed Oct 4, 2021
1 parent 884a9f9 commit 5867e8f
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 106 deletions.
38 changes: 0 additions & 38 deletions .eslintrc.js
Expand Up @@ -14,44 +14,6 @@ module.exports = {
{
files: ['*.test.ts', '*.test.js'],
extends: ['@metamask/eslint-config-jest'],
rules: {
'jsdoc/check-access': 'off',
'jsdoc/check-alignment': 'off',
'jsdoc/check-line-alignment': 'off',
'jsdoc/check-param-names': 'off',
'jsdoc/check-property-names': 'off',
'jsdoc/check-tag-names': 'off',
'jsdoc/check-types': 'off',
'jsdoc/check-values': 'off',
'jsdoc/empty-tags': 'off',
'jsdoc/implements-on-classes': 'off',
'jsdoc/match-description': 'off',
'jsdoc/multiline-blocks': 'off',
'jsdoc/newline-after-description': 'off',
'jsdoc/no-bad-blocks': 'off',
'jsdoc/no-defaults': 'off',
'jsdoc/no-multi-asterisks': 'off',
'jsdoc/require-asterisk-prefix': 'off',
'jsdoc/require-description': 'off',
'jsdoc/require-hyphen-before-param-description': 'off',
'jsdoc/require-jsdoc': 'off',
'jsdoc/require-param-name': 'off',
'jsdoc/require-param': 'off',
'jsdoc/require-param-description': 'off',
'jsdoc/require-param-type': 'off',
'jsdoc/require-property': 'off',
'jsdoc/require-property-description': 'off',
'jsdoc/require-property-name': 'off',
'jsdoc/require-property-type': 'off',
'jsdoc/require-returns': 'off',
'jsdoc/require-returns-check': 'off',
'jsdoc/require-returns-description': 'off',
'jsdoc/require-returns-type': 'off',
'jsdoc/require-yields': 'off',
'jsdoc/require-yields-check': 'off',
'jsdoc/tag-lines': 'off',
'jsdoc/valid-types': 'off',
},
},
{
files: ['*.js'],
Expand Down
153 changes: 110 additions & 43 deletions src/gas/GasFeeController.test.ts
Expand Up @@ -4,7 +4,10 @@ import {
GasFeeController,
GetGasFeeState,
GasFeeStateChange,
GasFeeEstimates,
LegacyGasPriceEstimate,
} from './GasFeeController';
import { FeeHistory } from './fetchFeeHistory';
import { calculateTimeEstimate } from './gas-util';

jest.mock('./gas-util', () => {
Expand Down Expand Up @@ -43,7 +46,20 @@ function getRestrictedMessenger() {
return messenger;
}

function buildReturnValueForFetchGasEstimates({ modifier = 1 } = {}) {
/**
* Builds mock data for the `fetchGasEstimates` function you can pass to GasFeeController. All of
* the values here are filled in in order to satisfy the gas fee estimation code in GasFeeController
* and do not necessarily represent real-world scenarios.
*
* @param args - The arguments.
* @param args.modifier - A number you can use to build a unique response in the event that you need
* to mock multiple invocations of `fetchGasEstimates`. All data points will be multiplied by this
* number.
* @returns The mock data.
*/
function buildMockDataForFetchGasEstimates({
modifier = 1,
} = {}): GasFeeEstimates {
return {
low: {
minWaitTimeEstimate: 10000 * modifier,
Expand All @@ -67,37 +83,68 @@ function buildReturnValueForFetchGasEstimates({ modifier = 1 } = {}) {
};
}

function buildReturnValueForLegacyFetchGasPriceEstimates({
/**
* Builds mock data for the `legacyFetchGasPriceEstimates` function you can pass to
* GasFeeController. All of the values here are filled in in order to satisfy the gas fee estimation
* code in GasFeeController and do not necessarily represent real-world scenarios.
*
* @param args - The arguments.
* @param args.modifier - A number you can use to build a unique response in the event that you need
* to mock multiple invocations of `legacyFetchGasPriceEstimates`. All data points will be
* multiplied by this number.
* @returns The mock data.
*/
function buildMockDataForLegacyFetchGasPriceEstimates({
modifier = 1,
} = {}) {
} = {}): LegacyGasPriceEstimate {
return {
SafeGasPrice: (10 * modifier).toString(),
ProposeGasPrice: (20 * modifier).toString(),
FastGasPrice: (30 * modifier).toString(),
low: (10 * modifier).toString(),
medium: (20 * modifier).toString(),
high: (30 * modifier).toString(),
};
}

function buildReturnValueForFetchFeeHistory({
/**
* Builds mock data for the `fetchFeeHistory` function you can pass to GasFeeController. All of the
* values here are filled in in order to satisfy the network congestion gauge code in
* GasFeeController and do not necessarily represent real-world scenarios.
*
* @param args - The arguments.
* @param args.isNetworkCongested - Specifies whether the mock data should represent whether the
* network is congested or not.
* @returns The mock data.
*/
function buildMockDataForFetchFeeHistory({
isNetworkCongested = false,
} = {}) {
} = {}): FeeHistory {
if (isNetworkCongested) {
return {
startBlockId: '0x1',
blocks: [
{
baseFeePerGas: 1,
gasUsedRatio: 1,
priorityFeesByPercentile: new Map([[50, 100]]),
},
{
baseFeePerGas: 1,
gasUsedRatio: 1,
priorityFeesByPercentile: new Map([[50, 200]]),
},
],
};
}
return {
startBlockId: '0x1',
blocks: [
{
baseFeePerGas: 1,
gasUsedRatio: 1,
priorityFeesByPercentile: new Map([[50, 100]]),
},
{
baseFeePerGas: 1,
gasUsedRatio: 1,
priorityFeesByPercentile: new Map([[50, 100]]),
},
],
Expand All @@ -111,17 +158,37 @@ describe('GasFeeController', () => {
let fetchLegacyGasPriceEstimates: jest.Mock<any>;
let fetchFeeHistory: jest.Mock<any>;

/**
* Builds an instance of GasFeeController for use in testing.
*
* @param options - The options.
* @param options.getChainId - Sets getChainId on the GasFeeController.
* @param options.getIsEIP1559Compatible - Sets getCurrentNetworkEIP1559Compatibility on the
* GasFeeController.
* @param options.getCurrentNetworkLegacyGasAPICompatibility - Sets
* getCurrentNetworkLegacyGasAPICompatibility on the GasFeeController.
* @param options.mockReturnValuesForFetchGasEstimates - Specifies mock data for one or more
* invocations of `fetchGasEstimates`.
* @param options.mockReturnValuesForFetchLegacyGasPriceEstimates - Specifies mock data for one or more
* invocations of `fetchLegacyGasPriceEstimates`.
* @param options.mockReturnValuesForFetchFeeHistory - Specifies mock data for one or more
* invocations of `fetchFeeHistory.`
* @param options.legacyAPIEndpoint - Sets legacyAPIEndpoint on the GasFeeController.
* @param options.EIP1559APIEndpoint - Sets EIP1559APIEndpoint on the GasFeeController.
* @param options.clientId - Sets clientId on the GasFeeController.
* @returns The gas fee controller.
*/
function buildGasFeeController({
getChainId = jest.fn(() => '0x1'),
getIsEIP1559Compatible = jest.fn(() => Promise.resolve(true)),
getCurrentNetworkLegacyGasAPICompatibility = jest.fn(() => false),
mockReturnValuesForFetchGasEstimates = [
buildReturnValueForFetchGasEstimates(),
buildMockDataForFetchGasEstimates(),
],
mockReturnValuesForFetchLegacyGasPriceEstimates = [
buildReturnValueForLegacyFetchGasPriceEstimates(),
buildMockDataForLegacyFetchGasPriceEstimates(),
],
mockReturnValuesForFetchFeeHistory = [buildReturnValueForFetchFeeHistory()],
mockReturnValuesForFetchFeeHistory = [buildMockDataForFetchFeeHistory()],
legacyAPIEndpoint = 'http://legacy.endpoint/<chain_id>',
EIP1559APIEndpoint = 'http://eip-1559.endpoint/<chain_id>',
clientId,
Expand Down Expand Up @@ -221,8 +288,8 @@ describe('GasFeeController', () => {
it('should not fetch estimates if the controller is already polling, and should still return the passed token', async () => {
({ gasFeeController, fetchGasEstimates } = buildGasFeeController({
mockReturnValuesForFetchGasEstimates: [
buildReturnValueForFetchGasEstimates(),
buildReturnValueForFetchGasEstimates(),
buildMockDataForFetchGasEstimates(),
buildMockDataForFetchGasEstimates(),
],
}));
const result1 = await gasFeeController.getGasFeeEstimatesAndStartPolling(
Expand All @@ -240,8 +307,8 @@ describe('GasFeeController', () => {
it('should cause the fetching new estimates if called after the poll tokens are cleared, and then should not cause additional new fetches when subsequently called', async () => {
({ gasFeeController, fetchGasEstimates } = buildGasFeeController({
mockReturnValuesForFetchGasEstimates: [
buildReturnValueForFetchGasEstimates(),
buildReturnValueForFetchGasEstimates(),
buildMockDataForFetchGasEstimates(),
buildMockDataForFetchGasEstimates(),
],
}));
const pollToken = 'token';
Expand Down Expand Up @@ -271,7 +338,7 @@ describe('GasFeeController', () => {
describe('gasFeeEstimates', () => {
it('should update the state with a fetched set of estimates', async () => {
const mockReturnValuesForFetchGasEstimates = [
buildReturnValueForFetchGasEstimates(),
buildMockDataForFetchGasEstimates(),
];
({ gasFeeController } = buildGasFeeController({
mockReturnValuesForFetchGasEstimates,
Expand All @@ -294,8 +361,8 @@ describe('GasFeeController', () => {

it('should continue updating the state with all estimate data (including new time estimates because of a subsequent request) on a set interval', async () => {
const mockReturnValuesForFetchGasEstimates = [
buildReturnValueForFetchGasEstimates({ modifier: 1 }),
buildReturnValueForFetchGasEstimates({ modifier: 1.5 }),
buildMockDataForFetchGasEstimates({ modifier: 1 }),
buildMockDataForFetchGasEstimates({ modifier: 1.5 }),
];
({ gasFeeController } = buildGasFeeController({
mockReturnValuesForFetchGasEstimates,
Expand Down Expand Up @@ -324,8 +391,8 @@ describe('GasFeeController', () => {
it('should not make the request to fetch estimates a second time if called twice', async () => {
({ gasFeeController, fetchGasEstimates } = buildGasFeeController({
mockReturnValuesForFetchGasEstimates: [
buildReturnValueForFetchGasEstimates(),
buildReturnValueForFetchGasEstimates(),
buildMockDataForFetchGasEstimates(),
buildMockDataForFetchGasEstimates(),
],
}));

Expand All @@ -338,8 +405,8 @@ describe('GasFeeController', () => {
it('should not add the request to fetch estimates to the polling queue a second time if called twice', async () => {
({ gasFeeController, fetchGasEstimates } = buildGasFeeController({
mockReturnValuesForFetchGasEstimates: [
buildReturnValueForFetchGasEstimates(),
buildReturnValueForFetchGasEstimates(),
buildMockDataForFetchGasEstimates(),
buildMockDataForFetchGasEstimates(),
],
}));

Expand All @@ -354,7 +421,7 @@ describe('GasFeeController', () => {
describe('isNetworkCongested', () => {
it('should update the state with whether the network is congested', async () => {
const mockReturnValuesForFetchFeeHistory = [
buildReturnValueForFetchFeeHistory({ isNetworkCongested: true }),
buildMockDataForFetchFeeHistory({ isNetworkCongested: true }),
];
({ gasFeeController } = buildGasFeeController({
mockReturnValuesForFetchFeeHistory,
Expand All @@ -369,8 +436,8 @@ describe('GasFeeController', () => {

it('should continue updating the state on a set interval', async () => {
const mockReturnValuesForFetchFeeHistory = [
buildReturnValueForFetchFeeHistory({ isNetworkCongested: true }),
buildReturnValueForFetchFeeHistory({ isNetworkCongested: false }),
buildMockDataForFetchFeeHistory({ isNetworkCongested: true }),
buildMockDataForFetchFeeHistory({ isNetworkCongested: false }),
];
({ gasFeeController } = buildGasFeeController({
mockReturnValuesForFetchFeeHistory,
Expand All @@ -387,8 +454,8 @@ describe('GasFeeController', () => {
it('should not make the request to fetch fee history a second time if called twice', async () => {
({ gasFeeController, fetchFeeHistory } = buildGasFeeController({
mockReturnValuesForFetchFeeHistory: [
buildReturnValueForFetchFeeHistory(),
buildReturnValueForFetchFeeHistory(),
buildMockDataForFetchFeeHistory(),
buildMockDataForFetchFeeHistory(),
],
}));

Expand All @@ -406,8 +473,8 @@ describe('GasFeeController', () => {
it('should not add the request to fetch fee history to the polling queue a second time if called twice', async () => {
({ gasFeeController, fetchFeeHistory } = buildGasFeeController({
mockReturnValuesForFetchFeeHistory: [
buildReturnValueForFetchFeeHistory(),
buildReturnValueForFetchFeeHistory(),
buildMockDataForFetchFeeHistory(),
buildMockDataForFetchFeeHistory(),
],
}));

Expand All @@ -429,10 +496,10 @@ describe('GasFeeController', () => {
it('should remove the given item from the polling queue such that a second call to updateWithAndStartPollingFor with the same item has the same effect as the very first invocation', async () => {
({ gasFeeController, fetchGasEstimates } = buildGasFeeController({
mockReturnValuesForFetchGasEstimates: [
buildReturnValueForFetchGasEstimates(),
buildReturnValueForFetchGasEstimates(),
buildReturnValueForFetchGasEstimates(),
buildReturnValueForFetchGasEstimates(),
buildMockDataForFetchGasEstimates(),
buildMockDataForFetchGasEstimates(),
buildMockDataForFetchGasEstimates(),
buildMockDataForFetchGasEstimates(),
],
}));
await gasFeeController.updateWithAndStartPollingFor('gasFeeEstimates');
Expand All @@ -449,10 +516,10 @@ describe('GasFeeController', () => {
it('should not affect different items than those passed to stopPollingFor', async () => {
({ gasFeeController, fetchGasEstimates } = buildGasFeeController({
mockReturnValuesForFetchGasEstimates: [
buildReturnValueForFetchGasEstimates(),
buildReturnValueForFetchGasEstimates(),
buildReturnValueForFetchGasEstimates(),
buildReturnValueForFetchGasEstimates(),
buildMockDataForFetchGasEstimates(),
buildMockDataForFetchGasEstimates(),
buildMockDataForFetchGasEstimates(),
buildMockDataForFetchGasEstimates(),
],
}));
await gasFeeController.updateWithAndStartPollingFor('gasFeeEstimates');
Expand All @@ -478,10 +545,10 @@ describe('GasFeeController', () => {
it('should clear the polling queue such that a second call to updateWithAndStartPollingFor has the same behavior as the very first invocation', async () => {
({ gasFeeController, fetchGasEstimates } = buildGasFeeController({
mockReturnValuesForFetchGasEstimates: [
buildReturnValueForFetchGasEstimates(),
buildReturnValueForFetchGasEstimates(),
buildReturnValueForFetchGasEstimates(),
buildReturnValueForFetchGasEstimates(),
buildMockDataForFetchGasEstimates(),
buildMockDataForFetchGasEstimates(),
buildMockDataForFetchGasEstimates(),
buildMockDataForFetchGasEstimates(),
],
}));
await gasFeeController.updateWithAndStartPollingFor('gasFeeEstimates');
Expand All @@ -504,7 +571,7 @@ describe('GasFeeController', () => {
describe('_fetchGasFeeEstimateData', () => {
describe('when on any network supporting legacy gas estimation api', () => {
const mockReturnValuesForFetchLegacyGasPriceEstimates = [
buildReturnValueForLegacyFetchGasPriceEstimates(),
buildMockDataForLegacyFetchGasPriceEstimates(),
];
const defaultConstructorOptions = {
getIsEIP1559Compatible: jest.fn(() => Promise.resolve(false)),
Expand Down Expand Up @@ -587,7 +654,7 @@ describe('GasFeeController', () => {
describe('when on any network supporting EIP-1559', () => {
it('should return estimates', async () => {
const mockReturnValuesForFetchGasEstimates = [
buildReturnValueForFetchGasEstimates(),
buildMockDataForFetchGasEstimates(),
];
({ gasFeeController } = buildGasFeeController({
getIsEIP1559Compatible: jest.fn(() => Promise.resolve(true)),
Expand Down

0 comments on commit 5867e8f

Please sign in to comment.