2022-03-28 10:19:52 +00:00
|
|
|
import { fireEvent, render, screen, act } from '@testing-library/react';
|
2022-03-25 07:44:10 +00:00
|
|
|
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 () => {
|
2022-03-25 07:44:10 +00:00
|
|
|
mockHookValue = defaultHookValue;
|
2022-03-28 10:19:52 +00:00
|
|
|
await act(async () => {
|
|
|
|
render(
|
|
|
|
<Web3Container>
|
|
|
|
<div>Child</div>
|
|
|
|
</Web3Container>
|
|
|
|
);
|
|
|
|
});
|
2022-03-25 07:44:10 +00:00
|
|
|
|
|
|
|
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 () => {
|
2022-03-25 07:44:10 +00:00
|
|
|
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>
|
|
|
|
);
|
|
|
|
});
|
2022-03-25 07:44:10 +00:00
|
|
|
|
|
|
|
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 () => {
|
2022-03-25 07:44:10 +00:00
|
|
|
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>
|
|
|
|
);
|
|
|
|
});
|
2022-03-25 07:44:10 +00:00
|
|
|
|
|
|
|
expect(screen.queryByText('Child')).not.toBeInTheDocument();
|
|
|
|
expect(screen.getByText(`This app only works on chain ID: 3`));
|
|
|
|
});
|