afeb772702
* feat: add node swicther * chore: remove hook form from node switcher * feat: generate apollo types and add tests * fix: format * fix: types * fix: remove redundant wrapper * fix: layout styles * fix: add controlled value to radio group * fix: flaky node hook test * feat: add custom input for node and more tests * feat: wip refactor config hook to run on init * fix: dont open node switcher on init * fix: lint * fix: lint * fix: cache key error * fix: format * fix: lint * feat: validate connected node on init WIP * chore: refactor useconfig and usenodes * fix: revert custom node branch changes * feat: fix config loading errors and custom node handling * feat: add test for nodes init * fix: env error states * fix: add more tests * fix: format * fix: lint and format * fix: mock type in test * fix: clean up queries * fix: node block heights * fix: make git variables optional * fix: dialog width on lg screens * feat: improve mobile looks * fix: format * fix: remove commented out styles * fix: use node data url instead of key * fix: clean up node switcher dialog props * fix: add missing title and subtitle for dialog * fix: show confiug load errors
156 lines
4.1 KiB
TypeScript
156 lines
4.1 KiB
TypeScript
import { fireEvent, render, screen } from '@testing-library/react';
|
|
import type { MockedResponse } from '@apollo/client/testing';
|
|
import { MockedProvider } from '@apollo/client/testing';
|
|
import { Web3Container } from './web3-container';
|
|
import type { useWeb3React } from '@web3-react/core';
|
|
import type { NetworkParamsQuery } from '@vegaprotocol/web3';
|
|
import { NETWORK_PARAMS_QUERY } from '@vegaprotocol/web3';
|
|
import { EnvironmentProvider } from '@vegaprotocol/environment';
|
|
|
|
const defaultHookValue = {
|
|
isActive: false,
|
|
error: undefined,
|
|
connector: null,
|
|
chainId: 3,
|
|
} as unknown as ReturnType<typeof useWeb3React>;
|
|
let mockHookValue: ReturnType<typeof useWeb3React>;
|
|
|
|
const mockEthereumConfig = {
|
|
network_id: '3',
|
|
chain_id: '3',
|
|
confirmations: 3,
|
|
collateral_bridge_contract: {
|
|
address: 'bridge address',
|
|
},
|
|
};
|
|
|
|
const networkParamsQueryMock: MockedResponse<NetworkParamsQuery> = {
|
|
request: {
|
|
query: NETWORK_PARAMS_QUERY,
|
|
},
|
|
result: {
|
|
data: {
|
|
networkParameters: [
|
|
{
|
|
__typename: 'NetworkParameter',
|
|
key: 'blockchains.ethereumConfig',
|
|
value: JSON.stringify(mockEthereumConfig),
|
|
},
|
|
],
|
|
},
|
|
},
|
|
};
|
|
|
|
const mockEnvironment = {
|
|
VEGA_ENV: 'TESTNET',
|
|
VEGA_URL: 'https://vega-node.url',
|
|
VEGA_NETWORKS: JSON.stringify({}),
|
|
GIT_BRANCH: 'test',
|
|
GIT_COMMIT_HASH: 'abcdef',
|
|
GIT_ORIGIN_URL: 'https://github.com/test/repo',
|
|
};
|
|
|
|
jest.mock('@web3-react/core', () => {
|
|
const original = jest.requireActual('@web3-react/core');
|
|
return {
|
|
...original,
|
|
useWeb3React: jest.fn(() => mockHookValue),
|
|
};
|
|
});
|
|
|
|
function setup(mock = networkParamsQueryMock) {
|
|
return render(
|
|
<EnvironmentProvider definitions={mockEnvironment}>
|
|
<MockedProvider mocks={[mock]}>
|
|
<Web3Container>
|
|
<div>
|
|
<div>Child</div>
|
|
<div>{mockEthereumConfig.collateral_bridge_contract.address}</div>
|
|
</div>
|
|
</Web3Container>
|
|
</MockedProvider>
|
|
</EnvironmentProvider>
|
|
);
|
|
}
|
|
|
|
it('Prompt to connect opens dialog', async () => {
|
|
mockHookValue = defaultHookValue;
|
|
setup();
|
|
|
|
expect(screen.getByText('Loading...')).toBeInTheDocument();
|
|
expect(
|
|
await screen.findByText('Connect your Ethereum wallet')
|
|
).toBeInTheDocument();
|
|
|
|
expect(screen.queryByText('Child')).not.toBeInTheDocument();
|
|
expect(screen.queryByTestId('web3-connector-list')).not.toBeInTheDocument();
|
|
|
|
fireEvent.click(screen.getByText('Connect'));
|
|
expect(screen.getByTestId('web3-connector-list')).toBeInTheDocument();
|
|
});
|
|
|
|
it('Error message is shown', async () => {
|
|
const message = 'Opps! An error';
|
|
mockHookValue = { ...defaultHookValue, error: new Error(message) };
|
|
setup();
|
|
|
|
expect(screen.getByText('Loading...')).toBeInTheDocument();
|
|
expect(
|
|
await screen.findByText(`Something went wrong: ${message}`)
|
|
).toBeInTheDocument();
|
|
expect(screen.queryByText('Child')).not.toBeInTheDocument();
|
|
});
|
|
|
|
it('Checks that chain ID matches app ID', async () => {
|
|
const expectedChainId = 4;
|
|
mockHookValue = {
|
|
...defaultHookValue,
|
|
isActive: true,
|
|
chainId: expectedChainId,
|
|
};
|
|
setup();
|
|
|
|
expect(screen.getByText('Loading...')).toBeInTheDocument();
|
|
expect(
|
|
await screen.findByText(`This app only works on chain ID: 3`)
|
|
).toBeInTheDocument();
|
|
expect(screen.queryByText('Child')).not.toBeInTheDocument();
|
|
});
|
|
|
|
it('Passes ethereum config to children', async () => {
|
|
mockHookValue = {
|
|
...defaultHookValue,
|
|
isActive: true,
|
|
};
|
|
setup();
|
|
|
|
expect(screen.getByText('Loading...')).toBeInTheDocument();
|
|
expect(
|
|
await screen.findByText(
|
|
mockEthereumConfig.collateral_bridge_contract.address
|
|
)
|
|
).toBeInTheDocument();
|
|
});
|
|
|
|
it('Shows no config found message if the network parameter doesnt exist', async () => {
|
|
const mock: MockedResponse<NetworkParamsQuery> = {
|
|
request: {
|
|
query: NETWORK_PARAMS_QUERY,
|
|
},
|
|
result: {
|
|
data: {
|
|
networkParameters: [
|
|
{
|
|
__typename: 'NetworkParameter',
|
|
key: 'nope',
|
|
value: 'foo',
|
|
},
|
|
],
|
|
},
|
|
},
|
|
};
|
|
setup(mock);
|
|
expect(screen.getByText('Loading...')).toBeInTheDocument();
|
|
expect(await screen.findByText('No data')).toBeInTheDocument();
|
|
});
|