Wrap Web3Container with act

This commit is contained in:
Bartłomiej Głownia 2022-03-28 12:19:52 +02:00
parent e3a1142579
commit 6e6a79d8e3

View File

@ -1,4 +1,4 @@
import { fireEvent, render, screen } from '@testing-library/react'; import { fireEvent, render, screen, act } from '@testing-library/react';
import { Web3Container } from './web3-container'; import { Web3Container } from './web3-container';
const defaultHookValue = { const defaultHookValue = {
@ -17,13 +17,15 @@ jest.mock('@web3-react/core', () => {
}; };
}); });
test('Prompt to connect opens dialog', () => { test('Prompt to connect opens dialog', async () => {
mockHookValue = defaultHookValue; mockHookValue = defaultHookValue;
render( await act(async () => {
<Web3Container> render(
<div>Child</div> <Web3Container>
</Web3Container> <div>Child</div>
); </Web3Container>
);
});
expect(screen.queryByText('Child')).not.toBeInTheDocument(); expect(screen.queryByText('Child')).not.toBeInTheDocument();
expect(screen.queryByTestId('web3-connector-list')).not.toBeInTheDocument(); expect(screen.queryByTestId('web3-connector-list')).not.toBeInTheDocument();
@ -32,33 +34,35 @@ test('Prompt to connect opens dialog', () => {
expect(screen.getByTestId('web3-connector-list')).toBeInTheDocument(); expect(screen.getByTestId('web3-connector-list')).toBeInTheDocument();
}); });
test('Error message is shown', () => { test('Error message is shown', async () => {
const message = 'Opps! An error'; const message = 'Opps! An error';
mockHookValue = { ...defaultHookValue, error: new Error(message) }; mockHookValue = { ...defaultHookValue, error: new Error(message) };
await act(async () => {
render( render(
<Web3Container> <Web3Container>
<div>Child</div> <div>Child</div>
</Web3Container> </Web3Container>
); );
});
expect(screen.queryByText('Child')).not.toBeInTheDocument(); expect(screen.queryByText('Child')).not.toBeInTheDocument();
expect(screen.getByText(`Something went wrong: ${message}`)); expect(screen.getByText(`Something went wrong: ${message}`));
}); });
test('Chain id matches app configuration', () => { test('Chain id matches app configuration', async () => {
const expectedChainId = 4; const expectedChainId = 4;
mockHookValue = { mockHookValue = {
...defaultHookValue, ...defaultHookValue,
isActive: true, isActive: true,
chainId: expectedChainId, chainId: expectedChainId,
}; };
await act(async () => {
render( render(
<Web3Container> <Web3Container>
<div>Child</div> <div>Child</div>
</Web3Container> </Web3Container>
); );
});
expect(screen.queryByText('Child')).not.toBeInTheDocument(); expect(screen.queryByText('Child')).not.toBeInTheDocument();
expect(screen.getByText(`This app only works on chain ID: 3`)); expect(screen.getByText(`This app only works on chain ID: 3`));