diff options
author | diosmosis <diosmosis@users.noreply.github.com> | 2018-08-11 10:17:20 +0300 |
---|---|---|
committer | Matthieu Aubry <mattab@users.noreply.github.com> | 2018-08-11 10:17:20 +0300 |
commit | 78426614c226c50ba3ab9afc37aa2821500fa6d5 (patch) | |
tree | af0d74401e3425aea4e31ca3a61eb577ced4f52e /plugins/CoreHome/angularjs/common | |
parent | 30ae2ddbd43e46c773a01467e9a7f6da95a02fb9 (diff) |
In piwik.updatePeriodParamsFromUrl() get period/date values from hash or URL. (#13268)
* In piwik.updatePeriodParamsFromUrl() get period/date values from hash or URL.
* Remove date parameter from defaults in ajaxHelper, already handled properly below.
* Remove .only
* fixing tests & add new test for MultiSites
* correct screenshot
* upload screenshot + remove rejected change
* Remove port in Url::isLocalHost().
Diffstat (limited to 'plugins/CoreHome/angularjs/common')
-rw-r--r-- | plugins/CoreHome/angularjs/common/services/piwik.js | 5 | ||||
-rw-r--r-- | plugins/CoreHome/angularjs/common/services/piwik.spec.js | 138 |
2 files changed, 140 insertions, 3 deletions
diff --git a/plugins/CoreHome/angularjs/common/services/piwik.js b/plugins/CoreHome/angularjs/common/services/piwik.js index 680a44bfa4..8fbf94c96a 100644 --- a/plugins/CoreHome/angularjs/common/services/piwik.js +++ b/plugins/CoreHome/angularjs/common/services/piwik.js @@ -23,9 +23,8 @@ } function updatePeriodParamsFromUrl() { - var date = piwik.broadcast.getValueFromHash('date'); - var period = piwik.broadcast.getValueFromHash('period'); - + var date = piwik.broadcast.getValueFromHash('date') || piwik.broadcast.getValueFromUrl('date'); + var period = piwik.broadcast.getValueFromHash('period') || piwik.broadcast.getValueFromUrl('period'); if (!isValidPeriod(period, date)) { // invalid data in URL return; diff --git a/plugins/CoreHome/angularjs/common/services/piwik.spec.js b/plugins/CoreHome/angularjs/common/services/piwik.spec.js index 44d7678e32..ef109cb184 100644 --- a/plugins/CoreHome/angularjs/common/services/piwik.spec.js +++ b/plugins/CoreHome/angularjs/common/services/piwik.spec.js @@ -34,5 +34,143 @@ expect(piwikService.piwik_url).to.eql('http://localhost/'); }); }); + + describe('#updatePeriodParamsFromUrl()', function() { + 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(function (test) { + var date = test.date, + period = test.period, + expected = test.expected; + + it('should parse the period in the URL correctly when date=' + date + ' and period=' + period, function () { + delete piwikService.currentDateString; + delete piwikService.period; + delete piwikService.startDateString; + delete piwikService.endDateString; + + history.pushState(null, null, '?date=' + date + '&period=' + period); + + piwikService.updatePeriodParamsFromUrl(); + + expect(piwikService.currentDateString).to.equal(expected.currentDateString); + expect(piwikService.period).to.equal(expected.period); + expect(piwikService.startDateString).to.equal(expected.startDateString); + expect(piwikService.endDateString).to.equal(expected.endDateString); + }); + + it('should parse the period in the URL hash correctly when date=' + date + ' and period=' + period, function () { + delete piwikService.currentDateString; + delete piwikService.period; + delete piwikService.startDateString; + delete piwikService.endDateString; + + history.pushState(null, null, '?someparam=somevalue#?date=' + date + '&period=' + period); + + piwikService.updatePeriodParamsFromUrl(); + + expect(piwikService.currentDateString).to.equal(expected.currentDateString); + expect(piwikService.period).to.equal(expected.period); + expect(piwikService.startDateString).to.equal(expected.startDateString); + expect(piwikService.endDateString).to.equal(expected.endDateString); + }); + }); + + it('should not change object values if the current date/period is the same as the URL date/period', function () { + piwik.period = 'range'; + piwik.currentDateString = '2012-01-01,2012-01-02'; + piwik.startDateString = 'shouldnotchange'; + piwik.endDateString = 'shouldnotchangeeither'; + + history.pushState(null, null, '?someparam=somevalue#?date=' + piwik.currentDateString + '&period=' + piwik.period); + + piwikService.updatePeriodParamsFromUrl(); + + expect(piwikService.startDateString).to.equal('shouldnotchange'); + expect(piwikService.endDateString).to.equal('shouldnotchangeeither'); + }); + }); }); })();
\ No newline at end of file |