vega-frontend-monorepo/apps/trading/components/web3-container/web3-container.spec.tsx

70 lines
1.8 KiB
TypeScript
Raw Normal View History

2022-03-28 10:19:52 +00:00
import { fireEvent, render, screen, act } from '@testing-library/react';
import { Web3Container } from './web3-container';
const defaultHookValue = {
isActive: false,
error: undefined,
connector: null,
chainId: 3,
};
let mockHookValue;
jest.mock('@web3-react/core', () => {
const original = jest.requireActual('@web3-react/core');
return {
...original,
useWeb3React: jest.fn(() => mockHookValue),
};
});
2022-03-28 10:19:52 +00:00
test('Prompt to connect opens dialog', async () => {
mockHookValue = defaultHookValue;
2022-03-28 10:19:52 +00:00
await act(async () => {
render(
<Web3Container>
<div>Child</div>
</Web3Container>
);
});
expect(screen.queryByText('Child')).not.toBeInTheDocument();
expect(screen.queryByTestId('web3-connector-list')).not.toBeInTheDocument();
expect(screen.getByText('Connect your Ethereum wallet')).toBeInTheDocument();
fireEvent.click(screen.getByText('Connect'));
expect(screen.getByTestId('web3-connector-list')).toBeInTheDocument();
});
2022-03-28 10:19:52 +00:00
test('Error message is shown', async () => {
const message = 'Opps! An error';
mockHookValue = { ...defaultHookValue, error: new Error(message) };
2022-03-28 10:19:52 +00:00
await act(async () => {
render(
<Web3Container>
<div>Child</div>
</Web3Container>
);
});
expect(screen.queryByText('Child')).not.toBeInTheDocument();
expect(screen.getByText(`Something went wrong: ${message}`));
});
2022-03-28 10:19:52 +00:00
test('Chain id matches app configuration', async () => {
const expectedChainId = 4;
mockHookValue = {
...defaultHookValue,
isActive: true,
chainId: expectedChainId,
};
2022-03-28 10:19:52 +00:00
await act(async () => {
render(
<Web3Container>
<div>Child</div>
</Web3Container>
);
});
expect(screen.queryByText('Child')).not.toBeInTheDocument();
expect(screen.getByText(`This app only works on chain ID: 3`));
});