Passed
Push — main ( 017db1...f1f3c3 )
by Alejandro
42:56 queued 39:55
created

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

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 59
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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