2022-08-07 13:59:04 +03:00
|
|
|
import { screen, waitFor } from '@testing-library/react';
|
2020-12-15 20:57:23 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
2023-02-18 12:40:37 +03:00
|
|
|
import type { DateRangeSelectorProps } from '../../../src/utils/dates/DateRangeSelector';
|
|
|
|
import { DateRangeSelector } from '../../../src/utils/dates/DateRangeSelector';
|
|
|
|
import type { DateInterval } from '../../../src/utils/helpers/dateIntervals';
|
2022-07-14 23:14:42 +03:00
|
|
|
import { renderWithEvents } from '../../__helpers__/setUpTest';
|
2020-12-15 01:15:06 +03:00
|
|
|
|
|
|
|
describe('<DateRangeSelector />', () => {
|
|
|
|
const onDatesChange = jest.fn();
|
2022-07-14 23:14:42 +03:00
|
|
|
const setUp = async (props: Partial<DateRangeSelectorProps> = {}) => {
|
|
|
|
const result = renderWithEvents(
|
2021-10-03 22:07:07 +03:00
|
|
|
<DateRangeSelector
|
|
|
|
{...Mock.of<DateRangeSelectorProps>(props)}
|
|
|
|
defaultText="Default text"
|
|
|
|
onDatesChange={onDatesChange}
|
|
|
|
/>,
|
|
|
|
);
|
2020-12-15 01:15:06 +03:00
|
|
|
|
2022-07-14 23:14:42 +03:00
|
|
|
await result.user.click(screen.getByRole('button'));
|
2022-08-07 13:59:04 +03:00
|
|
|
await waitFor(() => screen.getByRole('menu'));
|
2022-07-14 23:14:42 +03:00
|
|
|
|
|
|
|
return result;
|
2020-12-15 01:15:06 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
afterEach(jest.clearAllMocks);
|
|
|
|
|
2022-07-14 23:14:42 +03:00
|
|
|
it('renders proper amount of items', async () => {
|
|
|
|
const { container } = await setUp();
|
2020-12-15 01:15:06 +03:00
|
|
|
|
2022-07-14 23:14:42 +03:00
|
|
|
expect(screen.getAllByRole('menuitem')).toHaveLength(8);
|
|
|
|
expect(screen.getByRole('heading')).toHaveTextContent('Custom:');
|
|
|
|
expect(container.querySelector('.dropdown-divider')).toBeInTheDocument();
|
|
|
|
expect(container.querySelector('.dropdown-item-text')).toBeInTheDocument();
|
2020-12-15 01:15:06 +03:00
|
|
|
});
|
|
|
|
|
2021-03-06 19:25:09 +03:00
|
|
|
it.each([
|
2022-03-26 14:17:42 +03:00
|
|
|
[undefined, 0],
|
|
|
|
['all' as DateInterval, 1],
|
|
|
|
['today' as DateInterval, 1],
|
|
|
|
['yesterday' as DateInterval, 1],
|
|
|
|
['last7Days' as DateInterval, 1],
|
|
|
|
['last30Days' as DateInterval, 1],
|
|
|
|
['last90Days' as DateInterval, 1],
|
|
|
|
['last180Days' as DateInterval, 1],
|
|
|
|
['last365Days' as DateInterval, 1],
|
|
|
|
[{ startDate: new Date() }, 0],
|
2022-07-14 23:14:42 +03:00
|
|
|
])('sets proper element as active based on provided date range', async (initialDateRange, expectedActiveItems) => {
|
|
|
|
const { container } = await setUp({ initialDateRange });
|
|
|
|
expect(container.querySelectorAll('.active')).toHaveLength(expectedActiveItems);
|
2020-12-15 01:15:06 +03:00
|
|
|
});
|
|
|
|
|
2022-07-14 23:14:42 +03:00
|
|
|
it('triggers onDatesChange callback when selecting an element', async () => {
|
|
|
|
const { user } = await setUp();
|
|
|
|
|
|
|
|
await user.click(screen.getByPlaceholderText('Since...'));
|
|
|
|
await user.click(screen.getAllByRole('option')[0]);
|
|
|
|
|
|
|
|
await user.click(screen.getByPlaceholderText('Until...'));
|
|
|
|
await user.click(screen.getAllByRole('option')[0]);
|
|
|
|
|
|
|
|
await user.click(screen.getAllByRole('menuitem')[0]);
|
2020-12-15 01:15:06 +03:00
|
|
|
|
|
|
|
expect(onDatesChange).toHaveBeenCalledTimes(3);
|
|
|
|
});
|
2021-10-03 22:07:07 +03:00
|
|
|
|
2022-07-14 23:14:42 +03:00
|
|
|
it('propagates default text to DateIntervalDropdownItems', async () => {
|
|
|
|
await setUp();
|
|
|
|
expect(screen.getAllByText('Default text')).toHaveLength(2);
|
2021-10-03 22:07:07 +03:00
|
|
|
});
|
2020-12-15 01:15:06 +03:00
|
|
|
});
|