Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Periods.spec.ts « Periods « src « vue « CoreHome « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7ff6c637a48ea0baac92178b5c16c355545522ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*!
 * Matomo - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */
import Periods from './Periods';
import './Day';
import './Week';
import './Month';
import './Year';
import RangePeriod from './Range';
import { parseDate } from './utilities';

describe('CoreHome/Periods', () => {
  function clearDate(strDate: Date|string):Date {
    const date = new Date(strDate);
    date.setHours(0);
    date.setMinutes(0);
    date.setSeconds(0);
    date.setMilliseconds(0);

    return date;
  }

  let originalDateNow: (() => number)|null;

  beforeEach(() => {
    originalDateNow = null;
    window.piwik.timezoneOffset = 0;
  });

  afterEach(() => {
    if (originalDateNow) {
      Date.now = originalDateNow;
    }
  });

  it('should get daterange for day', () => {
    const day = '2021-03-10';

    const result = Periods.parse('day', day).getDateRange();
    const expected = [clearDate(day), clearDate(day)];

    expect(result).toEqual(expected);
  })

  it('should get daterange for week', function() {
    const day = '2021-03-10';
    const monday = '2021-03-08';
    const sunday = '2021-03-14';

    const result = Periods.parse('week', day).getDateRange();
    const expected = [clearDate(monday), clearDate(sunday)];

    expect(result).toEqual(expected);
  });

  it('should get daterange for month', function() {
    const day = '2021-03-10';
    const first = '2021-03-01';
    const last = '2021-03-31';

    const result = Periods.parse('month', day).getDateRange();
    const expected = [clearDate(first), clearDate(last)];

    expect(result).toEqual(expected);
  });

  it('should get daterange for month for date 31th', function() {
    const day = '2021-03-31';
    const first = '2021-03-01';
    const last = '2021-03-31';

    const result = Periods.parse('month', day).getDateRange();
    const expected = [clearDate(first), clearDate(last)];

    expect(result).toEqual(expected);
  });

  it('should get daterange for year', function() {
    const day = '2021-03-10';
    const first = '2021-01-01';
    const last = '2021-12-31';

    const result = Periods.parse('year', day).getDateRange();
    const expected = [clearDate(first), clearDate(last)];

    expect(result).toEqual(expected);
  });

  it('should get daterange for year for date 31th december', function() {
    const day = '2021-12-31';
    const first = '2021-01-01';
    const last = '2021-12-31';

    const result = Periods.parse('year', day).getDateRange();
    const expected = [clearDate(first), clearDate(last)];

    expect(result).toEqual(expected);
  });

  it('should get proper month rangeperiod when date is 31th march', function() {
    const day = '2021-03-31';
    const first = '2021-02-01';
    const last = '2021-03-31';

    const result = RangePeriod.getLastNRange('month', 2, day);

    expect(result.startDate).toEqual(clearDate(first));
    expect(result.endDate).toEqual(clearDate(last));
  });

  it('should parse last month properly when date is 31th march', function() {
    originalDateNow = Date.now;
    Date.now = function mockNow() {
      return clearDate('2021-03-31').getTime();
    };

    const result = parseDate('last month');

    expect(result.getMonth()).toEqual(1); // 1 is February
  });

  it('should parse last month properly', function() {
    originalDateNow = Date.now;
    Date.now = function mockNow() {
      return clearDate('2021-03-10').getTime();
    };

    const result = parseDate('last month');

    expect(result.getMonth()).toEqual(1); // 1 is February
  });

  it('should contains today for daterange if it contains', function() {
    const day = '2021-03-10';

    originalDateNow = Date.now;
    Date.now = function() {
      return clearDate(day).getTime();
    };

    const result = Periods.parse('week', day).containsToday();

    expect(result).toBe(true);
  });

  it('should not contains today for daterange if it not contains', function() {
    const today = '2021-03-10';
    const day = '2021-03-17';

    originalDateNow = Date.now;
    Date.now = function() {
      return clearDate(today).getTime();
    };

    const result = Periods.parse('week', day).containsToday();

    expect(result).toBe(false);
  });
});