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

Matomo.spec.ts « Matomo « src « vue « CoreHome « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 49ab7f5b4a34ce1c28d580f9d2e6aa7d59231cb6 (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
/*!
 * Matomo - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */
import Matomo from './Matomo';
import '../Periods/Day';
import '../Periods/Week';
import '../Periods/Month';
import '../Periods/Year';
import '../Periods/Range';

describe('CoreHome/Matomo', () => {
  describe('#updatePeriodParamsFromUrl()', () => {
    const DATE_PERIODS_TO_TEST = [
      {
        date: '2012-01-02',
        period: 'day',
        expected: {
          currentDateString: '2012-01-02',
          period: 'day',
          startDateString: '2012-01-02',
          endDateString: '2012-01-02'
        }
      },
      {
        date: '2012-01-02',
        period: 'week',
        expected: {
          currentDateString: '2012-01-02',
          period: 'week',
          startDateString: '2012-01-02',
          endDateString: '2012-01-08'
        }
      },
      {
        date: '2012-01-02',
        period: 'month',
        expected: {
          currentDateString: '2012-01-02',
          period: 'month',
          startDateString: '2012-01-01',
          endDateString: '2012-01-31'
        }
      },
      {
        date: '2012-01-02',
        period: 'year',
        expected: {
          currentDateString: '2012-01-02',
          period: 'year',
          startDateString: '2012-01-01',
          endDateString: '2012-12-31'
        }
      },
      {
        date: '2012-01-02,2012-02-03',
        period: 'range',
        expected: {
          currentDateString: '2012-01-02,2012-02-03',
          period: 'range',
          startDateString: '2012-01-02',
          endDateString: '2012-02-03'
        }
      },
      // invalid
      {
        date: '2012-01-02',
        period: 'range',
        expected: {
          currentDateString: undefined,
          period: undefined,
          startDateString: undefined,
          endDateString: undefined
        }
      },
      {
        date: 'sldfjkdslkfj',
        period: 'month',
        expected: {
          currentDateString: undefined,
          period: undefined,
          startDateString: undefined,
          endDateString: undefined
        }
      },
      {
        date: '2012-01-02',
        period: 'sflkjdslkfj',
        expected: {
          currentDateString: undefined,
          period: undefined,
          startDateString: undefined,
          endDateString: undefined
        }
      }
    ];

    DATE_PERIODS_TO_TEST.forEach((test) => {
      const date = test.date,
        period = test.period,
        expected = test.expected;

      it(`should parse the period in the URL correctly when date=${date} and period=${period}`, () => {
        delete Matomo.currentDateString;
        delete Matomo.period;
        delete Matomo.startDateString;
        delete Matomo.endDateString;

        history.pushState(null, '', '?date=' + date + '&period=' + period);

        Matomo.updatePeriodParamsFromUrl();

        expect(Matomo.currentDateString).toEqual(expected.currentDateString);
        expect(Matomo.period).toEqual(expected.period);
        expect(Matomo.startDateString).toEqual(expected.startDateString);
        expect(Matomo.endDateString).toEqual(expected.endDateString);
      });

      it('should parse the period in the URL hash correctly when date=' + date + ' and period=' + period, () => {
        delete Matomo.currentDateString;
        delete Matomo.period;
        delete Matomo.startDateString;
        delete Matomo.endDateString;

        history.pushState(null, '', '?someparam=somevalue#?date=' + date + '&period=' + period);

        Matomo.updatePeriodParamsFromUrl();

        expect(Matomo.currentDateString).toEqual(expected.currentDateString);
        expect(Matomo.period).toEqual(expected.period);
        expect(Matomo.startDateString).toEqual(expected.startDateString);
        expect(Matomo.endDateString).toEqual(expected.endDateString);
      });
    });

    it('should not change object values if the current date/period is the same as the URL date/period', () => {
      Matomo.period = 'range';
      Matomo.currentDateString = '2012-01-01,2012-01-02';
      Matomo.startDateString = 'shouldnotchange';
      Matomo.endDateString = 'shouldnotchangeeither';

      history.pushState(null, '', '?someparam=somevalue#?date=' + Matomo.currentDateString + '&period=' + Matomo.period);

      Matomo.updatePeriodParamsFromUrl();

      expect(Matomo.startDateString).toEqual('shouldnotchange');
      expect(Matomo.endDateString).toEqual('shouldnotchangeeither');
    });
  });
});