Skip to content

Commit

Permalink
address review, changed migration 024 test file for not use the initi…
Browse files Browse the repository at this point in the history
…al background state, adapt 36 migration to check every state data first and only then modify the data and updated the tests accordingly, updated fixture-builder with networkId property
  • Loading branch information
tommasini committed Mar 19, 2024
1 parent 7e59132 commit 9454fd2
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 50 deletions.
35 changes: 4 additions & 31 deletions app/store/migrations/024.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import migrate from './024';
import { merge } from 'lodash';
import initialRootState from '../../util/test/initial-root-state';
import initialBackgroundState from '../../util/test/initial-background-state.json';
import { captureException } from '@sentry/react-native';

jest.mock('@sentry/react-native', () => ({
Expand Down Expand Up @@ -54,64 +53,38 @@ describe('Migration #24', () => {
it('should migrate loading network state', () => {
const state = {
engine: {
backgroundState: merge({}, initialBackgroundState, {
backgroundState: {
NetworkController: {
network: 'loading',
},
}),
},
},
};

const newState = migrate(state);

expect(newState.engine.backgroundState.NetworkController).toStrictEqual({
networkConfigurations: {},
networkId: null,
networkStatus: 'unknown',
providerConfig: {
chainId: '0x1',
ticker: 'ETH',
type: 'mainnet',
},
networksMetadata: {
mainnet: {
EIPS: {},
status: 'unknown',
},
},
selectedNetworkClientId: 'mainnet',
});
});

it('should migrate non-loading network state', () => {
const state = {
engine: {
backgroundState: merge({}, initialBackgroundState, {
backgroundState: {
NetworkController: {
network: '1',
},
}),
},
},
};

const newState = migrate(state);

expect(newState.engine.backgroundState.NetworkController).toStrictEqual({
networkConfigurations: {},
networkId: '1',
networkStatus: 'available',
providerConfig: {
chainId: '0x1',
type: 'mainnet',
ticker: 'ETH',
},
networksMetadata: {
mainnet: {
EIPS: {},
status: 'unknown',
},
},
selectedNetworkClientId: 'mainnet',
});
});
});
61 changes: 59 additions & 2 deletions app/store/migrations/036.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,33 @@ const expectedState = {
engine: {
backgroundState: {
NetworkController: {
networkConfigurations: {},
networksMetadata: {
goerli: {
EIPS: {},
status: 'unknown',
},
'linea-goerli': {
EIPS: {},
status: 'unknown',
},
'linea-mainnet': {
EIPS: {},
status: 'unknown',
},
'linea-sepolia': {
EIPS: {},
status: 'unknown',
},
mainnet: {
EIPS: {},
status: 'unknown',
},
sepolia: {
EIPS: {},
status: 'unknown',
},
},
selectedNetworkClientId: 'mainnet',
providerConfig: {
type: 'mainnet',
Expand Down Expand Up @@ -119,6 +146,7 @@ describe('Migration #36', () => {
engine: {
backgroundState: {
NetworkController: {
networkConfigurations: {},
networkDetails: {},
networkStatus: 'test',
providerConfig: {
Expand All @@ -140,6 +168,7 @@ describe('Migration #36', () => {
engine: {
backgroundState: {
NetworkController: {
networkConfigurations: {},
networkDetails: {},
networkStatus: 'test',
providerConfig: {
Expand All @@ -158,6 +187,33 @@ describe('Migration #36', () => {
engine: {
backgroundState: {
NetworkController: {
networkConfigurations: {},
networksMetadata: {
goerli: {
EIPS: {},
status: 'unknown',
},
'linea-goerli': {
EIPS: {},
status: 'unknown',
},
'linea-mainnet': {
EIPS: {},
status: 'unknown',
},
'linea-sepolia': {
EIPS: {},
status: 'unknown',
},
mainnet: {
EIPS: {},
status: 'unknown',
},
sepolia: {
EIPS: {},
status: 'unknown',
},
},
providerConfig: {
type: 'rpc',
chainId: '0x1',
Expand All @@ -180,6 +236,7 @@ describe('Migration #36', () => {
NetworkController: {
networkDetails: {},
networkStatus: 'test',
networkConfigurations: {},
providerConfig: {
type: 'mainnet',
chainId: '0x1',
Expand All @@ -204,7 +261,7 @@ describe('Migration #36', () => {
chainId: '0xa86a',
id: '52b62bd0-296c-41d0-9772-2f418c9e81ef',
nickname: 'Avalanche Mainnet C-Chain',
rpcPrefs: ['Object'],
rpcPrefs: [''],
rpcUrl: 'https://api.avax.network/ext/bc/C/rpc',
ticker: 'AVAX',
},
Expand Down Expand Up @@ -232,7 +289,7 @@ describe('Migration #36', () => {
chainId: '0xa86a',
id: '52b62bd0-296c-41d0-9772-2f418c9e81ef',
nickname: 'Avalanche Mainnet C-Chain',
rpcPrefs: ['Object'],
rpcPrefs: [''],
rpcUrl: 'https://api.avax.network/ext/bc/C/rpc',
ticker: 'AVAX',
},
Expand Down
36 changes: 19 additions & 17 deletions app/store/migrations/036.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { InfuraNetworkType } from '@metamask/controller-utils';

/**
* This migration removes networkDetails and networkStatus property
* This migration add a new property `networkMetadata` to the NetworkController (Still under investigation if it's needed)
* This migration add a new property `networkMetadata` to the NetworkController
* This migrations adds a new property called `selectedNetworkClientId` to the NetworkController
* @param {unknown} stateAsync - Promise Redux state.
* @returns Migrated Redux state.
*/
Expand Down Expand Up @@ -62,11 +63,16 @@ export default async function migrate(stateAsync: unknown) {
return state;
}

if (networkControllerState.networkDetails) {
delete networkControllerState.networkDetails;
}
if (networkControllerState.networkStatus) {
delete networkControllerState.networkStatus;
if (
!isObject(networkControllerState.networkConfigurations) ||
!hasProperty(networkControllerState, 'networkConfigurations')
) {
captureException(
new Error(
`Migration 36: Invalid NetworkController networkConfigurations state: '${typeof networkControllerState.networkConfigurations}'`,
),
);
return state;
}

if (
Expand All @@ -81,6 +87,13 @@ export default async function migrate(stateAsync: unknown) {
return state;
}

if (networkControllerState.networkDetails) {
delete networkControllerState.networkDetails;
}
if (networkControllerState.networkStatus) {
delete networkControllerState.networkStatus;
}

newNetworkControllerState.selectedNetworkClientId =
(networkControllerState.providerConfig.id as string | undefined) ??
(networkControllerState.providerConfig.type as string);
Expand All @@ -95,17 +108,6 @@ export default async function migrate(stateAsync: unknown) {
};
});

if (
!isObject(networkControllerState.networkConfigurations) ||
!hasProperty(networkControllerState, 'networkConfigurations')
) {
captureException(
new Error(
`Migration 36: Invalid NetworkController networkConfigurations state: '${typeof networkControllerState.networkConfigurations}'`,
),
);
return state;
}
Object.keys(networkControllerState.networkConfigurations).forEach(
(networkConfigurationId) => {
customNetworksMetadata = {
Expand Down
1 change: 1 addition & 0 deletions e2e/fixtures/fixture-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ class FixtureBuilder {
},
NetworkController: {
selectedNetworkClientId: 'mainnet',
networkId: '1',
providerConfig: {
type: 'mainnet',
chainId: '0x1',
Expand Down

0 comments on commit 9454fd2

Please sign in to comment.