2021-02-04 19:15:23 +01:00
|
|
|
import React from 'react';
|
|
|
|
import configureMockStore from 'redux-mock-store';
|
|
|
|
import thunk from 'redux-thunk';
|
2022-08-03 22:30:43 +02:00
|
|
|
import { screen } from '@testing-library/react';
|
|
|
|
import { renderWithProvider } from '../../../../test/lib/render-helpers';
|
2021-11-22 17:24:42 +01:00
|
|
|
import { LOCALHOST_RPC_URL } from '../../../../shared/constants/network';
|
2021-03-16 22:00:08 +01:00
|
|
|
import NetworkDropdown from './network-dropdown';
|
2018-09-24 18:28:04 +02:00
|
|
|
|
2023-03-21 17:28:38 +01:00
|
|
|
// Mock linea test network feature toggle
|
|
|
|
jest.mock('../../../../shared/constants/network', () => {
|
|
|
|
const constants = jest.requireActual('../../../../shared/constants/network');
|
|
|
|
return {
|
|
|
|
...constants,
|
|
|
|
SHOULD_SHOW_LINEA_TESTNET_NETWORK: true,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('Network Dropdown', () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const createMockStore = configureMockStore([thunk]);
|
2018-09-24 18:28:04 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('NetworkDropdown in appState in false', () => {
|
2018-09-24 18:28:04 +02:00
|
|
|
const mockState = {
|
|
|
|
metamask: {
|
NetworkController: Split `network` into `networkId` and `networkStatus` (#17556)
The `network` store of the network controller crams two types of data
into one place. It roughly tracks whether we have enough information to
make requests to the network and whether the network is capable of
receiving requests, but it also stores the ID of the network (as
obtained via `net_version`).
Generally we shouldn't be using the network ID for anything, as it has
been completely replaced by chain ID, which all custom RPC endpoints
have been required to support for over a year now. However, as the
network ID is used in various places within the extension codebase,
removing it entirely would be a non-trivial effort. So, minimally, this
commit splits `network` into two stores: `networkId` and
`networkStatus`. But it also expands the concept of network status.
Previously, the network was in one of two states: "loading" and
"not-loading". But now it can be in one of four states:
- `available`: The network is able to receive and respond to requests.
- `unavailable`: The network is not able to receive and respond to
requests for unknown reasons.
- `blocked`: The network is actively blocking requests based on the
user's geolocation. (This is specific to Infura.)
- `unknown`: We don't know whether the network can receive and respond
to requests, either because we haven't checked or we tried to check
and were unsuccessful.
This commit also changes how the network status is determined —
specifically, how many requests are used to determine that status, when
they occur, and whether they are awaited. Previously, the network
controller would make 2 to 3 requests during the course of running
`lookupNetwork`.
* First, if it was an Infura network, it would make a request for
`eth_blockNumber` to determine whether Infura was blocking requests or
not, then emit an appropriate event. This operation was not awaited.
* Then, regardless of the network, it would fetch the network ID via
`net_version`. This operation was awaited.
* Finally, regardless of the network, it would fetch the latest block
via `eth_getBlockByNumber`, then use the result to determine whether
the network supported EIP-1559. This operation was awaited.
Now:
* One fewer request is made, specifically `eth_blockNumber`, as we don't
need to make an extra request to determine whether Infura is blocking
requests; we can reuse `eth_getBlockByNumber`;
* All requests are awaited, which makes `lookupNetwork` run fully
in-band instead of partially out-of-band; and
* Both requests for `net_version` and `eth_getBlockByNumber` are
performed in parallel to make `lookupNetwork` run slightly faster.
2023-03-31 00:49:12 +02:00
|
|
|
networkId: '1',
|
|
|
|
networkStatus: 'available',
|
2018-09-24 18:28:04 +02:00
|
|
|
provider: {
|
|
|
|
type: 'test',
|
|
|
|
},
|
2022-05-31 18:46:38 +02:00
|
|
|
showTestnetMessageInDropdown: false,
|
2021-10-28 21:31:05 +02:00
|
|
|
preferences: {
|
|
|
|
showTestNetworks: true,
|
|
|
|
},
|
2018-09-24 18:28:04 +02:00
|
|
|
},
|
|
|
|
appState: {
|
2020-01-16 04:33:37 +01:00
|
|
|
networkDropdownOpen: false,
|
2018-09-24 18:28:04 +02:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-09-24 18:28:04 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const store = createMockStore(mockState);
|
2018-09-24 18:28:04 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
beforeEach(() => {
|
2022-08-03 22:30:43 +02:00
|
|
|
renderWithProvider(<NetworkDropdown />, store);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-09-24 18:28:04 +02:00
|
|
|
|
2022-08-03 22:30:43 +02:00
|
|
|
it('should not render menu dropdown when network dropdown is in false state', () => {
|
|
|
|
const menuDropdown = screen.queryByTestId('menu-dropdown');
|
|
|
|
expect(menuDropdown).not.toBeInTheDocument();
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-09-24 18:28:04 +02:00
|
|
|
|
2022-08-03 22:30:43 +02:00
|
|
|
it('checks for network droppo class', () => {
|
|
|
|
const networkDropdown = screen.queryByTestId('network-droppo');
|
|
|
|
expect(networkDropdown).toBeInTheDocument();
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2018-09-24 18:28:04 +02:00
|
|
|
|
2021-10-28 21:31:05 +02:00
|
|
|
describe('NetworkDropdown in appState is true and show test networks is true', () => {
|
2018-09-24 18:28:04 +02:00
|
|
|
const mockState = {
|
|
|
|
metamask: {
|
NetworkController: Split `network` into `networkId` and `networkStatus` (#17556)
The `network` store of the network controller crams two types of data
into one place. It roughly tracks whether we have enough information to
make requests to the network and whether the network is capable of
receiving requests, but it also stores the ID of the network (as
obtained via `net_version`).
Generally we shouldn't be using the network ID for anything, as it has
been completely replaced by chain ID, which all custom RPC endpoints
have been required to support for over a year now. However, as the
network ID is used in various places within the extension codebase,
removing it entirely would be a non-trivial effort. So, minimally, this
commit splits `network` into two stores: `networkId` and
`networkStatus`. But it also expands the concept of network status.
Previously, the network was in one of two states: "loading" and
"not-loading". But now it can be in one of four states:
- `available`: The network is able to receive and respond to requests.
- `unavailable`: The network is not able to receive and respond to
requests for unknown reasons.
- `blocked`: The network is actively blocking requests based on the
user's geolocation. (This is specific to Infura.)
- `unknown`: We don't know whether the network can receive and respond
to requests, either because we haven't checked or we tried to check
and were unsuccessful.
This commit also changes how the network status is determined —
specifically, how many requests are used to determine that status, when
they occur, and whether they are awaited. Previously, the network
controller would make 2 to 3 requests during the course of running
`lookupNetwork`.
* First, if it was an Infura network, it would make a request for
`eth_blockNumber` to determine whether Infura was blocking requests or
not, then emit an appropriate event. This operation was not awaited.
* Then, regardless of the network, it would fetch the network ID via
`net_version`. This operation was awaited.
* Finally, regardless of the network, it would fetch the latest block
via `eth_getBlockByNumber`, then use the result to determine whether
the network supported EIP-1559. This operation was awaited.
Now:
* One fewer request is made, specifically `eth_blockNumber`, as we don't
need to make an extra request to determine whether Infura is blocking
requests; we can reuse `eth_getBlockByNumber`;
* All requests are awaited, which makes `lookupNetwork` run fully
in-band instead of partially out-of-band; and
* Both requests for `net_version` and `eth_getBlockByNumber` are
performed in parallel to make `lookupNetwork` run slightly faster.
2023-03-31 00:49:12 +02:00
|
|
|
networkId: '1',
|
|
|
|
networkStatus: 'available',
|
2018-09-24 18:28:04 +02:00
|
|
|
provider: {
|
2020-11-03 00:41:28 +01:00
|
|
|
type: 'test',
|
2018-09-24 18:28:04 +02:00
|
|
|
},
|
2022-05-31 18:46:38 +02:00
|
|
|
showTestnetMessageInDropdown: false,
|
2021-10-28 21:31:05 +02:00
|
|
|
preferences: {
|
|
|
|
showTestNetworks: true,
|
|
|
|
},
|
2023-03-09 22:00:28 +01:00
|
|
|
networkConfigurations: {
|
|
|
|
networkConfigurationId1: {
|
|
|
|
chainId: '0x1a',
|
|
|
|
rpcUrl: 'http://localhost:7545',
|
|
|
|
},
|
|
|
|
networkConfigurationId2: { rpcUrl: 'http://localhost:7546' },
|
|
|
|
networkConfigurationId3: {
|
|
|
|
rpcUrl: LOCALHOST_RPC_URL,
|
|
|
|
nickname: 'localhost',
|
|
|
|
},
|
|
|
|
},
|
2018-09-24 18:28:04 +02:00
|
|
|
},
|
|
|
|
appState: {
|
2020-11-03 00:41:28 +01:00
|
|
|
networkDropdownOpen: true,
|
2018-09-24 18:28:04 +02:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2021-10-28 21:31:05 +02:00
|
|
|
|
|
|
|
global.platform = {
|
|
|
|
openExtensionInBrowser: jest.fn(),
|
|
|
|
};
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const store = createMockStore(mockState);
|
2018-09-24 18:28:04 +02:00
|
|
|
|
2022-08-03 22:30:43 +02:00
|
|
|
beforeEach(() => {
|
|
|
|
renderWithProvider(<NetworkDropdown />, store);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-09-24 18:28:04 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('checks background color for first ColorIndicator', () => {
|
2022-08-03 22:30:43 +02:00
|
|
|
const mainnetColorIndicator = screen.queryByTestId('color-icon-mainnet');
|
|
|
|
expect(mainnetColorIndicator).toBeInTheDocument();
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-09-24 18:28:04 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('checks background color for fourth ColorIndicator', () => {
|
2022-08-03 22:30:43 +02:00
|
|
|
const goerliColorIndicator = screen.queryByTestId('color-icon-goerli');
|
|
|
|
expect(goerliColorIndicator).toBeInTheDocument();
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2019-04-17 19:34:49 +02:00
|
|
|
|
2022-09-29 05:26:01 +02:00
|
|
|
it('checks background color for fifth ColorIndicator', () => {
|
2022-09-14 20:26:45 +02:00
|
|
|
const sepoliaColorIndicator = screen.queryByTestId('color-icon-sepolia');
|
|
|
|
expect(sepoliaColorIndicator).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
|
2022-09-29 05:26:01 +02:00
|
|
|
it('checks background color for sixth ColorIndicator', () => {
|
2022-08-03 22:30:43 +02:00
|
|
|
const localhostColorIndicator = screen.queryByTestId(
|
|
|
|
'color-icon-localhost',
|
|
|
|
);
|
|
|
|
expect(localhostColorIndicator).toBeInTheDocument();
|
2021-10-28 21:31:05 +02:00
|
|
|
});
|
|
|
|
|
2023-03-21 17:28:38 +01:00
|
|
|
it('checks background color for seventh ColorIndicator', () => {
|
|
|
|
const lineaColorIndicator = screen.queryByTestId(
|
|
|
|
'color-icon-lineatestnet',
|
|
|
|
);
|
|
|
|
expect(lineaColorIndicator).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
|
2021-10-28 21:31:05 +02:00
|
|
|
it('checks that Add Network button is rendered', () => {
|
2022-08-08 21:01:38 +02:00
|
|
|
const addNetworkButton = screen.queryByText('Add network');
|
2022-08-03 22:30:43 +02:00
|
|
|
expect(addNetworkButton).toBeInTheDocument();
|
2021-10-28 21:31:05 +02:00
|
|
|
});
|
2022-01-05 20:04:34 +01:00
|
|
|
|
|
|
|
it('shows test networks in the dropdown', () => {
|
2022-08-03 22:30:43 +02:00
|
|
|
const networkItems = screen.queryAllByTestId(/network-item/u);
|
2023-03-21 17:28:38 +01:00
|
|
|
expect(networkItems).toHaveLength(7);
|
2022-01-05 20:04:34 +01:00
|
|
|
});
|
2021-10-28 21:31:05 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('NetworkDropdown in appState is true and show test networks is false', () => {
|
|
|
|
const mockState = {
|
|
|
|
metamask: {
|
NetworkController: Split `network` into `networkId` and `networkStatus` (#17556)
The `network` store of the network controller crams two types of data
into one place. It roughly tracks whether we have enough information to
make requests to the network and whether the network is capable of
receiving requests, but it also stores the ID of the network (as
obtained via `net_version`).
Generally we shouldn't be using the network ID for anything, as it has
been completely replaced by chain ID, which all custom RPC endpoints
have been required to support for over a year now. However, as the
network ID is used in various places within the extension codebase,
removing it entirely would be a non-trivial effort. So, minimally, this
commit splits `network` into two stores: `networkId` and
`networkStatus`. But it also expands the concept of network status.
Previously, the network was in one of two states: "loading" and
"not-loading". But now it can be in one of four states:
- `available`: The network is able to receive and respond to requests.
- `unavailable`: The network is not able to receive and respond to
requests for unknown reasons.
- `blocked`: The network is actively blocking requests based on the
user's geolocation. (This is specific to Infura.)
- `unknown`: We don't know whether the network can receive and respond
to requests, either because we haven't checked or we tried to check
and were unsuccessful.
This commit also changes how the network status is determined —
specifically, how many requests are used to determine that status, when
they occur, and whether they are awaited. Previously, the network
controller would make 2 to 3 requests during the course of running
`lookupNetwork`.
* First, if it was an Infura network, it would make a request for
`eth_blockNumber` to determine whether Infura was blocking requests or
not, then emit an appropriate event. This operation was not awaited.
* Then, regardless of the network, it would fetch the network ID via
`net_version`. This operation was awaited.
* Finally, regardless of the network, it would fetch the latest block
via `eth_getBlockByNumber`, then use the result to determine whether
the network supported EIP-1559. This operation was awaited.
Now:
* One fewer request is made, specifically `eth_blockNumber`, as we don't
need to make an extra request to determine whether Infura is blocking
requests; we can reuse `eth_getBlockByNumber`;
* All requests are awaited, which makes `lookupNetwork` run fully
in-band instead of partially out-of-band; and
* Both requests for `net_version` and `eth_getBlockByNumber` are
performed in parallel to make `lookupNetwork` run slightly faster.
2023-03-31 00:49:12 +02:00
|
|
|
networkId: '1',
|
|
|
|
networkStatus: 'available',
|
2021-10-28 21:31:05 +02:00
|
|
|
provider: {
|
|
|
|
type: 'test',
|
|
|
|
},
|
2022-05-31 18:46:38 +02:00
|
|
|
showTestnetMessageInDropdown: false,
|
2021-10-28 21:31:05 +02:00
|
|
|
preferences: {
|
|
|
|
showTestNetworks: false,
|
|
|
|
},
|
2023-03-09 22:00:28 +01:00
|
|
|
networkConfigurations: {
|
|
|
|
networkConfigurationId1: {
|
|
|
|
chainId: '0x1a',
|
|
|
|
rpcUrl: 'http://localhost:7545',
|
|
|
|
},
|
|
|
|
networkConfigurationId2: { rpcUrl: 'http://localhost:7546' },
|
|
|
|
},
|
2021-10-28 21:31:05 +02:00
|
|
|
},
|
|
|
|
appState: {
|
|
|
|
networkDropdownOpen: true,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
global.platform = {
|
|
|
|
openExtensionInBrowser: jest.fn(),
|
|
|
|
};
|
|
|
|
|
|
|
|
const store = createMockStore(mockState);
|
|
|
|
|
2022-08-03 22:30:43 +02:00
|
|
|
beforeEach(() => {
|
|
|
|
renderWithProvider(<NetworkDropdown />, store);
|
2021-10-28 21:31:05 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('checks background color for first ColorIndicator', () => {
|
2022-08-03 22:30:43 +02:00
|
|
|
const mainnetColorIndicator = screen.queryByTestId('color-icon-mainnet');
|
|
|
|
expect(mainnetColorIndicator).toBeInTheDocument();
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-09-24 18:28:04 +02:00
|
|
|
|
2021-10-28 21:31:05 +02:00
|
|
|
it('checks that Add Network button is rendered', () => {
|
2022-08-08 21:01:38 +02:00
|
|
|
const addNetworkButton = screen.queryByText('Add network');
|
2022-08-03 22:30:43 +02:00
|
|
|
expect(addNetworkButton).toBeInTheDocument();
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2022-01-05 20:04:34 +01:00
|
|
|
|
|
|
|
it('does not show test networks in the dropdown', () => {
|
2022-08-03 22:30:43 +02:00
|
|
|
const networkItems = screen.queryAllByTestId(/network-item/u);
|
|
|
|
|
|
|
|
expect(networkItems).toHaveLength(3);
|
2022-01-05 20:04:34 +01:00
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|