@@ -11551,206 +11551,6 @@ describe('NetworkController', () => {
11551
11551
});
11552
11552
});
11553
11553
11554
- describe('dangerouslySetNetworkConfiguration', () => {
11555
- const TEST_CHAIN_ID = '0x1337';
11556
- const ORIGINAL_NETWORK_CLIENT_ID = '1111';
11557
-
11558
- const arrangeTestUtils = (props: { newNetworkClientIds: string[] }) => {
11559
- mockCreateNetworkClient().mockReturnValue(buildFakeClient());
11560
-
11561
- const originalNetwork = buildCustomNetworkConfiguration({
11562
- chainId: TEST_CHAIN_ID,
11563
- rpcEndpoints: [
11564
- buildCustomRpcEndpoint({
11565
- networkClientId: ORIGINAL_NETWORK_CLIENT_ID,
11566
- }),
11567
- ],
11568
- });
11569
-
11570
- const overrideNetwork = buildCustomNetworkConfiguration({
11571
- chainId: TEST_CHAIN_ID,
11572
- rpcEndpoints: props.newNetworkClientIds.map((id) =>
11573
- buildCustomRpcEndpoint({ networkClientId: id }),
11574
- ),
11575
- });
11576
-
11577
- const controllerState =
11578
- buildNetworkControllerStateWithDefaultSelectedNetworkClientId({
11579
- networkConfigurationsByChainId: {
11580
- [originalNetwork.chainId]: originalNetwork,
11581
- },
11582
- networksMetadata: {
11583
- [ORIGINAL_NETWORK_CLIENT_ID]: {
11584
- EIPS: {
11585
- '1559': true,
11586
- },
11587
- status: NetworkStatus.Available,
11588
- },
11589
- },
11590
- });
11591
-
11592
- return { originalNetwork, overrideNetwork, controllerState };
11593
- };
11594
-
11595
- const actTest = async (
11596
- props: Pick<
11597
- ReturnType<typeof arrangeTestUtils>,
11598
- 'controllerState' | 'overrideNetwork'
11599
- >,
11600
- ) => {
11601
- const result = await withController(
11602
- { state: props.controllerState },
11603
- async ({ controller, messenger }) => {
11604
- await messenger.call(
11605
- 'NetworkController:dangerouslySetNetworkConfiguration',
11606
- props.overrideNetwork,
11607
- );
11608
- return {
11609
- state: controller.state,
11610
- controller,
11611
- };
11612
- },
11613
- );
11614
-
11615
- return result;
11616
- };
11617
-
11618
- const arrangeActTest = async (props: { newNetworkClientIds: string[] }) => {
11619
- const arrange = arrangeTestUtils(props);
11620
-
11621
- // Act
11622
- const result = await actTest({
11623
- controllerState: arrange.controllerState,
11624
- overrideNetwork: arrange.overrideNetwork,
11625
- });
11626
- return { ...arrange, result };
11627
- };
11628
-
11629
- const assertStateHasBeenUpdated = (props: {
11630
- state: NetworkState;
11631
- newNetworkConfiguration: NetworkConfiguration;
11632
- networkClientIdsRemoved: string[];
11633
- }) => {
11634
- const { state, newNetworkConfiguration, networkClientIdsRemoved } = props;
11635
-
11636
- // Assert - new network config has been set
11637
- expect(state.networkConfigurationsByChainId[TEST_CHAIN_ID]).toStrictEqual(
11638
- newNetworkConfiguration,
11639
- );
11640
-
11641
- // Assert - networks metadata removed some endpoints (from original network)
11642
- networkClientIdsRemoved.forEach((id) => {
11643
- expect(state.networksMetadata[id]).toBeUndefined();
11644
- });
11645
-
11646
- // Assert - networks metadata has been set
11647
- newNetworkConfiguration.rpcEndpoints.forEach((r) => {
11648
- expect(state.networksMetadata[r.networkClientId]).toBeDefined();
11649
- });
11650
- };
11651
-
11652
- const assertNetworkRegistryHasBeenUpdated = (props: {
11653
- controller: NetworkController;
11654
- newNetworkConfiguration: NetworkConfiguration;
11655
- networkClientIdsRemoved: string[];
11656
- }) => {
11657
- const { controller, newNetworkConfiguration, networkClientIdsRemoved } =
11658
- props;
11659
- const registry = controller.getNetworkClientRegistry();
11660
-
11661
- // Assert - networks that were removed (from original network that was overwritten)
11662
- networkClientIdsRemoved.forEach((id) => {
11663
- expect(registry[id]).toBeUndefined();
11664
- });
11665
-
11666
- // Assert - new network config RPCs has been updated in the network registry
11667
- newNetworkConfiguration.rpcEndpoints.forEach((r) => {
11668
- expect(registry[r.networkClientId]).toBeDefined();
11669
- });
11670
- };
11671
-
11672
- const assertNetworkConfigurationsByIdCacheHasBeenUpdated = (props: {
11673
- controller: NetworkController;
11674
- newNetworkConfiguration: NetworkConfiguration;
11675
- }) => {
11676
- const { controller, newNetworkConfiguration } = props;
11677
- expect(
11678
- controller.getNetworkConfigurationByChainId(TEST_CHAIN_ID),
11679
- ).toStrictEqual(newNetworkConfiguration);
11680
- };
11681
-
11682
- it('overrides a set network configuration', async () => {
11683
- const { result, overrideNetwork } = await arrangeActTest({
11684
- newNetworkClientIds: ['2222', '3333'],
11685
- });
11686
-
11687
- assertStateHasBeenUpdated({
11688
- state: result.state,
11689
- newNetworkConfiguration: overrideNetwork,
11690
- networkClientIdsRemoved: [ORIGINAL_NETWORK_CLIENT_ID],
11691
- });
11692
- assertNetworkRegistryHasBeenUpdated({
11693
- controller: result.controller,
11694
- newNetworkConfiguration: overrideNetwork,
11695
- networkClientIdsRemoved: [ORIGINAL_NETWORK_CLIENT_ID],
11696
- });
11697
- assertNetworkConfigurationsByIdCacheHasBeenUpdated({
11698
- controller: result.controller,
11699
- newNetworkConfiguration: overrideNetwork,
11700
- });
11701
-
11702
- // Selected network has changed (since original was removed)
11703
- // We will select next available RPC for the given chain
11704
- expect(result.state.selectedNetworkClientId).toBe('2222');
11705
- });
11706
-
11707
- it('overrides network config, but keeps same selected network', async () => {
11708
- const { result, overrideNetwork } = await arrangeActTest({
11709
- newNetworkClientIds: [ORIGINAL_NETWORK_CLIENT_ID, '2222'],
11710
- });
11711
-
11712
- assertStateHasBeenUpdated({
11713
- state: result.state,
11714
- newNetworkConfiguration: overrideNetwork,
11715
- // no networks were removed, as the new network config contains the original network client id
11716
- networkClientIdsRemoved: [],
11717
- });
11718
- assertNetworkRegistryHasBeenUpdated({
11719
- controller: result.controller,
11720
- newNetworkConfiguration: overrideNetwork,
11721
- // no networks were removed, as the new network config contains the original network client id
11722
- networkClientIdsRemoved: [],
11723
- });
11724
- assertNetworkConfigurationsByIdCacheHasBeenUpdated({
11725
- controller: result.controller,
11726
- newNetworkConfiguration: overrideNetwork,
11727
- });
11728
-
11729
- // selected RPC has not changed, as it was not removed
11730
- expect(result.state.selectedNetworkClientId).toBe(
11731
- ORIGINAL_NETWORK_CLIENT_ID,
11732
- );
11733
- });
11734
-
11735
- it('does nothing if there is no network to override', async () => {
11736
- const { controllerState, overrideNetwork } = arrangeTestUtils({
11737
- newNetworkClientIds: ['2222'],
11738
- });
11739
-
11740
- // shim/mock the controller state to not contain a network we are overriding
11741
- controllerState.networkConfigurationsByChainId['0xDiffChain'] =
11742
- controllerState.networkConfigurationsByChainId[TEST_CHAIN_ID];
11743
- delete controllerState.networkConfigurationsByChainId[TEST_CHAIN_ID];
11744
- controllerState.networkConfigurationsByChainId['0xDiffChain'].chainId =
11745
- '0xDiffChain';
11746
-
11747
- const result = await actTest({ controllerState, overrideNetwork });
11748
-
11749
- // No changes should have occurred
11750
- expect(result.state).toStrictEqual(controllerState);
11751
- });
11752
- });
11753
-
11754
11554
describe('rollbackToPreviousProvider', () => {
11755
11555
describe('when called not following any network switches', () => {
11756
11556
for (const infuraNetworkType of Object.values(InfuraNetworkType)) {
0 commit comments