mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-19 15:48:04 +01:00
initial tests
This commit is contained in:
@@ -8,7 +8,6 @@ function _aggregate (history, aggregateBy) {
|
|||||||
return moment(entry.date).format(aggregateBy);
|
return moment(entry.date).format(aggregateBy);
|
||||||
})
|
})
|
||||||
.sortBy((entry, key) => key) // sort by date
|
.sortBy((entry, key) => key) // sort by date
|
||||||
.reverse() // sort from most to least recent
|
|
||||||
.map(entries => {
|
.map(entries => {
|
||||||
return {
|
return {
|
||||||
date: Number(entries[0].date),
|
date: Number(entries[0].date),
|
||||||
@@ -40,17 +39,19 @@ export function preenHistory (history, isSubscribed, timezoneOffset) {
|
|||||||
|
|
||||||
// Keep uncompressed entries (modifies history)
|
// Keep uncompressed entries (modifies history)
|
||||||
let newHistory = _.remove(history, entry => {
|
let newHistory = _.remove(history, entry => {
|
||||||
return moment(entry.date).isSameOrAfter(cutOff);
|
let date = moment(entry.date);
|
||||||
|
return date.isSame(cutOff) || date.isAfter(cutOff);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Date after which to begin compressing data by year
|
// Date after which to begin compressing data by year
|
||||||
let monthsCutOff = cutOff.subtract(isSubscribed ? 12 : 10, 'months').startOf('day');
|
let monthsCutOff = cutOff.subtract(isSubscribed ? 12 : 10, 'months').startOf('day');
|
||||||
let aggregateByMonth = _.remove(history, entry => {
|
let aggregateByMonth = _.remove(history, entry => {
|
||||||
return moment(entry.date).isSameOrAfter(monthsCutOff);
|
let date = moment(entry.date);
|
||||||
|
return date.isSame(monthsCutOff) || date.isAfter(monthsCutOff);
|
||||||
});
|
});
|
||||||
// Aggregate remaining entries by month and year
|
// Aggregate remaining entries by month and year
|
||||||
newHistory.push(..._aggregate(aggregateByMonth, 'YYYYMM'));
|
newHistory.unshift(..._aggregate(aggregateByMonth, 'YYYYMM'));
|
||||||
newHistory.push(..._aggregate(history, 'YYYY'));
|
newHistory.unshift(..._aggregate(history, 'YYYY'));
|
||||||
|
|
||||||
return newHistory;
|
return newHistory;
|
||||||
}
|
}
|
||||||
|
|||||||
58
test/common/preenHistory.test.js
Normal file
58
test/common/preenHistory.test.js
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
import { preenHistory } from '../../common/script/preenUserHistory';
|
||||||
|
import moment from 'moment';
|
||||||
|
import _ from 'lodash';
|
||||||
|
import sinon from 'sinon';
|
||||||
|
function generateHistory (days) {
|
||||||
|
let history = [];
|
||||||
|
let now = Number(moment().toDate());
|
||||||
|
|
||||||
|
while (days > 0) {
|
||||||
|
history.push({
|
||||||
|
value: days,
|
||||||
|
date: Number(moment(now).subtract(days, 'days').toDate()),
|
||||||
|
});
|
||||||
|
days--;
|
||||||
|
}
|
||||||
|
|
||||||
|
return history;
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('preenHistory', function () {
|
||||||
|
let history;
|
||||||
|
|
||||||
|
beforeEach(function beforeEach () {
|
||||||
|
// Replace system clocks so we can get predictable results
|
||||||
|
this.clock = sinon.useFakeTimers(Number(moment('2013-10-20').zone(0).startOf('day').toDate()), 'Date');
|
||||||
|
});
|
||||||
|
afterEach(function afterEach () {
|
||||||
|
return this.clock.restore();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not modify history if all entries are more recent than cutoff (free users)', () => {
|
||||||
|
let h = generateHistory(60);
|
||||||
|
expect(preenHistory(_.cloneDeep(h))).to.eql(h);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not modify history if all entries are more recent than cutoff (subscribers)', () => {
|
||||||
|
let h = generateHistory(365);
|
||||||
|
expect(preenHistory(_.cloneDeep(h), true)).to.eql(h);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does aggregate data in monthly entries before cutoff (free users)', () => {
|
||||||
|
let h = generateHistory(81); // Jumps to July
|
||||||
|
let preened = preenHistory(_.cloneDeep(h));
|
||||||
|
expect(preened.length).to.eql(62); // Keeps 60 days + 2 entries per august and july
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does aggregate data in monthly entries before cutoff (subscribers)', () => {
|
||||||
|
let h = generateHistory(386); // Jumps to July
|
||||||
|
let preened = preenHistory(_.cloneDeep(h), true);
|
||||||
|
expect(preened.length).to.eql(367); // Keeps 367 days + 2 entries per august and july
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does aggregate data in monthly and yearly entries before cutoff (free users)', () => {
|
||||||
|
let h = generateHistory(364); // Jumps to October 21 2012
|
||||||
|
let preened = preenHistory(_.cloneDeep(h));
|
||||||
|
expect(preened.length).to.eql(71); // Keeps 60 days + 10 montly entries and 1 yearly entry for Oct 21 - Oct 31 2012
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
import preenUserHistory from '../../common/script/preenUserHistory';
|
|
||||||
|
|
||||||
describe('preenUserHistory', () => {
|
|
||||||
describe('preen tasks history', () => {
|
|
||||||
it('works');
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('preen user history', () => {
|
|
||||||
it('works');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
Reference in New Issue
Block a user