shlink-web-client/test/utils/dates/DateRangeSelector.test.tsx

70 lines
2.6 KiB
TypeScript
Raw Normal View History

2020-12-15 01:15:06 +03:00
import { shallow, ShallowWrapper } from 'enzyme';
import { DropdownItem } from 'reactstrap';
import moment from 'moment';
2020-12-15 20:57:23 +03:00
import { Mock } from 'ts-mockery';
2020-12-15 01:15:06 +03:00
import { DateRangeSelector, DateRangeSelectorProps } from '../../../src/utils/dates/DateRangeSelector';
import { DateInterval } from '../../../src/utils/dates/types';
import { DateIntervalDropdownItems } from '../../../src/utils/dates/DateIntervalDropdownItems';
2020-12-15 01:15:06 +03:00
describe('<DateRangeSelector />', () => {
let wrapper: ShallowWrapper;
const onDatesChange = jest.fn();
const createWrapper = (props: Partial<DateRangeSelectorProps> = {}) => {
2020-12-15 11:54:45 +03:00
wrapper = shallow(<DateRangeSelector {...Mock.of<DateRangeSelectorProps>(props)} onDatesChange={onDatesChange} />);
2020-12-15 01:15:06 +03:00
return wrapper;
};
afterEach(jest.clearAllMocks);
afterEach(() => wrapper?.unmount());
2021-03-06 19:25:09 +03:00
it('renders proper amount of items', () => {
2020-12-15 01:15:06 +03:00
const wrapper = createWrapper();
const items = wrapper.find(DropdownItem);
const dateIntervalItems = wrapper.find(DateIntervalDropdownItems);
2020-12-15 01:15:06 +03:00
expect(items).toHaveLength(5);
expect(dateIntervalItems).toHaveLength(1);
2020-12-15 01:15:06 +03:00
expect(items.filter('[divider]')).toHaveLength(2);
expect(items.filter('[header]')).toHaveLength(1);
expect(items.filter('[text]')).toHaveLength(1);
expect(items.filter('[active]')).toHaveLength(1);
2020-12-15 01:15:06 +03:00
});
2021-03-06 19:25:09 +03:00
it.each([
[ undefined, 1, 0 ],
[ 'today' as DateInterval, 0, 1 ],
[ 'yesterday' as DateInterval, 0, 1 ],
[ 'last7Days' as DateInterval, 0, 1 ],
[ 'last30Days' as DateInterval, 0, 1 ],
[ 'last90Days' as DateInterval, 0, 1 ],
[ 'last180days' as DateInterval, 0, 1 ],
[ 'last365Days' as DateInterval, 0, 1 ],
[{ startDate: moment() }, 0, 0 ],
2021-03-06 19:25:09 +03:00
])('sets proper element as active based on provided date range', (
initialDateRange,
expectedActiveItems,
expectedActiveIntervalItems,
) => {
2020-12-15 01:15:06 +03:00
const wrapper = createWrapper({ initialDateRange });
const items = wrapper.find(DropdownItem).filterWhere((item) => item.prop('active') === true);
const dateIntervalItems = wrapper.find(DateIntervalDropdownItems).filterWhere(
(item) => item.prop('active') !== undefined,
);
2020-12-15 01:15:06 +03:00
expect(items).toHaveLength(expectedActiveItems);
expect(dateIntervalItems).toHaveLength(expectedActiveIntervalItems);
2020-12-15 01:15:06 +03:00
});
2021-03-06 19:25:09 +03:00
it('triggers onDatesChange callback when selecting an element', () => {
2020-12-15 01:15:06 +03:00
const wrapper = createWrapper();
const item = wrapper.find(DropdownItem).at(0);
const dateIntervalItems = wrapper.find(DateIntervalDropdownItems);
2020-12-15 01:15:06 +03:00
item.simulate('click');
item.simulate('click');
dateIntervalItems.simulate('change');
2020-12-15 01:15:06 +03:00
expect(onDatesChange).toHaveBeenCalledTimes(3);
});
});