Skip to content

Commit c066591

Browse files
test(tests): another portion of tests
1 parent e6190b3 commit c066591

File tree

8 files changed

+493
-48
lines changed

8 files changed

+493
-48
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import { render } from 'jest/testUtils';
2+
3+
import { SearchInput } from 'astro_2.0/components/SearchInput';
4+
import { fireEvent } from '@testing-library/dom';
5+
6+
jest.mock('astro_2.0/components/LoadingIndicator', () => {
7+
return {
8+
LoadingIndicator: () => <div>LoadingIndicator</div>,
9+
};
10+
});
11+
12+
jest.mock('components/Icon', () => {
13+
return {
14+
Icon: ({ name }: { name: string }) => <div>{name}</div>,
15+
};
16+
});
17+
18+
jest.mock('next-i18next', () => ({
19+
// this mock makes sure any components using the translate hook can use it without a warning being shown
20+
useTranslation: () => {
21+
return {
22+
t: (str: string): string => str,
23+
};
24+
},
25+
}));
26+
27+
describe('SearchInput', () => {
28+
it('Should render loading state', () => {
29+
const { getByText } = render(
30+
<SearchInput onSubmit={() => Promise.resolve(null)} loading />
31+
);
32+
33+
expect(getByText('LoadingIndicator')).toBeInTheDocument();
34+
});
35+
36+
it('Should render search icon', () => {
37+
const { getByText } = render(
38+
<SearchInput
39+
showLoader
40+
loading={false}
41+
onSubmit={() => Promise.resolve(null)}
42+
/>
43+
);
44+
45+
expect(getByText('buttonSearch')).toBeInTheDocument();
46+
});
47+
48+
it('Should render "close" button', () => {
49+
const inputPlaceholder = 'Hello World';
50+
51+
const { getByText, getByPlaceholderText } = render(
52+
<SearchInput
53+
loading={false}
54+
showLoader={false}
55+
placeholder={inputPlaceholder}
56+
onSubmit={() => Promise.resolve(null)}
57+
/>
58+
);
59+
60+
fireEvent.change(getByPlaceholderText(inputPlaceholder), {
61+
target: { value: 'Some Value' },
62+
});
63+
64+
expect(getByText('closeCircle')).toBeInTheDocument();
65+
});
66+
67+
it('Should trigger "onClose"', () => {
68+
const onClose = jest.fn();
69+
70+
const inputPlaceholder = 'Hello World';
71+
72+
const { getByText, getByPlaceholderText } = render(
73+
<SearchInput
74+
loading={false}
75+
onClose={onClose}
76+
showLoader={false}
77+
placeholder={inputPlaceholder}
78+
onSubmit={() => Promise.resolve(null)}
79+
/>
80+
);
81+
82+
fireEvent.change(getByPlaceholderText(inputPlaceholder), {
83+
target: { value: 'Some Value' },
84+
});
85+
86+
fireEvent.click(getByText('closeCircle'));
87+
88+
expect(onClose).toBeCalled();
89+
});
90+
91+
it('Should trigger "onSubmit"', () => {
92+
const onClose = jest.fn();
93+
94+
const inputPlaceholder = 'Hello World';
95+
96+
const { getByText, getByPlaceholderText } = render(
97+
<SearchInput
98+
loading={false}
99+
onClose={onClose}
100+
showLoader={false}
101+
placeholder={inputPlaceholder}
102+
onSubmit={() => Promise.resolve(null)}
103+
/>
104+
);
105+
106+
fireEvent.change(getByPlaceholderText(inputPlaceholder), {
107+
target: { value: 'Some Value' },
108+
});
109+
110+
fireEvent.click(getByText('closeCircle'));
111+
112+
expect(onClose).toBeCalled();
113+
});
114+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/* eslint-disable @typescript-eslint/ban-ts-comment */
2+
3+
import { ReactNode } from 'react';
4+
import { render } from 'jest/testUtils';
5+
6+
import { useFormContext } from 'react-hook-form';
7+
8+
import { SubjectRule } from 'astro_2.0/features/CreateDao/components/SubjectRule';
9+
import { fireEvent } from '@testing-library/dom';
10+
11+
const formContextMock = {
12+
trigger: () => 0,
13+
formState: {
14+
errors: {},
15+
touchedFields: {},
16+
},
17+
};
18+
19+
jest.mock('react-hook-form', () => {
20+
return {
21+
...jest.requireActual('react-hook-form'),
22+
// eslint-disable-next-line @typescript-eslint/no-shadow
23+
Controller: ({ render }: { render: (data: unknown) => ReactNode }) => {
24+
const renderProps = {
25+
field: {
26+
value: '123',
27+
onChange: () => 0,
28+
},
29+
};
30+
31+
return <div>{render(renderProps)}</div>;
32+
},
33+
useFormContext: jest.fn(() => formContextMock),
34+
};
35+
});
36+
37+
jest.mock('next-i18next', () => ({
38+
// this mock makes sure any components using the translate hook can use it without a warning being shown
39+
useTranslation: () => {
40+
return {
41+
t: (str: string): string => str,
42+
};
43+
},
44+
}));
45+
46+
describe('SubjectRule', () => {
47+
it('Should render component', () => {
48+
const title = 'Hello World';
49+
50+
const { getByText } = render(
51+
<SubjectRule title={title} subTitle="Subtitle" subject="proposals" />
52+
);
53+
54+
expect(getByText(title)).toBeTruthy();
55+
});
56+
57+
it('Should handle dao option change', () => {
58+
const subject = 'proposals';
59+
60+
const trigger = jest.fn();
61+
62+
// @ts-ignore
63+
useFormContext.mockImplementation(() => ({
64+
...formContextMock,
65+
trigger,
66+
}));
67+
68+
const { getAllByRole } = render(
69+
<SubjectRule title="Hello World" subTitle="Subtitle" subject={subject} />
70+
);
71+
72+
fireEvent.click(getAllByRole('button')[0]);
73+
74+
expect(trigger).toBeCalledWith(subject);
75+
});
76+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { ProposalVariant } from 'types/proposal';
2+
3+
import { getNewProposalObject } from 'astro_2.0/features/CreateProposal/helpers/newProposalObject';
4+
5+
import { dao, tokens } from './mock';
6+
7+
jest.mock(
8+
'astro_2.0/features/CreateProposal/helpers/proposalObjectHelpers',
9+
() => {
10+
return {
11+
...jest.requireActual(
12+
'astro_2.0/features/CreateProposal/helpers/proposalObjectHelpers'
13+
),
14+
getAddBountyProposal: () => 'getAddBountyProposal',
15+
getUpgradeCodeProposal: () => 'getUpgradeCodeProposal',
16+
getUpgradeSelfProposal: () => 'getUpgradeSelfProposal',
17+
getRemoveUpgradeCodeProposal: () => 'getRemoveUpgradeCodeProposal',
18+
};
19+
}
20+
);
21+
22+
jest.mock('astro_2.0/features/CreateProposal/helpers/bountiesHelpers', () => {
23+
return {
24+
...jest.requireActual(
25+
'astro_2.0/features/CreateProposal/helpers/bountiesHelpers'
26+
),
27+
getAddBountyProposal: () => 'getAddBountyProposal',
28+
};
29+
});
30+
31+
describe('newProposalObject', () => {
32+
describe('getChangeConfigProposal', () => {
33+
it.each`
34+
type | expectedResult
35+
${ProposalVariant.ProposeGetUpgradeCode} | ${'getUpgradeCodeProposal'}
36+
${ProposalVariant.ProposeRemoveUpgradeCode} | ${'getRemoveUpgradeCodeProposal'}
37+
${ProposalVariant.ProposeUpgradeSelf} | ${'getUpgradeSelfProposal'}
38+
${ProposalVariant.ProposeCreateBounty} | ${'getAddBountyProposal'}
39+
`(
40+
'Should return proposal for $type proposal',
41+
async ({ type, expectedResult }) => {
42+
const data = {
43+
details: 'details',
44+
externalUrl: 'externalUrl',
45+
versionHash: 'versionHash',
46+
};
47+
48+
const result = await getNewProposalObject(
49+
dao,
50+
type,
51+
data,
52+
tokens,
53+
'MyAccount'
54+
);
55+
56+
expect(result).toEqual(expectedResult);
57+
}
58+
);
59+
});
60+
});

astro_2.0/features/CreateProposal/helpers/tests/proposalObjectHelpers.spec.ts

+114
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import { DAO } from 'types/dao';
2+
import { CreateTokenInput } from 'astro_2.0/features/CreateProposal/types';
23

34
import {
45
BuyNftFromParasInput,
56
BuyNftFromMintbaseInput,
67
CustomFunctionCallInput,
78
getSwapsOnRefProposal,
9+
getUpgradeSelfProposal,
10+
getUpgradeCodeProposal,
11+
getCreateTokenProposal,
812
getBuyNftFromParasProposal,
13+
getRemoveUpgradeCodeProposal,
914
getBuyNftFromMintbaseProposal,
1015
getCustomFunctionCallProposal,
1116
getTransferMintbaseNFTProposal,
@@ -189,4 +194,113 @@ describe('proposalObjectHelpers', () => {
189194
});
190195
});
191196
});
197+
198+
describe('getUpgradeCodeProposal', () => {
199+
it('Should return proposal', () => {
200+
const data = {
201+
versionHash: 'versionHash',
202+
details: 'details',
203+
externalUrl: 'externalUrl',
204+
};
205+
206+
const result = getUpgradeCodeProposal(dao, data);
207+
208+
expect(result).toEqual({
209+
daoId: 'legaldao.sputnikv2.testnet',
210+
description: 'details$$$$externalUrl',
211+
kind: 'FunctionCall',
212+
data: {
213+
receiver_id: 'sputnikv2.testnet',
214+
actions: [
215+
{
216+
method_name: 'store_contract_self',
217+
args: 'eyJjb2RlX2hhc2giOiJ2ZXJzaW9uSGFzaCJ9',
218+
deposit: '6000000000000000000000000',
219+
gas: '220000000000000',
220+
},
221+
],
222+
},
223+
bond: '100000000000000000000000',
224+
});
225+
});
226+
});
227+
228+
describe('getRemoveUpgradeCodeProposal', () => {
229+
it('Should return proposal', () => {
230+
const data = {
231+
versionHash: 'versionHash',
232+
details: 'details',
233+
externalUrl: 'externalUrl',
234+
};
235+
236+
const result = getRemoveUpgradeCodeProposal(dao, data);
237+
238+
expect(result).toEqual({
239+
daoId: 'legaldao.sputnikv2.testnet',
240+
description: 'details$$$$externalUrl',
241+
kind: 'FunctionCall',
242+
data: {
243+
receiver_id: 'sputnikv2.testnet',
244+
actions: [
245+
{
246+
method_name: 'remove_contract_self',
247+
args: 'eyJjb2RlX2hhc2giOiJ2ZXJzaW9uSGFzaCJ9',
248+
deposit: '0',
249+
gas: '220000000000000',
250+
},
251+
],
252+
},
253+
bond: '100000000000000000000000',
254+
});
255+
});
256+
});
257+
258+
describe('getUpgradeSelfProposal', () => {
259+
it('Should return proposal', () => {
260+
const data = {
261+
versionHash: 'versionHash',
262+
details: 'details',
263+
externalUrl: 'externalUrl',
264+
};
265+
266+
const result = getUpgradeSelfProposal(dao, data);
267+
268+
expect(result).toEqual({
269+
daoId: 'legaldao.sputnikv2.testnet',
270+
description: 'details$$$$externalUrl',
271+
kind: 'UpgradeSelf',
272+
data: { hash: 'versionHash' },
273+
bond: '100000000000000000000000',
274+
});
275+
});
276+
});
277+
278+
describe('getCreateTokenProposal', () => {
279+
it('Should return proposal', async () => {
280+
const data = {
281+
details: 'details',
282+
externalUrl: 'externalUrl',
283+
} as CreateTokenInput;
284+
285+
const result = await getCreateTokenProposal(dao, data);
286+
287+
expect(result).toEqual({
288+
daoId: 'legaldao.sputnikv2.testnet',
289+
description: 'details$$$$externalUrl',
290+
kind: 'FunctionCall',
291+
data: {
292+
receiver_id: 'legaldao.sputnikv2.testnet',
293+
actions: [
294+
{
295+
args: 'e30=',
296+
deposit: '6000000000000000000000000',
297+
gas: '220000000000000',
298+
method_name: 'store_contract_self',
299+
},
300+
],
301+
},
302+
bond: '100000000000000000000000',
303+
});
304+
});
305+
});
192306
});

0 commit comments

Comments
 (0)