-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add migration for updated GasFeeController
- Loading branch information
Showing
3 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import migration from './042'; | ||
import { merge } from 'lodash'; | ||
import initialRootState from '../../util/test/initial-root-state'; | ||
import { captureException } from '@sentry/react-native'; | ||
|
||
const oldState = { | ||
engine: { | ||
backgroundState: { | ||
GasFeeController: { | ||
gasFeeEstimates: {}, | ||
estimatedGasFeeTimeBounds: {}, | ||
gasEstimateType: 'none', | ||
gasFeeEstimatesByChainId: {}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const expectedNewState = { | ||
engine: { | ||
backgroundState: { | ||
GasFeeController: { | ||
gasFeeEstimates: {}, | ||
estimatedGasFeeTimeBounds: {}, | ||
gasEstimateType: 'none', | ||
gasFeeEstimatesByChainId: {}, | ||
nonRPCGasFeeApisDisabled: false, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
jest.mock('@sentry/react-native', () => ({ | ||
captureException: jest.fn(), | ||
})); | ||
const mockedCaptureException = jest.mocked(captureException); | ||
|
||
describe('Migration #42', () => { | ||
beforeEach(() => { | ||
jest.restoreAllMocks(); | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
const invalidStates = [ | ||
{ | ||
state: merge({}, initialRootState, { | ||
engine: null, | ||
}), | ||
errorMessage: | ||
"FATAL ERROR: Migration 42: Invalid engine state error: 'object'", | ||
scenario: 'engine state is invalid', | ||
}, | ||
{ | ||
state: merge({}, initialRootState, { | ||
engine: { | ||
backgroundState: null, | ||
}, | ||
}), | ||
errorMessage: | ||
"FATAL ERROR: Migration 42: Invalid engine backgroundState error: 'object'", | ||
scenario: 'backgroundState is invalid', | ||
}, | ||
{ | ||
state: merge({}, initialRootState, { | ||
engine: { | ||
backgroundState: { | ||
GasFeeController: null, | ||
}, | ||
}, | ||
}), | ||
errorMessage: | ||
"FATAL ERROR: Migration 42: Invalid GasFeeController state error: 'null'", | ||
scenario: 'GasFeeController state is invalid', | ||
}, | ||
]; | ||
|
||
for (const { errorMessage, scenario, state } of invalidStates) { | ||
it(`should capture exception if ${scenario}`, async () => { | ||
const newState = await migration(state); | ||
|
||
expect(newState).toStrictEqual(state); | ||
expect(mockedCaptureException).toHaveBeenCalledWith(expect.any(Error)); | ||
expect(mockedCaptureException.mock.calls[0][0].message).toBe( | ||
errorMessage, | ||
); | ||
}); | ||
} | ||
|
||
it('should contain new property nonRPCGasFeeApisDisabled = false in GasFeeController state ', async () => { | ||
const newState = await migration(oldState); | ||
expect(newState).toStrictEqual(expectedNewState); | ||
|
||
expect( | ||
// @ts-expect-error: ignore for testing purposes: new state is type unknown | ||
newState.engine.backgroundState.GasFeeController.nonRPCGasFeeApisDisabled, | ||
).toEqual(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { captureException } from '@sentry/react-native'; | ||
import { isObject } from '@metamask/utils'; | ||
import { ensureValidState } from './util'; | ||
|
||
/** | ||
* Migration to update state of GasFeeController | ||
* | ||
* @param state Persisted Redux state | ||
* @returns | ||
*/ | ||
export default function migrate(state: unknown) { | ||
if (!ensureValidState(state, 42)) { | ||
return state; | ||
} | ||
|
||
const gasFeeControllerState = state.engine.backgroundState.GasFeeController; | ||
|
||
if (!isObject(gasFeeControllerState)) { | ||
captureException( | ||
new Error( | ||
`FATAL ERROR: Migration 42: Invalid GasFeeController state error: '${JSON.stringify( | ||
gasFeeControllerState, | ||
)}'`, | ||
), | ||
); | ||
return state; | ||
} | ||
|
||
gasFeeControllerState.nonRPCGasFeeApisDisabled = false; | ||
|
||
return state; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters