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

77 lines
2.8 KiB
TypeScript
Raw Normal View History

2020-12-15 01:15:06 +03:00
import { shallow, ShallowWrapper } from 'enzyme';
import { DropdownItem } from 'reactstrap';
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';
import DateRangeRow from '../../../src/utils/dates/DateRangeRow';
2020-12-15 01:15:06 +03:00
describe('<DateRangeSelector />', () => {
let wrapper: ShallowWrapper;
const onDatesChange = jest.fn();
const createWrapper = (props: Partial<DateRangeSelectorProps> = {}) => {
wrapper = shallow(
<DateRangeSelector
{...Mock.of<DateRangeSelectorProps>(props)}
defaultText="Default text"
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(3);
expect(dateIntervalItems).toHaveLength(1);
expect(items.filter('[divider]')).toHaveLength(1);
2020-12-15 01:15:06 +03:00
expect(items.filter('[header]')).toHaveLength(1);
expect(items.filter('[text]')).toHaveLength(1);
});
2021-03-06 19:25:09 +03:00
it.each([
[ 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 ],
])('sets proper element as active based on provided date range', (initialDateRange, expectedActiveIntervalItems) => {
2020-12-15 01:15:06 +03:00
const wrapper = createWrapper({ initialDateRange });
const dateIntervalItems = wrapper.find(DateIntervalDropdownItems).filterWhere(
(item) => item.prop('active') !== undefined,
);
2020-12-15 01:15:06 +03:00
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 dates = wrapper.find(DateRangeRow);
const dateIntervalItems = wrapper.find(DateIntervalDropdownItems);
2020-12-15 01:15:06 +03:00
dates.simulate('startDateChange', null);
dates.simulate('endDateChange', null);
dateIntervalItems.simulate('change');
2020-12-15 01:15:06 +03:00
expect(onDatesChange).toHaveBeenCalledTimes(3);
});
it('propagates default text to DateIntervalDropdownItems', () => {
const wrapper = createWrapper();
const dateIntervalItems = wrapper.find(DateIntervalDropdownItems);
expect(dateIntervalItems.prop('allText')).toEqual('Default text');
});
2020-12-15 01:15:06 +03:00
});