-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(tests): another portion of tests
- Loading branch information
1 parent
e6190b3
commit c066591
Showing
8 changed files
with
493 additions
and
48 deletions.
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
astro_2.0/components/SearchInput/tests/SearchInput.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
76 changes: 76 additions & 0 deletions
76
astro_2.0/features/CreateDao/components/SubjectRule/tests/SubjectRule.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
60 changes: 60 additions & 0 deletions
60
astro_2.0/features/CreateProposal/helpers/tests/newProposalObject.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.