Getting users timezone on client side and passing it to server side within POST (on challenge complete), showing users stats with users timezone

After CR
This commit is contained in:
JelenaBarinova
2016-01-19 21:11:20 -05:00
parent e02d9d4039
commit 29f93df1bb
12 changed files with 179 additions and 109 deletions

View File

@@ -1,34 +1,44 @@
import moment from 'moment';
import moment from 'moment-timezone';
import { dayCount } from '../../../server/utils/date-utils';
let test = require('tape');
test('Day count between two epochs (inclusive) calculation', function (t) {
t.plan(5);
t.plan(7);
t.equal(dayCount([
moment("8/3/2015 3:00", "M/D/YYYY H:mm").valueOf(),
moment("8/3/2015 2:00", "M/D/YYYY H:mm").valueOf()
moment.utc("8/3/2015 3:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("8/3/2015 2:00", "M/D/YYYY H:mm").valueOf()
]), 1, "should return 1 day given epochs of the same day");
t.equal(dayCount([
moment("8/3/2015 2:00", "M/D/YYYY H:mm").valueOf(),
moment("8/3/2015 2:00", "M/D/YYYY H:mm").valueOf()
moment.utc("8/3/2015 2:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("8/3/2015 2:00", "M/D/YYYY H:mm").valueOf()
]), 1, "should return 1 day given same epochs");
t.equal(dayCount([
moment("8/4/2015 2:00", "M/D/YYYY H:mm").valueOf(),
moment("8/3/2015 2:00", "M/D/YYYY H:mm").valueOf()
moment.utc("8/4/2015 2:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("8/3/2015 2:00", "M/D/YYYY H:mm").valueOf()
]), 2, "should return 2 days when there is a 24 hours difference between given dates");
t.equal(dayCount([
moment("8/4/2015 1:00", "M/D/YYYY H:mm").valueOf(),
moment("8/3/2015 23:00", "M/D/YYYY H:mm").valueOf()
]), 2, "should return 2 days when the diff is less than 24h but days of the month are different");
moment.utc("8/4/2015 1:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("8/3/2015 23:00", "M/D/YYYY H:mm").valueOf()
]), 2, "should return 2 days when the diff is less than 24h but days are different in default timezone UTC");
t.equal(dayCount([
moment("10/27/2015 1:00", "M/D/YYYY H:mm").valueOf(),
moment("5/12/1982 1:00", "M/D/YYYY H:mm").valueOf()
moment.utc("8/4/2015 1:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("8/3/2015 23:00", "M/D/YYYY H:mm").valueOf()
], 'America/Los_Angeles'), 1, "should return 1 day when the diff is less than 24h and days are different in UTC, but given 'America/Los_Angeles' timezone");
t.equal(dayCount([
moment.utc("10/27/2015 1:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("5/12/1982 1:00", "M/D/YYYY H:mm").valueOf()
]), 12222, "should return correct count when there is very big period");
t.equal(dayCount([
moment.utc("8/4/2015 2:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("8/3/2015 2:00", "M/D/YYYY H:mm").valueOf()
], undefined), 2, "should return 2 days when there is a 24 hours difference between dates given 'undefined' timezone");
});

View File

@@ -1,132 +1,160 @@
import moment from 'moment';
import moment from 'moment-timezone';
import { calcCurrentStreak, calcLongestStreak } from '../../../server/utils/user-stats';
let test = require('tape');
test('Current streak calculation', function (t) {
t.plan(7);
t.plan(9);
t.equal(calcCurrentStreak([
moment(moment().subtract(1, 'hours')).valueOf()
moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
]), 1, "should return 1 day when today one challenge was completed");
t.equal(calcCurrentStreak([
moment(moment().subtract(2, 'hours')).valueOf(),
moment(moment().subtract(1, 'hours')).valueOf()
]), 1, "should return 1 day when today more than one challenge were completed");
moment.utc(moment.utc().subtract(1, 'hours')).valueOf(),
moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
]), 1, "should return 1 day when today more than one challenge was completed");
t.equal(calcCurrentStreak([
moment("9/11/2015 4:00", "M/D/YYYY H:mm").valueOf()
moment.utc("9/11/2015 4:00", "M/D/YYYY H:mm").valueOf()
]), 0, "should return 0 day when today 0 challenges were completed");
t.equal(calcCurrentStreak([
moment(moment().subtract(1, 'days')).valueOf(),
moment(moment().subtract(1, 'hours')).valueOf()
moment.utc(moment.utc().subtract(1, 'days')).valueOf(),
moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
]), 2, "should return 2 days when today and yesterday challenges were completed");
t.equal(calcCurrentStreak([
moment("8/3/2015 2:00", "M/D/YYYY H:mm").valueOf(),
moment("9/11/2015 4:00", "M/D/YYYY H:mm").valueOf(),
moment("9/12/2015 1:00", "M/D/YYYY H:mm").valueOf(),
moment("9/12/2015 1:00", "M/D/YYYY H:mm").valueOf(),
moment("9/12/2015 2:00", "M/D/YYYY H:mm").valueOf(),
moment("9/12/2015 3:00", "M/D/YYYY H:mm").valueOf(),
moment("9/13/2015 4:00", "M/D/YYYY H:mm").valueOf(),
moment("9/14/2015 5:00", "M/D/YYYY H:mm").valueOf(),
moment(moment().subtract(2, 'days')).valueOf(),
moment(moment().subtract(1, 'days')).valueOf(),
moment(moment().subtract(1, 'hours')).valueOf()
moment.utc("8/3/2015 2:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("9/11/2015 4:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("9/12/2015 1:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("9/12/2015 1:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("9/12/2015 2:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("9/12/2015 3:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("9/13/2015 4:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("9/14/2015 5:00", "M/D/YYYY H:mm").valueOf(),
moment.utc(moment.utc().subtract(2, 'days')).valueOf(),
moment.utc(moment.utc().subtract(1, 'days')).valueOf(),
moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
]), 3, "should return 3 when today and for two days before user was activity");
t.equal(calcCurrentStreak([
moment(moment().subtract(37, 'hours')).valueOf(),
moment(moment().subtract(1, 'hours')).valueOf()
moment.utc(moment.utc().subtract(37, 'hours')).valueOf(),
moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
]), 1, "should return 1 when between todays challenge completion and yesterdays there is a 1.5 day (36 hours) long break");
t.ok(calcCurrentStreak([
moment.utc(moment.utc().subtract(35, 'hours')).valueOf(),
moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
]) >= 2, "should return not less than 2 days when between todays challenge completion and yesterdays there is less than 1.5 day (36 hours) long break");
t.equal(calcCurrentStreak([
moment.utc(moment.utc().subtract(1, 'hours')).valueOf(),
moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
], undefined), 1, "should return correct count in default timezone UTC given 'undefined' timezone");
t.equal(calcCurrentStreak([
moment(moment().subtract(35, 'hours')).valueOf(),
moment(moment().subtract(1, 'hours')).valueOf()
]), 2, "should return 2 days when between todays challenge completion and yesterdays there is less than 1.5 day (36 hours) long break");
moment.utc(moment.utc().subtract(1, 'days')).valueOf(),
moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
], 'America/Los_Angeles'), 2, "should return 2 days when today and yesterday challenges were completed given 'America/Los_Angeles' timezone");
});
test('Longest streak calculation', function (t) {
t.plan(9);
t.plan(12);
t.equal(calcLongestStreak([
moment("9/12/2015 4:00", "M/D/YYYY H:mm").valueOf()
moment.utc("9/12/2015 4:00", "M/D/YYYY H:mm").valueOf()
]), 1, "should return 1 when there is the only one one-day-long streak available");
t.equal(calcLongestStreak([
moment("9/11/2015 4:00", "M/D/YYYY H:mm").valueOf(),
moment("9/12/2015 2:00", "M/D/YYYY H:mm").valueOf(),
moment("9/13/2015 3:00", "M/D/YYYY H:mm").valueOf(),
moment("9/14/2015 1:00", "M/D/YYYY H:mm").valueOf()
moment.utc("9/11/2015 4:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("9/12/2015 2:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("9/13/2015 3:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("9/14/2015 1:00", "M/D/YYYY H:mm").valueOf()
]), 4, "should return 4 when there is the only one more-than-one-days-long streak available");
t.equal(calcLongestStreak([
moment(moment().subtract(1, 'hours')).valueOf()
moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
]), 1, "should return 1 when there is only one one-day-long streak and it is today");
t.equal(calcLongestStreak([
moment(moment().subtract(1, 'days')).valueOf(),
moment(moment().subtract(1, 'hours')).valueOf()
moment.utc(moment.utc().subtract(1, 'days')).valueOf(),
moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
]), 2, "should return 2 when yesterday and today makes longest streak");
t.equal(calcLongestStreak([
moment("8/3/2015 2:00", "M/D/YYYY H:mm").valueOf(),
moment("9/11/2015 4:00", "M/D/YYYY H:mm").valueOf(),
moment("9/12/2015 2:00", "M/D/YYYY H:mm").valueOf(),
moment("10/4/2015 1:00", "M/D/YYYY H:mm").valueOf(),
moment("10/5/2015 5:00", "M/D/YYYY H:mm").valueOf(),
moment("10/6/2015 4:00", "M/D/YYYY H:mm").valueOf(),
moment("10/7/2015 5:00", "M/D/YYYY H:mm").valueOf(),
moment("11/3/2015 2:00", "M/D/YYYY H:mm").valueOf()
moment.utc("8/3/2015 2:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("9/11/2015 4:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("9/12/2015 2:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("10/4/2015 1:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("10/5/2015 5:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("10/6/2015 4:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("10/7/2015 5:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("11/3/2015 2:00", "M/D/YYYY H:mm").valueOf()
]), 4, "should return 4 when there is a month long break");
t.equal(calcLongestStreak([
moment("8/3/2015 2:00", "M/D/YYYY H:mm").valueOf(),
moment("9/11/2015 4:00", "M/D/YYYY H:mm").valueOf(),
moment("9/12/2015 1:00", "M/D/YYYY H:mm").valueOf(),
moment(moment("9/12/2015 1:00", "M/D/YYYY H:mm").add(37, 'hours')).valueOf(),
moment("9/14/2015 22:00", "M/D/YYYY H:mm").valueOf(),
moment("9/15/2015 4:00", "M/D/YYYY H:mm").valueOf(),
moment("10/3/2015 2:00", "M/D/YYYY H:mm").valueOf()
moment.utc("8/3/2015 2:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("9/11/2015 4:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("9/12/2015 1:00", "M/D/YYYY H:mm").valueOf(),
moment.utc(moment.utc("9/12/2015 1:00", "M/D/YYYY H:mm").add(37, 'hours')).valueOf(),
moment.utc("9/14/2015 22:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("9/15/2015 4:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("10/3/2015 2:00", "M/D/YYYY H:mm").valueOf()
]), 3, "should return 3 when there is a more than 1.5 days long break of (36 hours)");
t.equal(calcLongestStreak([
moment("8/3/2015 2:00", "M/D/YYYY H:mm").valueOf(),
moment("9/11/2015 4:00", "M/D/YYYY H:mm").valueOf(),
moment("9/12/2015 1:00", "M/D/YYYY H:mm").valueOf(),
moment("9/12/2015 1:00", "M/D/YYYY H:mm").valueOf(),
moment("9/12/2015 2:00", "M/D/YYYY H:mm").valueOf(),
moment("9/12/2015 3:00", "M/D/YYYY H:mm").valueOf(),
moment("9/13/2015 4:00", "M/D/YYYY H:mm").valueOf(),
moment("9/14/2015 5:00", "M/D/YYYY H:mm").valueOf(),
moment(moment().subtract(2, 'days')).valueOf(),
moment(moment().subtract(1, 'days')).valueOf(),
moment().valueOf()
moment.utc("8/3/2015 2:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("9/11/2015 4:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("9/12/2015 1:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("9/12/2015 1:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("9/12/2015 2:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("9/12/2015 3:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("9/13/2015 4:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("9/14/2015 5:00", "M/D/YYYY H:mm").valueOf(),
moment.utc(moment.utc().subtract(2, 'days')).valueOf(),
moment.utc(moment.utc().subtract(1, 'days')).valueOf(),
moment.utc().valueOf()
]), 4, "should return 4 when the longest streak consist of several same day timestamps");
t.equal(calcLongestStreak([
moment("8/3/2015 2:00", "M/D/YYYY H:mm").valueOf(),
moment("9/11/2015 4:00", "M/D/YYYY H:mm").valueOf(),
moment("9/12/2015 1:00", "M/D/YYYY H:mm").valueOf(),
moment("9/13/2015 4:00", "M/D/YYYY H:mm").valueOf(),
moment("9/14/2015 5:00", "M/D/YYYY H:mm").valueOf(),
moment("10/11/2015 4:00", "M/D/YYYY H:mm").valueOf(),
moment("10/12/2015 1:00", "M/D/YYYY H:mm").valueOf(),
moment("10/13/2015 4:00", "M/D/YYYY H:mm").valueOf(),
moment("10/14/2015 5:00", "M/D/YYYY H:mm").valueOf()
moment.utc("8/3/2015 2:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("9/11/2015 4:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("9/12/2015 1:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("9/13/2015 4:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("9/14/2015 5:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("10/11/2015 4:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("10/12/2015 1:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("10/13/2015 4:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("10/14/2015 5:00", "M/D/YYYY H:mm").valueOf()
]), 4, "should return 4 when there are several longest streaks (same length)");
let cals = [];
const n = 100;
for (var i = 0; i < n; i++) {
cals.push(moment(moment().subtract(i, 'days')).valueOf());
cals.push(moment.utc(moment.utc().subtract(i, 'days')).valueOf());
}
cals.sort();
t.equal(calcLongestStreak(cals), n, "should return correct longest streak when there is a very long period");
t.equal(calcLongestStreak([
moment.utc(moment.utc().subtract(1, 'days')).valueOf(),
moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
], undefined), 2, "should return correct longest streak in default timezone UTC given 'undefined' timezone");
t.equal(calcLongestStreak([
moment.utc("9/11/2015 4:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("9/12/2015 2:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("9/13/2015 3:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("9/14/2015 1:00", "M/D/YYYY H:mm").valueOf()
], 'America/Los_Angeles'), 4, "should return 4 when there is the only one more-than-one-days-long streak available given 'America/Los_Angeles' timezone");
t.equal(calcLongestStreak([
moment.utc("9/11/2015 23:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("9/12/2015 2:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("9/13/2015 2:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("9/14/2015 1:00", "M/D/YYYY H:mm").valueOf()
], 'America/Los_Angeles'), 3, "should return 3 when longest streak is 3 in given 'America/Los_Angeles' timezone (but would be different in default timezone UTC)");
});