Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#17 Optimize Unit Test for apps/rollups #21

Merged
merged 1 commit into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions apps/rollups/__tests__/components/Web3Container.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
// PARTICULAR PURPOSE. See the GNU General Public License for more details.

//
import { WalletConnectionProvider } from '@explorer/wallet/src/provider';
import { render, screen } from '@testing-library/react';
import { WalletConnectionProvider } from '@explorer/wallet';
import Web3Container, {
chainIds,
appMetaData,
chainIds,
} from '../../src/components/Web3Container';

const path = '@explorer/wallet';
const path = '@explorer/wallet/src/provider';
jest.mock(path, () => {
const originalModule = jest.requireActual(path);
return {
Expand Down
64 changes: 29 additions & 35 deletions apps/rollups/__tests__/containers/rollups/DApp.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,18 @@ import {
screen,
waitFor,
} from '@testing-library/react';
import { CombinedError } from '@urql/core';
import { Provider, createClient } from 'urql';
import {
hexToJSON,
DApp,
EpochItem,
InputContent,
Inputs,
EpochItem,
DApp,
hexToJSON,
transformPayload,
} from '../../../src/containers/rollups/DApp';
import {
DappQuery,
useDappQuery,
} from '../../../src/generated/graphql/rollups/0.9';
import { useDappQuery } from '../../../src/generated/graphql/rollups/0.9';
import { withChakraTheme } from '../../test-utilities';
import { CombinedError } from '@urql/core';

const path = '../../../src/generated/graphql/rollups/0.9';
jest.mock(path, () => {
const originalModule = jest.requireActual(path);
Expand All @@ -36,6 +33,9 @@ const mockUseDappQuery = useDappQuery as jest.MockedFunction<
typeof useDappQuery
>;

// Create a mock client
const mockClient = createClient({ url: '/graphql' });

const PAYLOAD_STRING = '0x496d616765206e6f74206c6f61646564';
const PAYLOAD_JSON =
'0x7b227473223a2022323032322d31302d31392031323a34353a3037222c20227470223a20312c2022647363223a20224f7574206f6620726f757465222c202264697374616e6365223a20302e32322c2022637572725f636f6f726473223a205b35392e34313337373535393338353435352c2032342e38353330353531323233393538385d2c20226275735f6c696e65223a20223135222c202274726970223a202231353b3133222c202276616c7565223a2031302e38337d';
Expand Down Expand Up @@ -156,29 +156,10 @@ const epochItemProps = {
},
};

const DAppComponent = withChakraTheme(DApp);
const dAppQueryData = {
epochs: {
totalCount: 1,
edges: [edge],
},
inputs: {
totalCount: 0,
},
notices: {
totalCount: 0,
},
reports: {
totalCount: 0,
},
vouchers: {
totalCount: 0,
},
} as unknown as DappQuery;

describe('DApp container', () => {
describe('DApp component', () => {
beforeEach(() => {
mockUseDappQuery.mockClear();
mockUseDappQuery.mockReturnValue([
{
fetching: false,
Expand All @@ -193,7 +174,7 @@ describe('DApp container', () => {
jest.resetAllMocks();
});

it('should display error message', () => {
it('should display error message', async () => {
const errorMessage =
'Warning: There was an error while fetching Dapps.';
mockUseDappQuery.mockReturnValue([
Expand All @@ -206,28 +187,41 @@ describe('DApp container', () => {
},
() => undefined,
]);
render(<DAppComponent address={''} chainId={0} />);

await act(() => {
render(
<Provider value={mockClient}>
<DApp address={''} chainId={0} />
</Provider>
);
});
expect(screen.getByText(errorMessage)).toBeInTheDocument();
});

it('should display spinner while fetching', () => {
it('should display spinner while fetching', async () => {
mockUseDappQuery.mockReturnValue([
{
fetching: true,
stale: false,
},
() => undefined,
]);
render(<DAppComponent address={''} chainId={0} />);
await act(() => {
render(
<Provider value={mockClient}>
<DApp address={''} chainId={0} />
</Provider>
);
});

expect(screen.getByTestId('dapp-spinner')).toBeInTheDocument();
});
});

describe('InputContent component', () => {
it('should display label', () => {
render(<InputContentComponent {...inputContentProps} />);
act(() => {
render(<InputContentComponent {...inputContentProps} />);
});

expect(
screen.getByText(inputContentProps.label)
Expand Down
18 changes: 10 additions & 8 deletions apps/rollups/__tests__/services/useRollupsFactory.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE. See the GNU General Public License for more details.

import { renderHook, cleanup, waitFor } from '@testing-library/react';
import {
CartesiDAppFactory,
CartesiDAppFactory__factory,
} from '@cartesi/rollups';
import { Web3Provider } from '@ethersproject/providers';
import { JsonRpcSigner } from '@ethersproject/providers/src.ts/json-rpc-provider';
import { useWallet } from '@explorer/wallet';
import { CartesiDAppFactory__factory } from '@cartesi/rollups';
import { useNetwork } from '../../src/services/useNetwork';
import { WalletConnectionContextProps } from '@explorer/wallet/src/definitions';
import { cleanup, renderHook, waitFor } from '@testing-library/react';
import { networks, useNetwork } from '../../src/services/useNetwork';
import { useRollupsFactory } from '../../src/services/useRollupsFactory';
import { networks } from '../../src/services/useNetwork';
import { JsonRpcSigner } from '@ethersproject/providers/src.ts/json-rpc-provider';
import { Web3Provider } from '@ethersproject/providers';
import { CartesiDAppFactory } from '@cartesi/rollups';

const walletMod = '@explorer/wallet';
const networkMod = '../../src/services/useNetwork';
Expand Down Expand Up @@ -112,7 +114,7 @@ describe('useRollupsFactory hook', () => {
getSigner: (): JsonRpcSigner =>
'signer' as unknown as JsonRpcSigner,
} as Web3Provider,
});
} as unknown as WalletConnectionContextProps);

await waitFor(() => {
renderHook(() => useRollupsFactory());
Expand Down
9 changes: 4 additions & 5 deletions apps/rollups/__tests__/services/useRollupsGraphQL.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE. See the GNU General Public License for more details.

import { renderHook, cleanup } from '@testing-library/react';
import { useWallet } from '@explorer/wallet';
import { useNetwork } from '../../src/services/useNetwork';
import { useWallet } from '@explorer/wallet/src/useWallet';
import { cleanup, renderHook } from '@testing-library/react';
import { networks, useNetwork } from '../../src/services/useNetwork';
import { useRollupsGraphQL } from '../../src/services/useRollupsGraphQL';
import { networks } from '../../src/services/useNetwork';

const walletMod = '@explorer/wallet';
const walletMod = `@explorer/wallet/src/useWallet`;
const networkMod = '../../src/services/useNetwork';

jest.mock(walletMod, () => {
Expand Down
1 change: 0 additions & 1 deletion apps/rollups/src/services/useRollupsGraphQL.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export const useRollupsGraphQL = (address: string, manualUrl?: string) => {
// Guess generate URL if only address is available
// If manual-URL is passed down (usually is a user input) that will be used instead
const url = isEmpty(manualUrl) ? network.graphql(address) : manualUrl;
console.log(`${manualUrl} - ${url}`);
return createClient({ url });
}, [network, address, manualUrl]);
};