mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-22 09:57:02 +01:00
Sync NetworkController initializeProvider tests w/ core (#19297)
This makes it easier to visually compare differences in the NetworkController unit tests between core and this repo.
This commit is contained in:
parent
63c402155d
commit
a5c370cdfa
@ -259,37 +259,34 @@ describe('NetworkController', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('initializeProvider', () => {
|
describe('initializeProvider', () => {
|
||||||
it('throws if the provider configuration is invalid', async () => {
|
describe('when the type in the provider config is invalid', () => {
|
||||||
const invalidProviderConfig = {};
|
it('throws', async () => {
|
||||||
await withController(
|
const invalidProviderConfig = {};
|
||||||
/* @ts-expect-error We're intentionally passing bad input. */
|
await withController(
|
||||||
{
|
/* @ts-expect-error We're intentionally passing bad input. */
|
||||||
state: {
|
{
|
||||||
providerConfig: invalidProviderConfig,
|
state: {
|
||||||
|
providerConfig: invalidProviderConfig,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
async ({ controller }) => {
|
||||||
async ({ controller }) => {
|
await expect(async () => {
|
||||||
await expect(async () => {
|
await controller.initializeProvider();
|
||||||
await controller.initializeProvider();
|
}).rejects.toThrow("Unrecognized network type: 'undefined'");
|
||||||
}).rejects.toThrow(
|
},
|
||||||
'NetworkController - #configureProvider - unknown type "undefined"',
|
);
|
||||||
);
|
});
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
for (const { networkType } of INFURA_NETWORKS) {
|
for (const { networkType } of INFURA_NETWORKS) {
|
||||||
describe(`when the type in the provider configuration is "${networkType}"`, () => {
|
describe(`when the type in the provider config is "${networkType}"`, () => {
|
||||||
it(`initializes a provider pointed to the "${networkType}" Infura network`, async () => {
|
it(`creates a network client for the ${networkType} Infura network, capturing the resulting provider`, async () => {
|
||||||
await withController(
|
await withController(
|
||||||
{
|
{
|
||||||
state: {
|
state: {
|
||||||
providerConfig: {
|
providerConfig: buildProviderConfig({
|
||||||
type: networkType,
|
type: networkType,
|
||||||
// NOTE: This doesn't need to match the logical chain ID
|
}),
|
||||||
// of the network selected, it just needs to exist
|
|
||||||
chainId: '0x9999999',
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
infuraProjectId: 'some-infura-project-id',
|
infuraProjectId: 'some-infura-project-id',
|
||||||
},
|
},
|
||||||
@ -297,7 +294,8 @@ describe('NetworkController', () => {
|
|||||||
const fakeProvider = buildFakeProvider([
|
const fakeProvider = buildFakeProvider([
|
||||||
{
|
{
|
||||||
request: {
|
request: {
|
||||||
method: 'test',
|
method: 'test_method',
|
||||||
|
params: [],
|
||||||
},
|
},
|
||||||
response: {
|
response: {
|
||||||
result: 'test response',
|
result: 'test response',
|
||||||
@ -305,27 +303,27 @@ describe('NetworkController', () => {
|
|||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
const fakeNetworkClient = buildFakeClient(fakeProvider);
|
const fakeNetworkClient = buildFakeClient(fakeProvider);
|
||||||
mockCreateNetworkClient()
|
createNetworkClientMock.mockReturnValue(fakeNetworkClient);
|
||||||
.calledWith({
|
|
||||||
network: networkType,
|
|
||||||
infuraProjectId: 'some-infura-project-id',
|
|
||||||
type: NetworkClientType.Infura,
|
|
||||||
})
|
|
||||||
.mockReturnValue(fakeNetworkClient);
|
|
||||||
|
|
||||||
await controller.initializeProvider();
|
await controller.initializeProvider();
|
||||||
|
|
||||||
|
expect(createNetworkClientMock).toHaveBeenCalledWith({
|
||||||
|
network: networkType,
|
||||||
|
infuraProjectId: 'some-infura-project-id',
|
||||||
|
type: NetworkClientType.Infura,
|
||||||
|
});
|
||||||
const { provider } = controller.getProviderAndBlockTracker();
|
const { provider } = controller.getProviderAndBlockTracker();
|
||||||
assert(provider, 'Provider is somehow unset');
|
assert(provider, 'Provider is not set');
|
||||||
const promisifiedSendAsync = promisify(provider.sendAsync).bind(
|
const promisifiedSendAsync = promisify(provider.sendAsync).bind(
|
||||||
provider,
|
provider,
|
||||||
);
|
);
|
||||||
const response = await promisifiedSendAsync({
|
const { result } = await promisifiedSendAsync({
|
||||||
id: '1',
|
id: 1,
|
||||||
jsonrpc: '2.0',
|
jsonrpc: '2.0',
|
||||||
method: 'test',
|
method: 'test_method',
|
||||||
|
params: [],
|
||||||
});
|
});
|
||||||
expect(response.result).toBe('test response');
|
expect(result).toBe('test response');
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
@ -344,23 +342,14 @@ describe('NetworkController', () => {
|
|||||||
|
|
||||||
describe(`when the type in the provider configuration is "rpc"`, () => {
|
describe(`when the type in the provider configuration is "rpc"`, () => {
|
||||||
describe('if chainId and rpcUrl are present in the provider config', () => {
|
describe('if chainId and rpcUrl are present in the provider config', () => {
|
||||||
it('initializes a provider pointed to the given RPC URL whose chain ID matches the configured chain ID', async () => {
|
it('creates a network client for a custom RPC endpoint using the provider config, capturing the resulting provider', async () => {
|
||||||
await withController(
|
await withController(
|
||||||
{
|
{
|
||||||
state: {
|
state: {
|
||||||
providerConfig: {
|
providerConfig: {
|
||||||
type: 'rpc',
|
type: NETWORK_TYPES.RPC,
|
||||||
chainId: '0x1337',
|
chainId: toHex(1337),
|
||||||
rpcUrl: 'https://mock-rpc-url',
|
rpcUrl: 'http://example.com',
|
||||||
ticker: 'TEST',
|
|
||||||
},
|
|
||||||
networkConfigurations: {
|
|
||||||
testNetworkConfigurationId: {
|
|
||||||
rpcUrl: 'https://mock-rpc-url',
|
|
||||||
chainId: '0x1337',
|
|
||||||
ticker: 'TEST',
|
|
||||||
id: 'testNetworkConfigurationId',
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -368,7 +357,8 @@ describe('NetworkController', () => {
|
|||||||
const fakeProvider = buildFakeProvider([
|
const fakeProvider = buildFakeProvider([
|
||||||
{
|
{
|
||||||
request: {
|
request: {
|
||||||
method: 'test',
|
method: 'test_method',
|
||||||
|
params: [],
|
||||||
},
|
},
|
||||||
response: {
|
response: {
|
||||||
result: 'test response',
|
result: 'test response',
|
||||||
@ -376,28 +366,27 @@ describe('NetworkController', () => {
|
|||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
const fakeNetworkClient = buildFakeClient(fakeProvider);
|
const fakeNetworkClient = buildFakeClient(fakeProvider);
|
||||||
mockCreateNetworkClient()
|
createNetworkClientMock.mockReturnValue(fakeNetworkClient);
|
||||||
.calledWith({
|
|
||||||
chainId: '0x1337',
|
|
||||||
rpcUrl: 'https://mock-rpc-url',
|
|
||||||
type: NetworkClientType.Custom,
|
|
||||||
})
|
|
||||||
.mockReturnValue(fakeNetworkClient);
|
|
||||||
mockCreateNetworkClient().mockReturnValue(fakeNetworkClient);
|
|
||||||
|
|
||||||
await controller.initializeProvider();
|
await controller.initializeProvider();
|
||||||
|
|
||||||
|
expect(createNetworkClientMock).toHaveBeenCalledWith({
|
||||||
|
chainId: toHex(1337),
|
||||||
|
rpcUrl: 'http://example.com',
|
||||||
|
type: NetworkClientType.Custom,
|
||||||
|
});
|
||||||
const { provider } = controller.getProviderAndBlockTracker();
|
const { provider } = controller.getProviderAndBlockTracker();
|
||||||
assert(provider, 'Provider is somehow unset');
|
assert(provider, 'Provider is not set');
|
||||||
const promisifiedSendAsync = promisify(provider.sendAsync).bind(
|
const promisifiedSendAsync = promisify(provider.sendAsync).bind(
|
||||||
provider,
|
provider,
|
||||||
);
|
);
|
||||||
const response = await promisifiedSendAsync({
|
const { result } = await promisifiedSendAsync({
|
||||||
id: '1',
|
id: 1,
|
||||||
jsonrpc: '2.0',
|
jsonrpc: '2.0',
|
||||||
method: 'test',
|
method: 'test_method',
|
||||||
|
params: [],
|
||||||
});
|
});
|
||||||
expect(response.result).toBe('test response');
|
expect(result).toBe('test response');
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
@ -909,9 +909,7 @@ export class NetworkController extends EventEmitter {
|
|||||||
}
|
}
|
||||||
this.#configureStandardProvider(rpcUrl, chainId);
|
this.#configureStandardProvider(rpcUrl, chainId);
|
||||||
} else {
|
} else {
|
||||||
throw new Error(
|
throw new Error(`Unrecognized network type: '${type}'`);
|
||||||
`NetworkController - #configureProvider - unknown type "${type}"`,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user