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

Add getProviderConfig action to NetworkController (#19317)

This controller action exists within the core version of
NetworkController. Although there are no plans to make use of this
action within the extension, it has accompanying tests. So ultimately,
the goal of this commit (like others) is to make it easier to compare
differences in the NetworkController tests between this repo and core
by adding a test for the `getProviderConfig` action.
This commit is contained in:
Elliot Winkler 2023-05-31 09:29:59 -06:00 committed by GitHub
parent 84d22e122c
commit cc2e18b16a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 3 deletions

View File

@ -18,6 +18,7 @@ import {
import { MetaMetricsNetworkEventSource } from '../../../../shared/constants/metametrics'; import { MetaMetricsNetworkEventSource } from '../../../../shared/constants/metametrics';
import { import {
NetworkController, NetworkController,
NetworkControllerAction,
NetworkControllerEvent, NetworkControllerEvent,
NetworkControllerOptions, NetworkControllerOptions,
NetworkControllerState, NetworkControllerState,
@ -2451,6 +2452,31 @@ describe('NetworkController', () => {
}); });
}); });
describe('NetworkController:getProviderConfig action', () => {
it('returns the provider config in state', async () => {
await withController(
{
state: {
providerConfig: buildProviderConfig({
type: NETWORK_TYPES.MAINNET,
}),
},
},
async ({ messenger }) => {
const providerConfig = await messenger.call(
'NetworkController:getProviderConfig',
);
expect(providerConfig).toStrictEqual(
buildProviderConfig({
type: NETWORK_TYPES.MAINNET,
}),
);
},
);
});
});
describe('rollbackToPreviousProvider', () => { describe('rollbackToPreviousProvider', () => {
for (const { networkType } of INFURA_NETWORKS) { for (const { networkType } of INFURA_NETWORKS) {
describe(`if the previous provider configuration had a type of "${networkType}"`, () => { describe(`if the previous provider configuration had a type of "${networkType}"`, () => {
@ -6088,7 +6114,10 @@ function lookupNetworkTests({
* @returns The controller messenger. * @returns The controller messenger.
*/ */
function buildMessenger() { function buildMessenger() {
return new ControllerMessenger<never, NetworkControllerEvent>(); return new ControllerMessenger<
NetworkControllerAction,
NetworkControllerEvent
>();
} }
/** /**
@ -6100,6 +6129,7 @@ function buildMessenger() {
function buildNetworkControllerMessenger(messenger = buildMessenger()) { function buildNetworkControllerMessenger(messenger = buildMessenger()) {
return messenger.getRestricted({ return messenger.getRestricted({
name: 'NetworkController', name: 'NetworkController',
allowedActions: ['NetworkController:getProviderConfig'],
allowedEvents: [ allowedEvents: [
'NetworkController:networkDidChange', 'NetworkController:networkDidChange',
'NetworkController:networkWillChange', 'NetworkController:networkWillChange',
@ -6116,7 +6146,10 @@ type WithControllerCallback<ReturnValue> = ({
controller, controller,
}: { }: {
controller: NetworkController; controller: NetworkController;
messenger: ControllerMessenger<never, NetworkControllerEvent>; messenger: ControllerMessenger<
NetworkControllerAction,
NetworkControllerEvent
>;
}) => Promise<ReturnValue> | ReturnValue; }) => Promise<ReturnValue> | ReturnValue;
/** /**

View File

@ -130,12 +130,19 @@ export type NetworkControllerEvent =
| NetworkControllerInfuraIsBlockedEvent | NetworkControllerInfuraIsBlockedEvent
| NetworkControllerInfuraIsUnblockedEvent; | NetworkControllerInfuraIsUnblockedEvent;
export type NetworkControllerGetProviderConfigAction = {
type: `NetworkController:getProviderConfig`;
handler: () => ProviderConfiguration;
};
export type NetworkControllerAction = NetworkControllerGetProviderConfigAction;
/** /**
* The messenger that the NetworkController uses to publish events. * The messenger that the NetworkController uses to publish events.
*/ */
export type NetworkControllerMessenger = RestrictedControllerMessenger< export type NetworkControllerMessenger = RestrictedControllerMessenger<
typeof name, typeof name,
never, NetworkControllerAction,
NetworkControllerEvent, NetworkControllerEvent,
string, string,
string string
@ -477,6 +484,9 @@ export class NetworkController extends EventEmitter {
} }
this.#infuraProjectId = infuraProjectId; this.#infuraProjectId = infuraProjectId;
this.#trackMetaMetricsEvent = trackMetaMetricsEvent; this.#trackMetaMetricsEvent = trackMetaMetricsEvent;
this.#messenger.registerActionHandler(`${name}:getProviderConfig`, () => {
return this.store.getState().providerConfig;
});
} }
/** /**