-
Notifications
You must be signed in to change notification settings - Fork 298
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
WEBREL-3 / Kate / Test coverage: composite-calendar-mobile & calendar-icon in Reports #15561
Merged
balakrishna-deriv
merged 5 commits into
binary-com:master
from
kate-deriv:kate/WEBREL-3/tests_calendar_mobile_reports
Jun 13, 2024
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6304691
refactor: add tests for calendar icon file
kate-deriv 2d35f73
Merge branch 'binary-com:master' into kate/WEBREL-3/tests_calendar_mo…
kate-deriv 9255956
refactor: add tests for composite calendar mobile file
kate-deriv c130949
refactor: extract some testing logic into a function
kate-deriv 669eb67
chore: grammar
kate-deriv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
packages/reports/src/Components/Form/CompositeCalendar/__tests__/calendar-icon.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,24 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import CalendarIcon from '../calendar-icon'; | ||
|
||
const mockDefaultProps = { | ||
onClick: jest.fn(), | ||
}; | ||
|
||
describe('CalendarIcon', () => { | ||
it('should render component with default props', () => { | ||
render(<CalendarIcon {...mockDefaultProps} />); | ||
|
||
expect(screen.getByTestId('dt_calendar_icon')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should call function onClick from props if user clicks on the component', () => { | ||
render(<CalendarIcon {...mockDefaultProps} />); | ||
|
||
expect(mockDefaultProps.onClick).not.toBeCalled(); | ||
userEvent.click(screen.getByTestId('dt_calendar_icon')); | ||
expect(mockDefaultProps.onClick).toBeCalled(); | ||
}); | ||
}); |
148 changes: 148 additions & 0 deletions
148
...eports/src/Components/Form/CompositeCalendar/__tests__/composite-calendar-mobile.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,148 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import CompositeCalendarMobile from '../composite-calendar-mobile'; | ||
import { toMoment } from '@deriv/shared'; | ||
|
||
const startDate = 'Start date'; | ||
const endDate = 'End date'; | ||
const backToTodayButtonText = 'Back to today'; | ||
const radioButtonText = ['All time', 'Last 7 days', 'Last 30 days', 'Last 365 days']; | ||
const customDateRangeText = 'Custom'; | ||
const mockDefaultProps = { | ||
duration_list: [ | ||
{ value: 'all_time', label: radioButtonText[0], onClick: jest.fn() }, | ||
{ value: 'last_7_days', label: radioButtonText[1], onClick: jest.fn() }, | ||
{ value: 'last_30_days', label: radioButtonText[2], onClick: jest.fn() }, | ||
{ value: 'last_365_days', label: radioButtonText[3], onClick: jest.fn() }, | ||
], | ||
from: 1696319493, | ||
to: 1715346191, | ||
onChange: jest.fn(), | ||
setCurrentFocus: jest.fn(), | ||
}; | ||
|
||
jest.mock('@deriv/components', () => ({ | ||
...jest.requireActual('@deriv/components'), | ||
MobileDialog: jest.fn(({ children, visible, footer, onClose }) => ( | ||
<div> | ||
{visible && ( | ||
<div> | ||
{children} | ||
{footer} | ||
<button onClick={onClose}>Close MobileDialog</button> | ||
</div> | ||
)} | ||
</div> | ||
)), | ||
})); | ||
|
||
describe('CompositeCalendarMobile', () => { | ||
const checkModalOpenCloseFunctionality = (buttonName?: string) => { | ||
userEvent.click(screen.getByRole('textbox')); | ||
|
||
radioButtonText.forEach(item => expect(screen.getByText(item)).toBeInTheDocument()); | ||
expect(screen.getByText(customDateRangeText)).toBeInTheDocument(); | ||
expect(screen.getByPlaceholderText(startDate)).toBeInTheDocument(); | ||
expect(screen.getByText(backToTodayButtonText)).toBeInTheDocument(); | ||
|
||
if (!buttonName) return; | ||
|
||
userEvent.click(screen.getByText(buttonName)); | ||
|
||
radioButtonText.forEach(item => expect(screen.queryByText(item)).not.toBeInTheDocument()); | ||
expect(screen.queryByText(customDateRangeText)).not.toBeInTheDocument(); | ||
expect(screen.queryByPlaceholderText(startDate)).not.toBeInTheDocument(); | ||
expect(screen.queryByText(backToTodayButtonText)).not.toBeInTheDocument(); | ||
}; | ||
|
||
it('should render the input field by default and not render MobileDialog with children if the Modal is closed (default state)', () => { | ||
render(<CompositeCalendarMobile {...mockDefaultProps} />); | ||
|
||
expect(screen.getByRole('textbox')).toBeInTheDocument(); | ||
radioButtonText.forEach(item => expect(screen.queryByText(item)).not.toBeInTheDocument()); | ||
expect(screen.queryByText(customDateRangeText)).not.toBeInTheDocument(); | ||
expect(screen.queryByPlaceholderText(startDate)).not.toBeInTheDocument(); | ||
expect(screen.queryByText(backToTodayButtonText)).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('should render functioning component if props "from" and "to" are equal to 0: MobileDialog should be opened if user clicks on the calendar field', () => { | ||
render(<CompositeCalendarMobile {...mockDefaultProps} from={0} to={0} />); | ||
|
||
radioButtonText.forEach(item => expect(screen.queryByText(item)).not.toBeInTheDocument()); | ||
expect(screen.queryByText(customDateRangeText)).not.toBeInTheDocument(); | ||
expect(screen.queryByPlaceholderText(startDate)).not.toBeInTheDocument(); | ||
expect(screen.queryByText(backToTodayButtonText)).not.toBeInTheDocument(); | ||
|
||
checkModalOpenCloseFunctionality(); | ||
}); | ||
|
||
it('should close the MobileDialog if user clicks on Cancel button', () => { | ||
render(<CompositeCalendarMobile {...mockDefaultProps} />); | ||
|
||
checkModalOpenCloseFunctionality('Cancel'); | ||
}); | ||
|
||
it('should close the MobileDialog if user clicks on Close button', () => { | ||
render(<CompositeCalendarMobile {...mockDefaultProps} />); | ||
|
||
checkModalOpenCloseFunctionality('Close MobileDialog'); | ||
}); | ||
|
||
it('should close the MobileDialog if user clicks on "Back to today" button', () => { | ||
render(<CompositeCalendarMobile {...mockDefaultProps} />); | ||
|
||
checkModalOpenCloseFunctionality(backToTodayButtonText); | ||
}); | ||
|
||
it('should apply new value and set it to the input if user chooses some radio button value and clicks on OK button', () => { | ||
render(<CompositeCalendarMobile {...mockDefaultProps} />); | ||
|
||
const input = screen.getByRole('textbox'); | ||
|
||
expect(input).toHaveValue(radioButtonText[0]); | ||
userEvent.click(input); | ||
userEvent.click(screen.getByText(radioButtonText[1])); | ||
|
||
userEvent.click(screen.getByText('OK')); | ||
expect(input).toHaveValue(radioButtonText[1]); | ||
}); | ||
|
||
it('should apply custom value and set it to the input if user clicks on Custom radio button value and clicks on OK button', () => { | ||
render(<CompositeCalendarMobile {...mockDefaultProps} />); | ||
|
||
const input = screen.getByRole('textbox'); | ||
|
||
expect(input).toHaveValue(radioButtonText[0]); | ||
userEvent.click(input); | ||
userEvent.click(screen.getByText(customDateRangeText)); | ||
|
||
userEvent.click(screen.getByText('OK')); | ||
expect(input).toHaveValue('03 Oct 2023 - 10 May 2024'); | ||
}); | ||
|
||
it('should apply date which user type in DatePicker for Start date', () => { | ||
render(<CompositeCalendarMobile {...mockDefaultProps} />); | ||
|
||
userEvent.click(screen.getByRole('textbox')); | ||
|
||
const inputForStartDate = screen.getByPlaceholderText(startDate); | ||
expect(inputForStartDate).toHaveValue('03 Oct 2023'); | ||
userEvent.type(inputForStartDate, '10 Jun 2024'); | ||
|
||
expect(inputForStartDate).toHaveValue('10 Jun 2024'); | ||
}); | ||
|
||
it('should apply date which user type in DatePicker for End date', () => { | ||
render(<CompositeCalendarMobile {...mockDefaultProps} />); | ||
|
||
userEvent.click(screen.getByRole('textbox')); | ||
|
||
const inputForEndDate = screen.getByPlaceholderText(endDate); | ||
expect(inputForEndDate).toHaveValue('10 May 2024'); | ||
|
||
const newDate = toMoment().format('DD MMM YYYY'); | ||
userEvent.type(inputForEndDate, newDate); | ||
expect(inputForEndDate).toHaveValue(newDate); | ||
}); | ||
}); |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit-pick) same for the next test case)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, will correct mistake)