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

test/utils/dates/types/index.test.ts   A

Complexity

Total Complexity 4
Complexity/F 0

Size

Lines of Code 89
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 79
mnd 4
bc 4
fnc 0
dl 0
loc 89
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
rs 10
1
import moment from 'moment';
2
import {
3
  DateInterval,
4
  dateRangeIsEmpty,
5
  intervalToDateRange,
6
  rangeIsInterval,
7
  rangeOrIntervalToString,
8
} from '../../../../src/utils/dates/types';
9
10
describe('date-types', () => {
11
  describe('dateRangeIsEmpty', () => {
12
    test.each([
13
      [ undefined, true ],
14
      [{}, true ],
15
      [{ startDate: null }, true ],
16
      [{ endDate: null }, true ],
17
      [{ startDate: null, endDate: null }, true ],
18
      [{ startDate: undefined }, true ],
19
      [{ endDate: undefined }, true ],
20
      [{ startDate: undefined, endDate: undefined }, true ],
21
      [{ startDate: undefined, endDate: null }, true ],
22
      [{ startDate: null, endDate: undefined }, true ],
23
      [{ startDate: moment() }, false ],
24
      [{ endDate: moment() }, false ],
25
      [{ startDate: moment(), endDate: moment() }, false ],
26
    ])('proper result is returned', (dateRange, expectedResult) => {
27
      expect(dateRangeIsEmpty(dateRange)).toEqual(expectedResult);
28
    });
29
  });
30
31
  describe('rangeIsInterval', () => {
32
    test.each([
33
      [ undefined, false ],
34
      [{}, false ],
35
      [ 'today' as DateInterval, true ],
36
      [ 'yesterday' as DateInterval, true ],
37
    ])('proper result is returned', (range, expectedResult) => {
38
      expect(rangeIsInterval(range)).toEqual(expectedResult);
39
    });
40
  });
41
42
  describe('rangeOrIntervalToString', () => {
43
    test.each([
44
      [ undefined, undefined ],
45
      [ 'today' as DateInterval, 'Today' ],
46
      [ 'yesterday' as DateInterval, 'Yesterday' ],
47
      [ 'last7Days' as DateInterval, 'Last 7 days' ],
48
      [ 'last30Days' as DateInterval, 'Last 30 days' ],
49
      [ 'last90Days' as DateInterval, 'Last 90 days' ],
50
      [ 'last180days' as DateInterval, 'Last 180 days' ],
51
      [ 'last365Days' as DateInterval, 'Last 365 days' ],
52
      [{}, undefined ],
53
      [{ startDate: null }, undefined ],
54
      [{ endDate: null }, undefined ],
55
      [{ startDate: null, endDate: null }, undefined ],
56
      [{ startDate: undefined }, undefined ],
57
      [{ endDate: undefined }, undefined ],
58
      [{ startDate: undefined, endDate: undefined }, undefined ],
59
      [{ startDate: undefined, endDate: null }, undefined ],
60
      [{ startDate: null, endDate: undefined }, undefined ],
61
      [{ startDate: moment('2020-01-01') }, 'Since 2020-01-01' ],
62
      [{ endDate: moment('2020-01-01') }, 'Until 2020-01-01' ],
63
      [{ startDate: moment('2020-01-01'), endDate: moment('2021-02-02') }, '2020-01-01 - 2021-02-02' ],
64
    ])('proper result is returned', (range, expectedValue) => {
65
      expect(rangeOrIntervalToString(range)).toEqual(expectedValue);
66
    });
67
  });
68
69
  describe('intervalToDateRange', () => {
70
    const now = () => moment();
71
72
    test.each([
73
      [ undefined, undefined, undefined ],
74
      [ 'today' as DateInterval, now(), now() ],
75
      [ 'yesterday' as DateInterval, now().subtract(1, 'day'), now().subtract(1, 'day') ],
76
      [ 'last7Days' as DateInterval, now().subtract(7, 'day'), now() ],
77
      [ 'last30Days' as DateInterval, now().subtract(30, 'day'), now() ],
78
      [ 'last90Days' as DateInterval, now().subtract(90, 'day'), now() ],
79
      [ 'last180days' as DateInterval, now().subtract(180, 'day'), now() ],
80
      [ 'last365Days' as DateInterval, now().subtract(365, 'day'), now() ],
81
    ])('proper result is returned', (interval, expectedStartDate, expectedEndDate) => {
82
      const { startDate, endDate } = intervalToDateRange(interval);
83
84
      expect(expectedStartDate?.format('YYYY-MM-DD')).toEqual(startDate?.format('YYYY-MM-DD'));
85
      expect(expectedEndDate?.format('YYYY-MM-DD')).toEqual(endDate?.format('YYYY-MM-DD'));
86
    });
87
  });
88
});
89