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

#3 Improve test coverage for apps/staking #4

Merged
merged 21 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
6cc281a
test(staking): Add unit tests for base components
nevendyulgerov May 31, 2023
356e765
test(staking): Remove directories from test coverage
nevendyulgerov May 31, 2023
1e1165d
test(staking): Add unit tests for block components
nevendyulgerov Jun 1, 2023
8c9ba8b
test(staking): Add unit tests for home and users components
nevendyulgerov Jun 1, 2023
4bc893d
test(staking): Add unit tests for pools components
nevendyulgerov Jun 2, 2023
b0f690a
test(staking): Add unit tests for utils
nevendyulgerov Jun 2, 2023
97cdb7a
test(staking) Add unit tests for services and contexts
nevendyulgerov Jun 4, 2023
3e99a75
test(staking): Add unit tests for services
nevendyulgerov Jun 5, 2023
70cc975
test(staking): Add unit tests for services
nevendyulgerov Jun 5, 2023
683ff70
test(staking): Add unit tests for pool and poolFactory services
nevendyulgerov Jun 6, 2023
5256fcd
test(staking): Add unit tests for node and pool services
nevendyulgerov Jun 7, 2023
36a299e
test(staking): Add unit tests for dateParser, featureFlags and string…
nevendyulgerov Jun 7, 2023
4455beb
test(staking): Add unit tests for Layout and ConfirmationIndicator
nevendyulgerov Jun 7, 2023
a157c5e
test(staking): Add unit tests for node components
nevendyulgerov Jun 7, 2023
9817f51
test(staking): Add unit tests for pools and stake components
nevendyulgerov Jun 8, 2023
154bed1
test(staking): Add unit tests for base and node components
nevendyulgerov Jun 9, 2023
84caea7
test(staking): Add unit tests for base, home and stake components
nevendyulgerov Jun 9, 2023
71185d6
test(staking): Add unit tests for useBlocks graphql hook
nevendyulgerov Jun 9, 2023
e76051b
test(staking): Fix failing test
nevendyulgerov Jun 9, 2023
5f66e46
test(staking): Fix failing test
nevendyulgerov Jun 9, 2023
f861760
fix(staking): Fix ts error for event type
nevendyulgerov Jun 12, 2023
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
81 changes: 81 additions & 0 deletions apps/staking/__tests__/components/ApolloContainer.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright (C) 2023 Cartesi Pte. Ltd.

// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.

// This program is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE. See the GNU General Public License for more details.

import { cleanup, render, screen } from '@testing-library/react';
import { useWallet } from '@explorer/wallet';
import { useApollo } from '../../src/services/apollo';
import ApolloContainer from '../../src/components/ApolloContainer';
import { withChakraTheme } from '../test-utilities';

jest.mock('@apollo/client', () => {
const originalModule = jest.requireActual('@apollo/client');
return {
__esModule: true,
...originalModule,
ApolloProvider: ({ children }) => <div>{children}</div>,
};
});

jest.mock('../../src/services/apollo', () => {
const originalModule = jest.requireActual('../../src/services/apollo');
return {
__esModule: true,
...originalModule,
useApollo: jest.fn(),
};
});

jest.mock('@explorer/wallet', () => {
const originalModule = jest.requireActual('@explorer/wallet');
return {
__esModule: true,
...originalModule,
useWallet: jest.fn(),
};
});

const account = '0x907eA0e65Ecf3af503007B382E1280Aeb46104ad';
const mockedUseWallet = useWallet as jest.MockedFunction<typeof useWallet>;
const mockedUseApollo = useApollo as jest.MockedFunction<typeof useApollo>;
const walletData = {
account,
active: true,
activate: jest.fn(),
deactivate: jest.fn(),
chainId: 3,
};

const Component = withChakraTheme(ApolloContainer);

describe('ApolloContainer component', () => {
beforeEach(() => {
mockedUseWallet.mockReturnValue(walletData);
});

afterEach(() => {
cleanup();
jest.clearAllMocks();
});

it('should display correct content', () => {
const content = 'Content';
render(<Component>{content}</Component>);
expect(screen.getByText(content)).toBeInTheDocument();
});

it('should invoke useApollo hook with chain id', () => {
const implementation = jest.fn();
mockedUseApollo.mockImplementation(implementation);

render(<Component>Content</Component>);
expect(implementation).toHaveBeenCalledWith(walletData.chainId);
});
});
25 changes: 25 additions & 0 deletions apps/staking/__tests__/components/AttentionIcon.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (C) 2023 Cartesi Pte. Ltd.

// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.

// This program is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE. See the GNU General Public License for more details.

import { render, screen } from '@testing-library/react';
import AttentionIcon from '../../src/components/AttentionIcon';
import { withChakraTheme } from '../test-utilities';

const Component = withChakraTheme(AttentionIcon);

describe('AttentionIcon component', () => {
it('should append additional props', () => {
const value = 'attention-icon';
render(<Component data-testid={value}>Content</Component>);

expect(screen.getByTestId(value)).toBeInTheDocument();
});
});
33 changes: 33 additions & 0 deletions apps/staking/__tests__/components/ConfirmationIndicator.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (C) 2023 Cartesi Pte. Ltd.

// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.

// This program is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE. See the GNU General Public License for more details.

import { render, screen } from '@testing-library/react';
import ConfirmationIndicator from '../../src/components/ConfirmationIndicator';
import { withChakraTheme } from '../test-utilities';

const Component = withChakraTheme(ConfirmationIndicator);

describe('ConfirmationIndicator component', () => {
it('should display correct label while loading', () => {
render(<Component loading />);
expect(screen.getByText('Pending')).toBeInTheDocument();
});

it('should display correct label when not loading', () => {
render(<Component loading={false} />);
expect(screen.getByText('Success')).toBeInTheDocument();
});

it('should display correct label when error has occurred', () => {
render(<Component loading={false} error="error" />);
expect(screen.getByText('Failure')).toBeInTheDocument();
});
});
35 changes: 35 additions & 0 deletions apps/staking/__tests__/components/IconLink.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (C) 2023 Cartesi Pte. Ltd.

// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.

// This program is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE. See the GNU General Public License for more details.

import { render, screen } from '@testing-library/react';
import IconLink from '../../src/components/IconLink';
import { withChakraTheme } from '../test-utilities';

const Component = withChakraTheme(IconLink);
const props = {
href: 'https://google.com',
tooltip: 'Google',
icon: 'Link to Google',
};

describe('IconLink component', () => {
it('should display correct link', () => {
const { container } = render(<Component {...props} />);
const link = container.querySelector('a');

expect(link.getAttribute('href')).toBe(props.href);
});

it('should display link icon', () => {
render(<Component {...props} />);
expect(screen.getByText(props.icon)).toBeInTheDocument();
});
});
115 changes: 115 additions & 0 deletions apps/staking/__tests__/components/Layout.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Copyright (C) 2023 Cartesi Pte. Ltd.

// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.

// This program is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE. See the GNU General Public License for more details.

import { cleanup, render, screen } from '@testing-library/react';
import { StakingImpl, PoS } from '@cartesi/pos';
import Layout, { headerLinks, footerLinks } from '../../src/components/Layout';
import {
useCartesiTokenContract,
usePoSContract,
useSimpleFaucetContract,
useStakingContract,
useStakingPoolFactoryContract,
useWorkerManagerContract,
} from '../../src/services/contracts';
import useMeta from '../../src/graphql/hooks/useMeta';
import { withChakraTheme } from '../test-utilities';
import { CartesiToken, SimpleFaucet } from '@cartesi/token';
import { StakingPoolFactoryImpl } from '@cartesi/staking-pool';
import { WorkerManagerAuthManagerImpl } from '@cartesi/util';

jest.mock('../../src/services/contracts', () => {
const original = jest.requireActual('../../src/services/contracts');
return {
__esModule: true,
...original,
useCartesiTokenContract: jest.fn(),
usePoSContract: jest.fn(),
useSimpleFaucetContract: jest.fn(),
useStakingContract: jest.fn(),
useStakingPoolFactoryContract: jest.fn(),
useWorkerManagerContract: jest.fn(),
};
});

jest.mock('../../src/graphql/hooks/useMeta');

const address = '0x2942aa4356783892c624125acfbbb80d29629a9d';
const mockedUseCartesiTokenContract =
useCartesiTokenContract as jest.MockedFunction<
typeof useCartesiTokenContract
>;
const mockedUsePoSContract = usePoSContract as jest.MockedFunction<
typeof usePoSContract
>;
const mockedUseSimpleFaucetContract =
useSimpleFaucetContract as jest.MockedFunction<
typeof useSimpleFaucetContract
>;
const mockedUseStakingContract = useStakingContract as jest.MockedFunction<
typeof useStakingContract
>;
const mockedUseStakingPoolFactoryContract =
useStakingPoolFactoryContract as jest.MockedFunction<
typeof useStakingPoolFactoryContract
>;
const mockedUseWorkerManagerContract =
useWorkerManagerContract as jest.MockedFunction<
typeof useWorkerManagerContract
>;
const mockedUseMeta = useMeta as jest.MockedFunction<typeof useMeta>;

const Component = withChakraTheme(Layout);

describe('Layout component', () => {
beforeEach(() => {
mockedUsePoSContract.mockReturnValue({
address,
} as unknown as PoS);
mockedUseCartesiTokenContract.mockReturnValue({
address,
} as unknown as CartesiToken);
mockedUseSimpleFaucetContract.mockReturnValue({
address,
} as unknown as SimpleFaucet);
mockedUseStakingContract.mockReturnValue({
address,
} as unknown as StakingImpl);
mockedUseStakingPoolFactoryContract.mockReturnValue({
address,
} as unknown as StakingPoolFactoryImpl);
mockedUseWorkerManagerContract.mockReturnValue({
address,
} as unknown as WorkerManagerAuthManagerImpl);
mockedUseMeta.mockReturnValue(undefined);
});

afterEach(() => {
jest.clearAllMocks();
cleanup();
});

it('should display correct header links', () => {
render(<Component>Footer</Component>);

headerLinks.forEach((link) => {
expect(screen.getByText(link.label)).toBeInTheDocument();
});
});

it('should display correct footer links', () => {
render(<Component>Footer</Component>);

footerLinks.forEach((link) => {
expect(screen.getByText(link.label)).toBeInTheDocument();
});
});
});
40 changes: 40 additions & 0 deletions apps/staking/__tests__/components/MarketInfo.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (C) 2023 Cartesi Pte. Ltd.

// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.

// This program is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE. See the GNU General Public License for more details.

import { render, screen } from '@testing-library/react';
import MarketInfo, { MarketInfoUnit } from '../../src/components/MarketInfo';
import { withChakraTheme } from '../test-utilities';

const Component = withChakraTheme(MarketInfo);
const props = {
label: 'Some label',
value: 10,
unit: 'USD' as MarketInfoUnit,
};

describe('MarketInfo component', () => {
it('should display label', () => {
render(<Component {...props} />);
expect(screen.getByText(props.label)).toBeInTheDocument();
});

it('should display unit when value is larger than zero', () => {
render(<Component {...props} />);
expect(screen.getByText(props.unit)).toBeInTheDocument();
});

it('should not display unit when value is zero', () => {
render(<Component {...props} value={0} />);
expect(() => screen.getByText(props.unit)).toThrow(
'Unable to find an element'
);
});
});
26 changes: 26 additions & 0 deletions apps/staking/__tests__/components/PageHeader.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (C) 2023 Cartesi Pte. Ltd.

// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.

// This program is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE. See the GNU General Public License for more details.

import { render, screen } from '@testing-library/react';
import PageHeader from '../../src/components/PageHeader';
import { withChakraTheme } from '../test-utilities';

const Component = withChakraTheme(PageHeader);
const props = {
title: 'Some title',
};

describe('PageHeader component', () => {
it('should display title', () => {
render(<Component {...props} />);
expect(screen.getByText(props.title)).toBeInTheDocument();
});
});
Loading