mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-22 09:57:02 +01:00
Refactor NC initializeProvider tests (#19253)
On the `core` side, the tests for `NetworkController.initializeProvider` use the `lookupNetworkTests` to automatically test the `lookupNetwork` behavior that `initializeProvider` initiates. This commit makes use of the helper to bring the tests closer to the `core` version.
This commit is contained in:
parent
5b85e9b73b
commit
b7ef99847a
@ -333,116 +333,14 @@ describe('NetworkController', () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('emits infuraIsBlocked or infuraIsUnblocked, depending on whether Infura is blocking requests', async () => {
|
lookupNetworkTests({
|
||||||
await withController(
|
expectedProviderConfig: buildProviderConfig({ type: networkType }),
|
||||||
{
|
initialState: {
|
||||||
state: {
|
providerConfig: buildProviderConfig({ type: networkType }),
|
||||||
providerConfig: {
|
},
|
||||||
type: networkType,
|
operation: async (controller: NetworkController) => {
|
||||||
// NOTE: This doesn't need to match the logical chain ID
|
await controller.initializeProvider();
|
||||||
// of the network selected, it just needs to exist
|
},
|
||||||
chainId: '0x9999999',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
async ({ controller, messenger }) => {
|
|
||||||
const fakeProvider = buildFakeProvider();
|
|
||||||
const fakeNetworkClient = buildFakeClient(fakeProvider);
|
|
||||||
mockCreateNetworkClient().mockReturnValue(fakeNetworkClient);
|
|
||||||
|
|
||||||
const infuraIsUnblocked = await waitForPublishedEvents({
|
|
||||||
messenger,
|
|
||||||
eventType: NetworkControllerEventType.InfuraIsUnblocked,
|
|
||||||
operation: async () => {
|
|
||||||
await controller.initializeProvider();
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(infuraIsUnblocked).toBeTruthy();
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('determines the status of the network, storing it in state', async () => {
|
|
||||||
await withController(
|
|
||||||
{
|
|
||||||
state: {
|
|
||||||
providerConfig: {
|
|
||||||
type: networkType,
|
|
||||||
// NOTE: This doesn't need to match the logical chain ID
|
|
||||||
// of the network selected, it just needs to exist
|
|
||||||
chainId: '0x9999999',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
async ({ controller }) => {
|
|
||||||
const fakeProvider = buildFakeProvider([
|
|
||||||
{
|
|
||||||
request: {
|
|
||||||
method: 'net_version',
|
|
||||||
},
|
|
||||||
response: SUCCESSFUL_NET_VERSION_RESPONSE,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
request: {
|
|
||||||
method: 'eth_getBlockByNumber',
|
|
||||||
},
|
|
||||||
response: SUCCESSFUL_ETH_GET_BLOCK_BY_NUMBER_RESPONSE,
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
const fakeNetworkClient = buildFakeClient(fakeProvider);
|
|
||||||
mockCreateNetworkClient().mockReturnValue(fakeNetworkClient);
|
|
||||||
expect(controller.store.getState().networkStatus).toBe('unknown');
|
|
||||||
|
|
||||||
await controller.initializeProvider();
|
|
||||||
|
|
||||||
expect(controller.store.getState().networkStatus).toBe(
|
|
||||||
'available',
|
|
||||||
);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('determines whether the network supports EIP-1559 and stores the result in state without overwriting other state in the networkDetails store', async () => {
|
|
||||||
await withController(
|
|
||||||
{
|
|
||||||
state: {
|
|
||||||
providerConfig: {
|
|
||||||
type: networkType,
|
|
||||||
// NOTE: This doesn't need to match the logical chain ID
|
|
||||||
// of the network selected, it just needs to exist
|
|
||||||
chainId: '0x9999999',
|
|
||||||
},
|
|
||||||
networkDetails: {
|
|
||||||
EIPS: {},
|
|
||||||
other: 'details',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
async ({ controller }) => {
|
|
||||||
const fakeProvider = buildFakeProvider([
|
|
||||||
{
|
|
||||||
request: {
|
|
||||||
method: 'eth_getBlockByNumber',
|
|
||||||
},
|
|
||||||
response: {
|
|
||||||
result: POST_1559_BLOCK,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
const fakeNetworkClient = buildFakeClient(fakeProvider);
|
|
||||||
mockCreateNetworkClient().mockReturnValue(fakeNetworkClient);
|
|
||||||
|
|
||||||
await controller.initializeProvider();
|
|
||||||
|
|
||||||
expect(controller.store.getState().networkDetails).toStrictEqual({
|
|
||||||
EIPS: {
|
|
||||||
1559: true,
|
|
||||||
},
|
|
||||||
other: 'details',
|
|
||||||
});
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -506,177 +404,18 @@ describe('NetworkController', () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('emits infuraIsUnblocked', async () => {
|
lookupNetworkTests({
|
||||||
await withController(
|
expectedProviderConfig: buildProviderConfig({
|
||||||
{
|
type: NETWORK_TYPES.RPC,
|
||||||
state: {
|
}),
|
||||||
providerConfig: {
|
initialState: {
|
||||||
type: 'rpc',
|
providerConfig: buildProviderConfig({
|
||||||
rpcUrl: 'https://mock-rpc-url',
|
type: NETWORK_TYPES.RPC,
|
||||||
chainId: '0xtest',
|
}),
|
||||||
ticker: 'TEST',
|
},
|
||||||
},
|
operation: async (controller: NetworkController) => {
|
||||||
networkConfigurations: {
|
await controller.initializeProvider();
|
||||||
testNetworkConfigurationId: {
|
},
|
||||||
rpcUrl: 'https://mock-rpc-url',
|
|
||||||
chainId: '0xtest',
|
|
||||||
ticker: 'TEST',
|
|
||||||
id: 'testNetworkConfigurationId',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
async ({ controller, messenger }) => {
|
|
||||||
const fakeProvider = buildFakeProvider();
|
|
||||||
const fakeNetworkClient = buildFakeClient(fakeProvider);
|
|
||||||
mockCreateNetworkClient().mockReturnValue(fakeNetworkClient);
|
|
||||||
|
|
||||||
const infuraIsUnblocked = await waitForPublishedEvents({
|
|
||||||
messenger,
|
|
||||||
eventType: NetworkControllerEventType.InfuraIsUnblocked,
|
|
||||||
operation: async () => {
|
|
||||||
await controller.initializeProvider();
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(infuraIsUnblocked).toBeTruthy();
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('does not emit infuraIsBlocked', async () => {
|
|
||||||
await withController(
|
|
||||||
{
|
|
||||||
state: {
|
|
||||||
providerConfig: {
|
|
||||||
type: 'rpc',
|
|
||||||
rpcUrl: 'https://mock-rpc-url',
|
|
||||||
chainId: '0xtest',
|
|
||||||
ticker: 'TEST',
|
|
||||||
},
|
|
||||||
networkConfigurations: {
|
|
||||||
testNetworkConfigurationId: {
|
|
||||||
rpcUrl: 'https://mock-rpc-url',
|
|
||||||
chainId: '0xtest',
|
|
||||||
ticker: 'TEST',
|
|
||||||
id: 'testNetworkConfigurationId',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
async ({ controller, messenger }) => {
|
|
||||||
const fakeProvider = buildFakeProvider();
|
|
||||||
const fakeNetworkClient = buildFakeClient(fakeProvider);
|
|
||||||
mockCreateNetworkClient().mockReturnValue(fakeNetworkClient);
|
|
||||||
|
|
||||||
const promiseForNoInfuraIsBlockedEvents = waitForPublishedEvents({
|
|
||||||
messenger,
|
|
||||||
eventType: NetworkControllerEventType.InfuraIsBlocked,
|
|
||||||
count: 0,
|
|
||||||
operation: async () => {
|
|
||||||
await controller.initializeProvider();
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(await promiseForNoInfuraIsBlockedEvents).toBeTruthy();
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('determines the status of the network, storing it in state', async () => {
|
|
||||||
await withController(
|
|
||||||
{
|
|
||||||
state: {
|
|
||||||
providerConfig: {
|
|
||||||
type: 'rpc',
|
|
||||||
rpcUrl: 'https://mock-rpc-url',
|
|
||||||
chainId: '0xtest',
|
|
||||||
ticker: 'TEST',
|
|
||||||
},
|
|
||||||
networkConfigurations: {
|
|
||||||
testNetworkConfigurationId: {
|
|
||||||
rpcUrl: 'https://mock-rpc-url',
|
|
||||||
chainId: '0xtest',
|
|
||||||
ticker: 'TEST',
|
|
||||||
id: 'testNetworkConfigurationId1',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
async ({ controller }) => {
|
|
||||||
const fakeProvider = buildFakeProvider([
|
|
||||||
{
|
|
||||||
request: {
|
|
||||||
method: 'net_version',
|
|
||||||
},
|
|
||||||
response: SUCCESSFUL_NET_VERSION_RESPONSE,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
request: {
|
|
||||||
method: 'eth_getBlockByNumber',
|
|
||||||
},
|
|
||||||
response: SUCCESSFUL_ETH_GET_BLOCK_BY_NUMBER_RESPONSE,
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
const fakeNetworkClient = buildFakeClient(fakeProvider);
|
|
||||||
mockCreateNetworkClient().mockReturnValue(fakeNetworkClient);
|
|
||||||
expect(controller.store.getState().networkStatus).toBe('unknown');
|
|
||||||
|
|
||||||
await controller.initializeProvider();
|
|
||||||
|
|
||||||
expect(controller.store.getState().networkStatus).toBe('available');
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('determines whether the network supports EIP-1559, storing it in state', async () => {
|
|
||||||
await withController(
|
|
||||||
{
|
|
||||||
state: {
|
|
||||||
providerConfig: {
|
|
||||||
type: 'rpc',
|
|
||||||
rpcUrl: 'https://mock-rpc-url',
|
|
||||||
chainId: '0xtest',
|
|
||||||
ticker: 'TEST',
|
|
||||||
},
|
|
||||||
networkConfigurations: {
|
|
||||||
testNetworkConfigurationId: {
|
|
||||||
rpcUrl: 'https://mock-rpc-url',
|
|
||||||
chainId: '0xtest',
|
|
||||||
ticker: 'TEST',
|
|
||||||
id: 'testNetworkConfigurationId',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
networkDetails: {
|
|
||||||
EIPS: {},
|
|
||||||
other: 'details',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
async ({ controller }) => {
|
|
||||||
const fakeProvider = buildFakeProvider([
|
|
||||||
{
|
|
||||||
request: {
|
|
||||||
method: 'eth_getBlockByNumber',
|
|
||||||
},
|
|
||||||
response: {
|
|
||||||
result: POST_1559_BLOCK,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
const fakeNetworkClient = buildFakeClient(fakeProvider);
|
|
||||||
mockCreateNetworkClient().mockReturnValue(fakeNetworkClient);
|
|
||||||
|
|
||||||
await controller.initializeProvider();
|
|
||||||
|
|
||||||
expect(controller.store.getState().networkDetails).toStrictEqual({
|
|
||||||
EIPS: {
|
|
||||||
1559: true,
|
|
||||||
},
|
|
||||||
other: 'details',
|
|
||||||
});
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user