Skip to content

Commit

Permalink
test(tests): another portion of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexeyPonomarenko committed May 1, 2022
1 parent e6190b3 commit c066591
Show file tree
Hide file tree
Showing 8 changed files with 493 additions and 48 deletions.
114 changes: 114 additions & 0 deletions astro_2.0/components/SearchInput/tests/SearchInput.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { render } from 'jest/testUtils';

import { SearchInput } from 'astro_2.0/components/SearchInput';
import { fireEvent } from '@testing-library/dom';

jest.mock('astro_2.0/components/LoadingIndicator', () => {
return {
LoadingIndicator: () => <div>LoadingIndicator</div>,
};
});

jest.mock('components/Icon', () => {
return {
Icon: ({ name }: { name: string }) => <div>{name}</div>,
};
});

jest.mock('next-i18next', () => ({
// this mock makes sure any components using the translate hook can use it without a warning being shown
useTranslation: () => {
return {
t: (str: string): string => str,
};
},
}));

describe('SearchInput', () => {
it('Should render loading state', () => {
const { getByText } = render(
<SearchInput onSubmit={() => Promise.resolve(null)} loading />
);

expect(getByText('LoadingIndicator')).toBeInTheDocument();
});

it('Should render search icon', () => {
const { getByText } = render(
<SearchInput
showLoader
loading={false}
onSubmit={() => Promise.resolve(null)}
/>
);

expect(getByText('buttonSearch')).toBeInTheDocument();
});

it('Should render "close" button', () => {
const inputPlaceholder = 'Hello World';

const { getByText, getByPlaceholderText } = render(
<SearchInput
loading={false}
showLoader={false}
placeholder={inputPlaceholder}
onSubmit={() => Promise.resolve(null)}
/>
);

fireEvent.change(getByPlaceholderText(inputPlaceholder), {
target: { value: 'Some Value' },
});

expect(getByText('closeCircle')).toBeInTheDocument();
});

it('Should trigger "onClose"', () => {
const onClose = jest.fn();

const inputPlaceholder = 'Hello World';

const { getByText, getByPlaceholderText } = render(
<SearchInput
loading={false}
onClose={onClose}
showLoader={false}
placeholder={inputPlaceholder}
onSubmit={() => Promise.resolve(null)}
/>
);

fireEvent.change(getByPlaceholderText(inputPlaceholder), {
target: { value: 'Some Value' },
});

fireEvent.click(getByText('closeCircle'));

expect(onClose).toBeCalled();
});

it('Should trigger "onSubmit"', () => {
const onClose = jest.fn();

const inputPlaceholder = 'Hello World';

const { getByText, getByPlaceholderText } = render(
<SearchInput
loading={false}
onClose={onClose}
showLoader={false}
placeholder={inputPlaceholder}
onSubmit={() => Promise.resolve(null)}
/>
);

fireEvent.change(getByPlaceholderText(inputPlaceholder), {
target: { value: 'Some Value' },
});

fireEvent.click(getByText('closeCircle'));

expect(onClose).toBeCalled();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */

import { ReactNode } from 'react';
import { render } from 'jest/testUtils';

import { useFormContext } from 'react-hook-form';

import { SubjectRule } from 'astro_2.0/features/CreateDao/components/SubjectRule';
import { fireEvent } from '@testing-library/dom';

const formContextMock = {
trigger: () => 0,
formState: {
errors: {},
touchedFields: {},
},
};

jest.mock('react-hook-form', () => {
return {
...jest.requireActual('react-hook-form'),
// eslint-disable-next-line @typescript-eslint/no-shadow
Controller: ({ render }: { render: (data: unknown) => ReactNode }) => {
const renderProps = {
field: {
value: '123',
onChange: () => 0,
},
};

return <div>{render(renderProps)}</div>;
},
useFormContext: jest.fn(() => formContextMock),
};
});

jest.mock('next-i18next', () => ({
// this mock makes sure any components using the translate hook can use it without a warning being shown
useTranslation: () => {
return {
t: (str: string): string => str,
};
},
}));

describe('SubjectRule', () => {
it('Should render component', () => {
const title = 'Hello World';

const { getByText } = render(
<SubjectRule title={title} subTitle="Subtitle" subject="proposals" />
);

expect(getByText(title)).toBeTruthy();
});

it('Should handle dao option change', () => {
const subject = 'proposals';

const trigger = jest.fn();

// @ts-ignore
useFormContext.mockImplementation(() => ({
...formContextMock,
trigger,
}));

const { getAllByRole } = render(
<SubjectRule title="Hello World" subTitle="Subtitle" subject={subject} />
);

fireEvent.click(getAllByRole('button')[0]);

expect(trigger).toBeCalledWith(subject);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { ProposalVariant } from 'types/proposal';

import { getNewProposalObject } from 'astro_2.0/features/CreateProposal/helpers/newProposalObject';

import { dao, tokens } from './mock';

jest.mock(
'astro_2.0/features/CreateProposal/helpers/proposalObjectHelpers',
() => {
return {
...jest.requireActual(
'astro_2.0/features/CreateProposal/helpers/proposalObjectHelpers'
),
getAddBountyProposal: () => 'getAddBountyProposal',
getUpgradeCodeProposal: () => 'getUpgradeCodeProposal',
getUpgradeSelfProposal: () => 'getUpgradeSelfProposal',
getRemoveUpgradeCodeProposal: () => 'getRemoveUpgradeCodeProposal',
};
}
);

jest.mock('astro_2.0/features/CreateProposal/helpers/bountiesHelpers', () => {
return {
...jest.requireActual(
'astro_2.0/features/CreateProposal/helpers/bountiesHelpers'
),
getAddBountyProposal: () => 'getAddBountyProposal',
};
});

describe('newProposalObject', () => {
describe('getChangeConfigProposal', () => {
it.each`
type | expectedResult
${ProposalVariant.ProposeGetUpgradeCode} | ${'getUpgradeCodeProposal'}
${ProposalVariant.ProposeRemoveUpgradeCode} | ${'getRemoveUpgradeCodeProposal'}
${ProposalVariant.ProposeUpgradeSelf} | ${'getUpgradeSelfProposal'}
${ProposalVariant.ProposeCreateBounty} | ${'getAddBountyProposal'}
`(
'Should return proposal for $type proposal',
async ({ type, expectedResult }) => {
const data = {
details: 'details',
externalUrl: 'externalUrl',
versionHash: 'versionHash',
};

const result = await getNewProposalObject(
dao,
type,
data,
tokens,
'MyAccount'
);

expect(result).toEqual(expectedResult);
}
);
});
});
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { DAO } from 'types/dao';
import { CreateTokenInput } from 'astro_2.0/features/CreateProposal/types';

import {
BuyNftFromParasInput,
BuyNftFromMintbaseInput,
CustomFunctionCallInput,
getSwapsOnRefProposal,
getUpgradeSelfProposal,
getUpgradeCodeProposal,
getCreateTokenProposal,
getBuyNftFromParasProposal,
getRemoveUpgradeCodeProposal,
getBuyNftFromMintbaseProposal,
getCustomFunctionCallProposal,
getTransferMintbaseNFTProposal,
Expand Down Expand Up @@ -189,4 +194,113 @@ describe('proposalObjectHelpers', () => {
});
});
});

describe('getUpgradeCodeProposal', () => {
it('Should return proposal', () => {
const data = {
versionHash: 'versionHash',
details: 'details',
externalUrl: 'externalUrl',
};

const result = getUpgradeCodeProposal(dao, data);

expect(result).toEqual({
daoId: 'legaldao.sputnikv2.testnet',
description: 'details$$$$externalUrl',
kind: 'FunctionCall',
data: {
receiver_id: 'sputnikv2.testnet',
actions: [
{
method_name: 'store_contract_self',
args: 'eyJjb2RlX2hhc2giOiJ2ZXJzaW9uSGFzaCJ9',
deposit: '6000000000000000000000000',
gas: '220000000000000',
},
],
},
bond: '100000000000000000000000',
});
});
});

describe('getRemoveUpgradeCodeProposal', () => {
it('Should return proposal', () => {
const data = {
versionHash: 'versionHash',
details: 'details',
externalUrl: 'externalUrl',
};

const result = getRemoveUpgradeCodeProposal(dao, data);

expect(result).toEqual({
daoId: 'legaldao.sputnikv2.testnet',
description: 'details$$$$externalUrl',
kind: 'FunctionCall',
data: {
receiver_id: 'sputnikv2.testnet',
actions: [
{
method_name: 'remove_contract_self',
args: 'eyJjb2RlX2hhc2giOiJ2ZXJzaW9uSGFzaCJ9',
deposit: '0',
gas: '220000000000000',
},
],
},
bond: '100000000000000000000000',
});
});
});

describe('getUpgradeSelfProposal', () => {
it('Should return proposal', () => {
const data = {
versionHash: 'versionHash',
details: 'details',
externalUrl: 'externalUrl',
};

const result = getUpgradeSelfProposal(dao, data);

expect(result).toEqual({
daoId: 'legaldao.sputnikv2.testnet',
description: 'details$$$$externalUrl',
kind: 'UpgradeSelf',
data: { hash: 'versionHash' },
bond: '100000000000000000000000',
});
});
});

describe('getCreateTokenProposal', () => {
it('Should return proposal', async () => {
const data = {
details: 'details',
externalUrl: 'externalUrl',
} as CreateTokenInput;

const result = await getCreateTokenProposal(dao, data);

expect(result).toEqual({
daoId: 'legaldao.sputnikv2.testnet',
description: 'details$$$$externalUrl',
kind: 'FunctionCall',
data: {
receiver_id: 'legaldao.sputnikv2.testnet',
actions: [
{
args: 'e30=',
deposit: '6000000000000000000000000',
gas: '220000000000000',
method_name: 'store_contract_self',
},
],
},
bond: '100000000000000000000000',
});
});
});
});
Loading

0 comments on commit c066591

Please sign in to comment.