Skip to content

Commit

Permalink
Merge branch 'develop' into 17191/onboarding-unit-tests-create-new-vault
Browse files Browse the repository at this point in the history
  • Loading branch information
tmashuang authored Apr 20, 2023
2 parents f5c3e32 + e96c0b6 commit a7643f9
Show file tree
Hide file tree
Showing 60 changed files with 639 additions and 326 deletions.
29 changes: 29 additions & 0 deletions app/_locales/en/messages.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 23 additions & 23 deletions app/scripts/controllers/network/network-controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6740,7 +6740,7 @@ describe('NetworkController', () => {
it('throws if the given chain ID is not a 0x-prefixed hex number', async () => {
const invalidChainId = '1';
await withController(async ({ controller }) => {
expect(() =>
await expect(() =>
controller.upsertNetworkConfiguration(
{
/* @ts-expect-error We are intentionally passing bad input. */
Expand All @@ -6755,7 +6755,7 @@ describe('NetworkController', () => {
source: MetaMetricsNetworkEventSource.Dapp,
},
),
).toThrow(
).rejects.toThrow(
new Error(
`Invalid chain ID "${invalidChainId}": invalid hex string.`,
),
Expand All @@ -6765,7 +6765,7 @@ describe('NetworkController', () => {

it('throws if the given chain ID is greater than the maximum allowed ID', async () => {
await withController(async ({ controller }) => {
expect(() =>
await expect(() =>
controller.upsertNetworkConfiguration(
{
chainId: '0xFFFFFFFFFFFFFFFF',
Expand All @@ -6779,7 +6779,7 @@ describe('NetworkController', () => {
source: MetaMetricsNetworkEventSource.Dapp,
},
),
).toThrow(
).rejects.toThrow(
new Error(
'Invalid chain ID "0xFFFFFFFFFFFFFFFF": numerical value greater than max safe value.',
),
Expand All @@ -6789,7 +6789,7 @@ describe('NetworkController', () => {

it('throws if the no (or a falsy) rpcUrl is passed', async () => {
await withController(async ({ controller }) => {
expect(() =>
await expect(() =>
controller.upsertNetworkConfiguration(
/* @ts-expect-error We are intentionally passing bad input. */
{
Expand All @@ -6803,7 +6803,7 @@ describe('NetworkController', () => {
source: MetaMetricsNetworkEventSource.Dapp,
},
),
).toThrow(
).rejects.toThrow(
new Error(
'An rpcUrl is required to add or update network configuration',
),
Expand All @@ -6813,7 +6813,7 @@ describe('NetworkController', () => {

it('throws if rpcUrl passed is not a valid Url', async () => {
await withController(async ({ controller }) => {
expect(() =>
await expect(() =>
controller.upsertNetworkConfiguration(
{
chainId: '0x9999',
Expand All @@ -6827,13 +6827,13 @@ describe('NetworkController', () => {
source: MetaMetricsNetworkEventSource.Dapp,
},
),
).toThrow(new Error('rpcUrl must be a valid URL'));
).rejects.toThrow(new Error('rpcUrl must be a valid URL'));
});
});

it('throws if the no (or a falsy) ticker is passed', async () => {
await withController(async ({ controller }) => {
expect(() =>
await expect(() =>
controller.upsertNetworkConfiguration(
/* @ts-expect-error We are intentionally passing bad input. */
{
Expand All @@ -6847,7 +6847,7 @@ describe('NetworkController', () => {
source: MetaMetricsNetworkEventSource.Dapp,
},
),
).toThrow(
).rejects.toThrow(
new Error(
'A ticker is required to add or update networkConfiguration',
),
Expand All @@ -6857,15 +6857,15 @@ describe('NetworkController', () => {

it('throws if an options object is not passed as a second argument', async () => {
await withController(async ({ controller }) => {
expect(() =>
await expect(() =>
/* @ts-expect-error We are intentionally passing bad input. */
controller.upsertNetworkConfiguration({
chainId: '0x5',
nickname: 'RPC',
rpcPrefs: { blockExplorerUrl: 'test-block-explorer.com' },
rpcUrl: 'https://mock-rpc-url',
}),
).toThrow(
).rejects.toThrow(
new Error(
"Cannot read properties of undefined (reading 'setActive')",
),
Expand All @@ -6888,7 +6888,7 @@ describe('NetworkController', () => {
ticker: 'test_ticker',
};

controller.upsertNetworkConfiguration(rpcUrlNetwork, {
await controller.upsertNetworkConfiguration(rpcUrlNetwork, {
referrer: 'https://test-dapp.com',
source: MetaMetricsNetworkEventSource.Dapp,
});
Expand Down Expand Up @@ -6926,7 +6926,7 @@ describe('NetworkController', () => {
invalidKey2: {},
};

controller.upsertNetworkConfiguration(rpcUrlNetwork, {
await controller.upsertNetworkConfiguration(rpcUrlNetwork, {
referrer: 'https://test-dapp.com',
source: MetaMetricsNetworkEventSource.Dapp,
});
Expand Down Expand Up @@ -6975,7 +6975,7 @@ describe('NetworkController', () => {
ticker: 'RPC',
};

controller.upsertNetworkConfiguration(rpcUrlNetwork, {
await controller.upsertNetworkConfiguration(rpcUrlNetwork, {
referrer: 'https://test-dapp.com',
source: MetaMetricsNetworkEventSource.Dapp,
});
Expand Down Expand Up @@ -7024,7 +7024,7 @@ describe('NetworkController', () => {
rpcPrefs: { blockExplorerUrl: 'alternativetestchainscan.io' },
chainId: '0x1' as const,
};
controller.upsertNetworkConfiguration(updatedConfiguration, {
await controller.upsertNetworkConfiguration(updatedConfiguration, {
referrer: 'https://test-dapp.com',
source: MetaMetricsNetworkEventSource.Dapp,
});
Expand Down Expand Up @@ -7069,7 +7069,7 @@ describe('NetworkController', () => {
},
},
async ({ controller }) => {
controller.upsertNetworkConfiguration(
await controller.upsertNetworkConfiguration(
{
rpcUrl: 'https://test-rpc-url',
ticker: 'new-ticker',
Expand Down Expand Up @@ -7136,7 +7136,7 @@ describe('NetworkController', () => {
ticker: 'test_ticker',
};

controller.upsertNetworkConfiguration(rpcUrlNetwork, {
await controller.upsertNetworkConfiguration(rpcUrlNetwork, {
referrer: 'https://test-dapp.com',
source: MetaMetricsNetworkEventSource.Dapp,
});
Expand Down Expand Up @@ -7180,7 +7180,7 @@ describe('NetworkController', () => {
ticker: 'test_ticker',
};

controller.upsertNetworkConfiguration(rpcUrlNetwork, {
await controller.upsertNetworkConfiguration(rpcUrlNetwork, {
setActive: true,
referrer: 'https://test-dapp.com',
source: MetaMetricsNetworkEventSource.Dapp,
Expand Down Expand Up @@ -7229,7 +7229,7 @@ describe('NetworkController', () => {
rpcPrefs: { blockExplorerUrl: 'https://block-explorer' },
};

controller.upsertNetworkConfiguration(newNetworkConfiguration, {
await controller.upsertNetworkConfiguration(newNetworkConfiguration, {
referrer: 'https://test-dapp.com',
source: MetaMetricsNetworkEventSource.Dapp,
});
Expand Down Expand Up @@ -7296,10 +7296,10 @@ describe('NetworkController', () => {
rpcPrefs: { blockExplorerUrl: 'https://block-explorer' },
};

expect(() => {
await expect(() =>
/* @ts-expect-error We are intentionally passing bad input. */
controller.upsertNetworkConfiguration(newNetworkConfiguration, {});
}).toThrow(
controller.upsertNetworkConfiguration(newNetworkConfiguration, {}),
).rejects.toThrow(
'referrer and source are required arguments for adding or updating a network configuration',
);
},
Expand Down
6 changes: 3 additions & 3 deletions app/scripts/controllers/network/network-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,7 @@ export class NetworkController extends EventEmitter {
* @throws if `rpcUrl` is not a valid URL.
* @returns The ID for the added or updated network configuration.
*/
upsertNetworkConfiguration(
async upsertNetworkConfiguration(
{
rpcUrl,
chainId,
Expand All @@ -1051,7 +1051,7 @@ export class NetworkController extends EventEmitter {
referrer: string;
source: string;
},
): NetworkConfigurationId {
): Promise<NetworkConfigurationId> {
assert.ok(
isPrefixedFormattedHexString(chainId),
`Invalid chain ID "${chainId}": invalid hex string.`,
Expand Down Expand Up @@ -1129,7 +1129,7 @@ export class NetworkController extends EventEmitter {
}

if (setActive) {
this.setActiveNetwork(newNetworkConfigurationId);
await this.setActiveNetwork(newNetworkConfigurationId);
}

return newNetworkConfigurationId;
Expand Down
4 changes: 2 additions & 2 deletions app/scripts/controllers/swaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export default class SwapsController {
fetchTradesInfo = defaultFetchTradesInfo,
getCurrentChainId,
getEIP1559GasFeeEstimates,
onNetworkDidChange,
onNetworkStateChange,
}) {
this.store = new ObservableStore({
swapsState: { ...initialState.swapsState },
Expand All @@ -137,7 +137,7 @@ export default class SwapsController {

this.ethersProvider = new Web3Provider(provider);
this._currentNetworkId = networkController.store.getState().networkId;
onNetworkDidChange(() => {
onNetworkStateChange(() => {
const { networkId, networkStatus } = networkController.store.getState();
if (
networkStatus === NetworkStatus.Available &&
Expand Down
20 changes: 10 additions & 10 deletions app/scripts/controllers/swaps.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ describe('SwapsController', function () {
return new SwapsController({
getBufferedGasLimit: MOCK_GET_BUFFERED_GAS_LIMIT,
networkController: getMockNetworkController(),
onNetworkDidChange: sinon.stub(),
onNetworkStateChange: sinon.stub(),
provider: _provider,
getProviderConfig: MOCK_GET_PROVIDER_CONFIG,
getTokenRatesState: MOCK_TOKEN_RATES_STORE,
Expand Down Expand Up @@ -208,19 +208,19 @@ describe('SwapsController', function () {

it('should replace ethers instance when network changes', function () {
const networkController = getMockNetworkController();
const onNetworkDidChange = sinon.stub();
const onNetworkStateChange = sinon.stub();
const swapsController = new SwapsController({
getBufferedGasLimit: MOCK_GET_BUFFERED_GAS_LIMIT,
networkController,
onNetworkDidChange,
onNetworkStateChange,
provider,
getProviderConfig: MOCK_GET_PROVIDER_CONFIG,
getTokenRatesState: MOCK_TOKEN_RATES_STORE,
fetchTradesInfo: fetchTradesInfoStub,
getCurrentChainId: getCurrentChainIdStub,
});
const currentEthersInstance = swapsController.ethersProvider;
const changeNetwork = onNetworkDidChange.getCall(0).args[0];
const changeNetwork = onNetworkStateChange.getCall(0).args[0];

networkController.store.getState.returns({
networkId: NETWORK_IDS.MAINNET,
Expand All @@ -238,19 +238,19 @@ describe('SwapsController', function () {

it('should not replace ethers instance when network changes to loading', function () {
const networkController = getMockNetworkController();
const onNetworkDidChange = sinon.stub();
const onNetworkStateChange = sinon.stub();
const swapsController = new SwapsController({
getBufferedGasLimit: MOCK_GET_BUFFERED_GAS_LIMIT,
networkController,
onNetworkDidChange,
onNetworkStateChange,
provider,
getProviderConfig: MOCK_GET_PROVIDER_CONFIG,
getTokenRatesState: MOCK_TOKEN_RATES_STORE,
fetchTradesInfo: fetchTradesInfoStub,
getCurrentChainId: getCurrentChainIdStub,
});
const currentEthersInstance = swapsController.ethersProvider;
const changeNetwork = onNetworkDidChange.getCall(0).args[0];
const changeNetwork = onNetworkStateChange.getCall(0).args[0];

networkController.store.getState.returns({
networkId: null,
Expand All @@ -268,19 +268,19 @@ describe('SwapsController', function () {

it('should not replace ethers instance when network changes to the same network', function () {
const networkController = getMockNetworkController();
const onNetworkDidChange = sinon.stub();
const onNetworkStateChange = sinon.stub();
const swapsController = new SwapsController({
getBufferedGasLimit: MOCK_GET_BUFFERED_GAS_LIMIT,
networkController,
onNetworkDidChange,
onNetworkStateChange,
provider,
getProviderConfig: MOCK_GET_PROVIDER_CONFIG,
getTokenRatesState: MOCK_TOKEN_RATES_STORE,
fetchTradesInfo: fetchTradesInfoStub,
getCurrentChainId: getCurrentChainIdStub,
});
const currentEthersInstance = swapsController.ethersProvider;
const changeNetwork = onNetworkDidChange.getCall(0).args[0];
const changeNetwork = onNetworkStateChange.getCall(0).args[0];

networkController.store.getState.returns({
networkId: NETWORK_IDS.GOERLI,
Expand Down
6 changes: 2 additions & 4 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -1195,10 +1195,8 @@ export default class MetamaskController extends EventEmitter {
this.txController.txGasUtil,
),
networkController: this.networkController,
onNetworkDidChange: networkControllerMessenger.subscribe.bind(
networkControllerMessenger,
NetworkControllerEventType.NetworkDidChange,
),
onNetworkStateChange: (listener) =>
this.networkController.store.subscribe(listener),
provider: this.provider,
getProviderConfig: () => this.networkController.store.getState().provider,
getTokenRatesState: () => this.tokenRatesController.state,
Expand Down
8 changes: 4 additions & 4 deletions coverage-targets.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
// subset of files to check against these targets.
module.exports = {
global: {
lines: 67.8,
branches: 55.84,
statements: 67.13,
functions: 59.66,
lines: 68.5,
branches: 56.34,
statements: 67.86,
functions: 60.69,
},
transforms: {
branches: 100,
Expand Down
Loading

0 comments on commit a7643f9

Please sign in to comment.