Passed
Pull Request — main (#350)
by Alejandro
21:55
created

test/utils/dates/DateRangeSelector.test.tsx   A

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 58
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 51
mnd 1
bc 1
fnc 0
dl 0
loc 58
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
rs 10
1
import { shallow, ShallowWrapper } from 'enzyme';
2
import { DropdownItem } from 'reactstrap';
3
import moment from 'moment';
4
import { DateRangeSelector, DateRangeSelectorProps } from '../../../src/utils/dates/DateRangeSelector';
5
import { DateInterval } from '../../../src/utils/dates/types';
6
7
describe('<DateRangeSelector />', () => {
8
  let wrapper: ShallowWrapper;
9
  const onDatesChange = jest.fn();
10
  const createWrapper = (props: Partial<DateRangeSelectorProps> = {}) => {
11
    wrapper = shallow(<DateRangeSelector {...props} onDatesChange={onDatesChange} />);
12
13
    return wrapper;
14
  };
15
16
  afterEach(jest.clearAllMocks);
17
  afterEach(() => wrapper?.unmount());
18
19
  test('proper amount of items is rendered', () => {
20
    const wrapper = createWrapper();
21
    const items = wrapper.find(DropdownItem);
22
23
    expect(items).toHaveLength(12);
24
    expect(items.filter('[divider]')).toHaveLength(2);
25
    expect(items.filter('[header]')).toHaveLength(1);
26
    expect(items.filter('[text]')).toHaveLength(1);
27
    expect(items.filter('[active]')).toHaveLength(8);
28
  });
29
30
  test.each([
31
    [ undefined, 0 ],
32
    [ 'today' as DateInterval, 1 ],
33
    [ 'yesterday' as DateInterval, 2 ],
34
    [ 'last7Days' as DateInterval, 3 ],
35
    [ 'last30Days' as DateInterval, 4 ],
36
    [ 'last90Days' as DateInterval, 5 ],
37
    [ 'last180days' as DateInterval, 6 ],
38
    [ 'last365Days' as DateInterval, 7 ],
39
    [{ startDate: moment() }, 8 ],
40
  ])('proper element is active based on provided date range', (initialDateRange, expectedActiveIndex) => {
41
    const wrapper = createWrapper({ initialDateRange });
42
    const items = wrapper.find(DropdownItem).filter('[active]');
43
44
    expect.assertions(8);
45
    items.forEach((item, index) => expect(item.prop('active')).toEqual(index === expectedActiveIndex));
46
  });
47
48
  test('selecting an element triggers onDatesChange callback', () => {
49
    const wrapper = createWrapper();
50
    const items = wrapper.find(DropdownItem).filter('[active]');
51
52
    items.at(2).simulate('click');
53
    items.at(4).simulate('click');
54
    items.at(1).simulate('click');
55
    expect(onDatesChange).toHaveBeenCalledTimes(3);
56
  });
57
});
58