chore: Refactor server tests to use jest

This commit is contained in:
Bouncey
2018-11-30 14:24:16 +00:00
committed by mrugesh mohapatra
parent 4a9d22cee0
commit 438bd94895
8 changed files with 3081 additions and 717 deletions

View File

@ -2,16 +2,16 @@ module.exports = {
plugins: [ plugins: [
require.resolve('babel-plugin-transform-function-bind'), require.resolve('babel-plugin-transform-function-bind'),
require.resolve('@babel/plugin-proposal-class-properties'), require.resolve('@babel/plugin-proposal-class-properties'),
require.resolve('@babel/plugin-proposal-object-rest-spread') require.resolve('@babel/plugin-proposal-object-rest-spread'),
], ],
presets: [ presets: [
[ [
require.resolve('@babel/preset-env'), require.resolve('@babel/preset-env'),
{ {
targets: { targets: {
node: '8', node: '10',
}, },
}, },
], ],
], ],
} };

View File

@ -1,38 +0,0 @@
import _ from 'lodash/fp';
// we don't store loop protect disable key
export const removeNoprotect = _.replace(/noprotect/gi, '');
export const encodeScriptTags = _.flow(
_.replace(/<script>/gi, 'fccss'),
_.replace(/<\/script>/gi, 'fcces')
);
export const decodeScriptTags = _.flow(
_.replace(/fccss/gi, '<script>'),
_.replace(/fcces/gi, '</script>')
);
export const encodeFormAction = _.replace(
// look for attributes in a form
/<form[^>]*>/,
// val is the string within the opening form tag
// look for an `action` attribute, replace it with a fcc tag
_.replace(/action(\s*?)=/, 'fccfaa$1=')
);
export const decodeFormAction = _.replace(
/<form[^>]*>/,
_.replace(/fccfaa(\s*?)=/, 'action$1=')
);
export const encodeFcc = _.flow(
removeNoprotect,
encodeFormAction,
encodeScriptTags
);
export const decodeFcc = _.flow(
decodeFormAction,
decodeScriptTags
);

View File

@ -1,70 +0,0 @@
import test from 'tape';
import {
encodeScriptTags,
decodeScriptTags,
encodeFormAction,
decodeFormAction,
encodeFcc,
decodeFcc
} from './encode-decode.js';
const scriptDecoded = `
<script>console.log('foo')</script>
`;
const scriptEncoded = `
fccssconsole.log('foo')fcces
`;
test('encodeScriptTags', t => {
t.plan(1);
t.equal(
encodeScriptTags(scriptDecoded),
scriptEncoded
);
});
test('decodeScriptTags', t => {
t.plan(1);
t.equal(
decodeScriptTags(scriptEncoded),
scriptDecoded
);
});
const formDecoded = `
<form action ='path'>foo</form>
`;
const formEncoded = `
<form fccfaa ='path'>foo</form>
`;
test('encodeFormAction', t => {
t.plan(1);
t.equal(
encodeFormAction(formDecoded),
formEncoded
);
});
test('decodeFormAction', t => {
t.plan(1);
t.equal(
decodeFormAction(formEncoded),
formDecoded
);
});
test('encodeFcc', t => {
t.plan(1);
t.equal(
encodeFcc('//noprotect' + scriptDecoded + formDecoded),
'//' + scriptEncoded + formEncoded
);
});
test('decodeFcc', t => {
t.plan(1);
t.equal(
decodeFcc(scriptEncoded + formEncoded),
scriptDecoded + formDecoded
);
});

22
api-server/jest.config.js Normal file
View File

@ -0,0 +1,22 @@
module.exports = {
moduleNameMapper: {
'\\.(jpg|jpeg|png|svg|woff|woff2)$': '<rootDir>/src/__mocks__/fileMock.js',
// Plain CSS - match css files that don't end with
// '.module.css' https://regex101.com/r/VzwrKH/4
'^(?!.*\\.module\\.css$).*\\.css$': '<rootDir>/src/__mocks__/styleMock.js',
// CSS Modules - match files that end with 'module.css'
'\\.module\\.css$': 'identity-obj-proxy',
analytics: '<rootDir>/src/__mocks__/analyticsMock.js'
},
testPathIgnorePatterns: ['/node_modules/', '<rootDir>/.cache/'],
globals: {
__PATH_PREFIX__: ''
},
verbose: true,
transform: {
'^.+\\.js$': 'babel-jest'
},
transformIgnorePatterns: [
'node_modules/(?!(gatsby)/)'
]
};

File diff suppressed because it is too large Load Diff

View File

@ -14,16 +14,11 @@
"only-once": "npm run prelint-js && echo '/****/' && echo 'Seeding Database' && echo '/****/' && SEEDING=true node seed/index.js && echo '/****/' && echo 'Seeding Completed' && echo '/****/'", "only-once": "npm run prelint-js && echo '/****/' && echo 'Seeding Database' && echo '/****/' && SEEDING=true node seed/index.js && echo '/****/' && echo 'Seeding Completed' && echo '/****/'",
"postonly-once": "gulp generate-migration-map", "postonly-once": "gulp generate-migration-map",
"postseed": "node post-seed.js", "postseed": "node post-seed.js",
"prelint-js": "npm run ensure-env",
"pretest": "npm run lint",
"seed": "node ../seed/index.js", "seed": "node ../seed/index.js",
"snyk-protect": "snyk protect", "snyk-protect": "snyk protect",
"start": "DEBUG=fcc* babel-node server/server.js", "start": "DEBUG=fcc* babel-node server/server.js",
"start-production": "node pm2Start", "start-production": "node pm2Start",
"test": "npm run test-js", "test": "jest"
"test-js": "npm run test-js-common && npm run test-js-server",
"test-js-common": "tape -r babel-register \"common/**/*.test.js\" | tap-spec",
"test-js-server": "tape -r babel-register \"server/**/*.test.js\" | tap-spec"
}, },
"license": "(BSD-3-Clause AND CC-BY-SA-4.0)", "license": "(BSD-3-Clause AND CC-BY-SA-4.0)",
"dependencies": { "dependencies": {
@ -88,6 +83,8 @@
"@babel/preset-env": "^7.0.0", "@babel/preset-env": "^7.0.0",
"@babel/register": "^7.0.0", "@babel/register": "^7.0.0",
"adler32": "~0.1.7", "adler32": "~0.1.7",
"babel-core": "^7.0.0-bridge.0",
"babel-jest": "^23.6.0",
"babel-plugin-transform-function-bind": "^6.22.0", "babel-plugin-transform-function-bind": "^6.22.0",
"babel-plugin-transform-imports": "^1.5.0", "babel-plugin-transform-imports": "^1.5.0",
"commitizen": "^2.9.6", "commitizen": "^2.9.6",
@ -96,19 +93,22 @@
"eslint-config-freecodecamp": "^1.1.1", "eslint-config-freecodecamp": "^1.1.1",
"eslint-plugin-import": "^2.9.0", "eslint-plugin-import": "^2.9.0",
"husky": "^0.14.3", "husky": "^0.14.3",
"jest": "^23.6.0",
"joi": "^13.1.2", "joi": "^13.1.2",
"joi-objectid": "^2.0.0", "joi-objectid": "^2.0.0",
"loopback-component-explorer": "^6.3.1", "loopback-component-explorer": "^6.3.1",
"nodemon": "^1.18.4", "nodemon": "^1.18.4",
"pm2": "^3.0.3", "pm2": "^3.0.3",
"prettier": "^1.14.2", "prettier": "^1.14.2",
"sinon": "^2.0.0", "sinon": "^7.1.1",
"tap-difflet": "^0.7.0", "tap-difflet": "^0.7.0",
"tap-spec": "^5.0.0", "tap-spec": "^5.0.0",
"tape": "^4.2.2",
"validate-commit-msg": "^2.12.2" "validate-commit-msg": "^2.12.2"
}, },
"snyk": true, "snyk": true,
"resolutions": {
"babel-core": "7.0.0-bridge.0"
},
"config": { "config": {
"commitizen": { "commitizen": {
"path": "./node_modules/cz-freecodecamp" "path": "./node_modules/cz-freecodecamp"

View File

@ -1,75 +1,83 @@
/* global describe expect it */
import moment from 'moment-timezone'; import moment from 'moment-timezone';
import { dayCount } from './date-utils'; import { dayCount } from './date-utils';
import test from 'tape';
const PST = 'America/Los_Angeles'; const PST = 'America/Los_Angeles';
test('Day count between two epochs (inclusive) calculation', function(t) { describe('date utils', () => {
t.plan(7); describe('dayCount', () => {
it('should return 1 day given epochs of the same day', () => {
t.equal( expect(
dayCount([ dayCount([
moment.utc('8/3/2015 3: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() moment.utc('8/3/2015 2:00', 'M/D/YYYY H:mm').valueOf()
]), ])
1, ).toEqual(1);
'should return 1 day given epochs of the same day' });
);
t.equal( it('should return 1 day given same epochs', () => {
expect(
dayCount([ dayCount([
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(),
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, ).toEqual(1);
'should return 1 day given same epochs' });
);
t.equal( it('should return 2 days when there is a 24 hours difference', () => {});
expect(
dayCount([ dayCount([
moment.utc('8/4/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() moment.utc('8/3/2015 2:00', 'M/D/YYYY H:mm').valueOf()
]), ])
2, ).toEqual(2);
'should return 2 days when there is a 24 hours difference'
);
t.equal( it(
'should return 2 days when the diff is less than 24h but ' +
'different in UTC',
() => {}
);
expect(
dayCount([ dayCount([
moment.utc('8/4/2015 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() moment.utc('8/3/2015 23:00', 'M/D/YYYY H:mm').valueOf()
]), ])
2, ).toEqual(2);
'should return 2 days when the diff is less than 24h but different in UTC'
);
t.equal( it(
dayCount([
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()
], PST),
1,
'should return 1 day when the diff is less than 24h ' + 'should return 1 day when the diff is less than 24h ' +
'and days are different in UTC, but given PST' 'and days are different in UTC, but given PST',
() => {}
); );
expect(
dayCount(
[
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()
],
PST
)
).toEqual(1);
t.equal( it('should return correct count when there is very big period', () => {});
expect(
dayCount([ dayCount([
moment.utc('10/27/2015 1:00', 'M/D/YYYY H:mm').valueOf(), 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() moment.utc('5/12/1982 1:00', 'M/D/YYYY H:mm').valueOf()
]), ])
12222, ).toEqual(12222);
'should return correct count when there is very big period'
);
t.equal( it(
'should return 2 days when there is a 24 hours difference ' +
'between dates given `undefined` timezone',
() => {}
);
expect(
dayCount([ dayCount([
moment.utc('8/4/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() moment.utc('8/3/2015 2:00', 'M/D/YYYY H:mm').valueOf()
]), ])
2, ).toEqual(2);
'should return 2 days when there is a 24 hours difference ' + });
'between dates given `undefined` timezone'
);
}); });

View File

@ -1,4 +1,4 @@
import test from 'tape'; /* global describe it expect afterAll */
import moment from 'moment-timezone'; import moment from 'moment-timezone';
import sinon from 'sinon'; import sinon from 'sinon';
@ -12,108 +12,125 @@ import {
const clock = sinon.useFakeTimers(1454526000000); const clock = sinon.useFakeTimers(1454526000000);
const PST = 'America/Los_Angeles'; const PST = 'America/Los_Angeles';
test('Prepare calendar items', function(t) { describe('user stats', () => {
afterAll(() => {
clock.restore();
});
t.plan(5); describe('prepUniqueDaysByHours', () => {
it(
t.deepEqual( 'should return correct epoch when all entries fall into ' +
'one day in UTC',
() => {
expect(
prepUniqueDaysByHours([ prepUniqueDaysByHours([
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(),
moment.utc('8/3/2015 14:00', 'M/D/YYYY H:mm').valueOf(), moment.utc('8/3/2015 14:00', 'M/D/YYYY H:mm').valueOf(),
moment.utc('8/3/2015 20:00', 'M/D/YYYY H:mm').valueOf() moment.utc('8/3/2015 20:00', 'M/D/YYYY H:mm').valueOf()
]), ])
[1438567200000], ).toEqual([1438567200000]);
'should return correct epoch when all entries fall into one day in UTC' }
); );
t.deepEqual( it('should return correct epoch when given two identical dates', () => {
expect(
prepUniqueDaysByHours([ prepUniqueDaysByHours([
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(),
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()
]), ])
[1438567200000], ).toEqual([1438567200000]);
'should return correct epoch when given two identical dates' });
);
it('should return 2 epochs when dates fall into two days in PST', () => {
t.deepEqual( expect(
prepUniqueDaysByHours([ prepUniqueDaysByHours(
[
// 8/2/2015 in America/Los_Angeles // 8/2/2015 in America/Los_Angeles
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(),
moment.utc('8/3/2015 14:00', 'M/D/YYYY H:mm').valueOf(), moment.utc('8/3/2015 14:00', 'M/D/YYYY H:mm').valueOf(),
moment.utc('8/3/2015 20:00', 'M/D/YYYY H:mm').valueOf() moment.utc('8/3/2015 20:00', 'M/D/YYYY H:mm').valueOf()
], PST), ],
[1438567200000, 1438610400000], PST
'should return 2 epochs when dates fall into two days in PST' )
); ).toEqual([1438567200000, 1438610400000]);
});
t.deepEqual( it('should return 3 epochs when dates fall into three days', () => {
expect(
prepUniqueDaysByHours([ prepUniqueDaysByHours([
moment.utc('8/1/2015 2:00', 'M/D/YYYY H:mm').valueOf(), moment.utc('8/1/2015 2:00', 'M/D/YYYY H:mm').valueOf(),
moment.utc('3/3/2015 14:00', 'M/D/YYYY H:mm').valueOf(), moment.utc('3/3/2015 14:00', 'M/D/YYYY H:mm').valueOf(),
moment.utc('9/30/2014 20:00', 'M/D/YYYY H:mm').valueOf() moment.utc('9/30/2014 20:00', 'M/D/YYYY H:mm').valueOf()
]), ])
[1412107200000, 1425391200000, 1438394400000], ).toEqual([1412107200000, 1425391200000, 1438394400000]);
'should return 3 epochs when dates fall into three days'
);
t.deepEqual(
prepUniqueDaysByHours([
1438387200000, 1425340800000, 1412035200000
]),
[1412035200000, 1425340800000, 1438387200000],
'should return same but sorted array if all input dates are start of day'
);
}); });
test('Current streak calculation', function(t) { it(
'should return same but sorted array if all input dates are ' +
'start of day',
() => {
expect(
prepUniqueDaysByHours([1438387200000, 1425340800000, 1412035200000])
).toEqual([1412035200000, 1425340800000, 1438387200000]);
}
);
});
t.plan(11); describe('calcCurrentStreak', function() {
it('should return 1 day when today one challenge was completed', () => {
t.equal( expect(
calcCurrentStreak( calcCurrentStreak(
prepUniqueDaysByHours([ prepUniqueDaysByHours([
moment.utc(moment.utc().subtract(1, 'hours')).valueOf() moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
]) ])
), )
1, ).toEqual(1);
'should return 1 day when today one challenge was completed' });
);
t.equal( it(
'should return 1 day when today more than one challenge ' +
'was completed',
() => {
expect(
calcCurrentStreak( calcCurrentStreak(
prepUniqueDaysByHours([ prepUniqueDaysByHours([
moment.utc(moment.utc().subtract(1, 'hours')).valueOf(), moment.utc(moment.utc().subtract(1, 'hours')).valueOf(),
moment.utc(moment.utc().subtract(1, 'hours')).valueOf() moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
]) ])
), )
1, ).toEqual(1);
'should return 1 day when today more than one challenge was completed' }
); );
t.equal( it('should return 0 days when today 0 challenges were completed', () => {
expect(
calcCurrentStreak( calcCurrentStreak(
prepUniqueDaysByHours([ prepUniqueDaysByHours([
moment.utc('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, ).toEqual(0);
'should return 0 day when today 0 challenges were completed' });
);
t.equal( it(
'should return 2 days when today and yesterday challenges were ' +
'completed',
() => {
expect(
calcCurrentStreak( calcCurrentStreak(
prepUniqueDaysByHours([ prepUniqueDaysByHours([
moment.utc(moment.utc().subtract(1, 'days')).valueOf(), moment.utc(moment.utc().subtract(1, 'days')).valueOf(),
moment.utc(moment.utc().subtract(1, 'hours')).valueOf() moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
]) ])
), )
2, ).toEqual(2);
'should return 2 days when today and yesterday challenges were completed' }
); );
t.equal( it(
'should return 3 when today and for two days before user was ' + 'active',
() => {
expect(
calcCurrentStreak( calcCurrentStreak(
prepUniqueDaysByHours([ prepUniqueDaysByHours([
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(),
@ -128,108 +145,170 @@ test('Current streak calculation', function(t) {
moment.utc(moment.utc().subtract(1, 'days')).valueOf(), moment.utc(moment.utc().subtract(1, 'days')).valueOf(),
moment.utc(moment.utc().subtract(1, 'hours')).valueOf() moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
]) ])
), )
3, ).toEqual(3);
'should return 3 when today and for two days before user was activity' }
); );
t.equal( it(
'should return 1 when there is a 1.5 day long break and ' +
'dates fall into two days separated by third',
() => {
expect(
calcCurrentStreak( calcCurrentStreak(
prepUniqueDaysByHours([ prepUniqueDaysByHours([
moment.utc(moment.utc().subtract(47, 'hours')).valueOf(), moment.utc(moment.utc().subtract(47, 'hours')).valueOf(),
moment.utc(moment.utc().subtract(11, 'hours')).valueOf() moment.utc(moment.utc().subtract(11, 'hours')).valueOf()
]) ])
), )
1, ).toEqual(1);
'should return 1 when there is 1.5 days long break and ' + }
'dates fall into two days separated by third'
); );
t.equal( it(
'should return 2 when the break is more than 1.5 days ' +
'but dates fall into two consecutive days',
() => {
expect(
calcCurrentStreak( calcCurrentStreak(
prepUniqueDaysByHours([ prepUniqueDaysByHours([
moment.utc(moment.utc().subtract(40, 'hours')).valueOf(), moment.utc(moment.utc().subtract(40, 'hours')).valueOf(),
moment.utc(moment.utc().subtract(1, 'hours')).valueOf() moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
]) ])
), )
2, ).toEqual(2);
'should return 2 when the break is more than 1.5 days ' + }
'but dates fall into two consecutive days'
); );
t.equal( it(
'should return correct count in default timezone UTC ' +
'given `undefined` timezone',
() => {
expect(
calcCurrentStreak( calcCurrentStreak(
prepUniqueDaysByHours([ prepUniqueDaysByHours([
moment.utc(moment.utc().subtract(1, 'hours')).valueOf(), moment.utc(moment.utc().subtract(1, 'hours')).valueOf(),
moment.utc(moment.utc().subtract(1, 'hours')).valueOf() moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
]) ])
), )
1, ).toEqual(1);
'should return correct count in default timezone UTC ' + }
'given `undefined` timezone'
); );
t.equal( it(
'should return 2 days when today and yesterday ' +
'challenges were completed given PST',
() => {
expect(
calcCurrentStreak( calcCurrentStreak(
prepUniqueDaysByHours([ prepUniqueDaysByHours(
[
moment.utc(moment.utc().subtract(1, 'days')).valueOf(), moment.utc(moment.utc().subtract(1, 'days')).valueOf(),
moment.utc(moment.utc().subtract(1, 'hours')).valueOf() moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
], PST), ],
PST PST
), ),
2, PST
'should return 2 days when today and yesterday ' + )
'challenges were completed given PST' ).toEqual(2);
}
); );
t.equal( it(
calcCurrentStreak(
prepUniqueDaysByHours([
1453174506164, 1453175436725, 1453252466853, 1453294968225,
1453383782844, 1453431903117, 1453471373080, 1453594733026,
1453645014058, 1453746762747, 1453747659197, 1453748029416,
1453818029213, 1453951796007, 1453988570615, 1454069704441,
1454203673979, 1454294055498, 1454333545125, 1454415163903,
1454519128123, moment.tz(PST).valueOf()
], PST),
PST
),
17,
'should return 17 when there is no break in given timezone ' + 'should return 17 when there is no break in given timezone ' +
'(but would be the break if in UTC)' '(but would be the break if in UTC)',
() => {
expect(
calcCurrentStreak(
prepUniqueDaysByHours(
[
1453174506164,
1453175436725,
1453252466853,
1453294968225,
1453383782844,
1453431903117,
1453471373080,
1453594733026,
1453645014058,
1453746762747,
1453747659197,
1453748029416,
1453818029213,
1453951796007,
1453988570615,
1454069704441,
1454203673979,
1454294055498,
1454333545125,
1454415163903,
1454519128123,
moment.tz(PST).valueOf()
],
PST
),
PST
)
).toEqual(17);
}
); );
t.equal( it(
'should return 4 when there is a break in UTC ' +
'(but would be no break in PST)',
() => {
expect(
calcCurrentStreak( calcCurrentStreak(
prepUniqueDaysByHours([ prepUniqueDaysByHours([
1453174506164, 1453175436725, 1453252466853, 1453294968225, 1453174506164,
1453383782844, 1453431903117, 1453471373080, 1453594733026, 1453175436725,
1453645014058, 1453746762747, 1453747659197, 1453748029416, 1453252466853,
1453818029213, 1453951796007, 1453988570615, 1454069704441, 1453294968225,
1454203673979, 1454294055498, 1454333545125, 1454415163903, 1453383782844,
1454519128123, moment.utc().valueOf() 1453431903117,
1453471373080,
1453594733026,
1453645014058,
1453746762747,
1453747659197,
1453748029416,
1453818029213,
1453951796007,
1453988570615,
1454069704441,
1454203673979,
1454294055498,
1454333545125,
1454415163903,
1454519128123,
moment.utc().valueOf()
]) ])
), )
4, ).toEqual(4);
'should return 4 when there is a break in UTC ' + }
'(but would be no break in PST)'
); );
}); });
test('Longest streak calculation', function(t) { describe('calcLongestStreak', function() {
t.plan(14); it(
'should return 1 when there is the only one one-day-long ' +
t.equal( 'streak available',
() => {
expect(
calcLongestStreak( calcLongestStreak(
prepUniqueDaysByHours([ prepUniqueDaysByHours([
moment.utc('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, ).toEqual(1);
'should return 1 when there is the only one one-day-long streak available' }
); );
t.equal( it(
'should return 4 when there is the only one ' +
'more-than-one-days-long streak available',
() => {
expect(
calcLongestStreak( calcLongestStreak(
prepUniqueDaysByHours([ prepUniqueDaysByHours([
moment.utc('9/11/2015 4:00', 'M/D/YYYY H:mm').valueOf(), moment.utc('9/11/2015 4:00', 'M/D/YYYY H:mm').valueOf(),
@ -237,34 +316,38 @@ test('Longest streak calculation', function(t) {
moment.utc('9/13/2015 3: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() moment.utc('9/14/2015 1:00', 'M/D/YYYY H:mm').valueOf()
]) ])
), )
4, ).toEqual(4);
'should return 4 when there is the only one ' + }
'more-than-one-days-long streak available'
); );
t.equal( it(
'should return 1 when there is only one one-day-long streak ' +
'and it is today',
() => {
expect(
calcLongestStreak( calcLongestStreak(
prepUniqueDaysByHours([ prepUniqueDaysByHours([
moment.utc(moment.utc().subtract(1, 'hours')).valueOf() moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
]) ])
), )
1, ).toEqual(1);
'should return 1 when there is only one one-day-long streak and it is today' }
); );
t.equal( it('should return 2 when yesterday and today makes longest streak', () => {
expect(
calcLongestStreak( calcLongestStreak(
prepUniqueDaysByHours([ prepUniqueDaysByHours([
moment.utc(moment.utc().subtract(1, 'days')).valueOf(), moment.utc(moment.utc().subtract(1, 'days')).valueOf(),
moment.utc(moment.utc().subtract(1, 'hours')).valueOf() moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
]) ])
), )
2, ).toEqual(2);
'should return 2 when yesterday and today makes longest streak' });
);
t.equal( it('should return 4 when there is a month long break', () => {
expect(
calcLongestStreak( calcLongestStreak(
prepUniqueDaysByHours([ prepUniqueDaysByHours([
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(),
@ -276,31 +359,41 @@ test('Longest streak calculation', function(t) {
moment.utc('10/7/2015 5: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() moment.utc('11/3/2015 2:00', 'M/D/YYYY H:mm').valueOf()
]) ])
), )
4, ).toEqual(4);
'should return 4 when there is a month long break' });
);
t.equal( it(
'should return 2 when there is a more than 1.5 days ' +
'long break of (36 hours)',
() => {
expect(
calcLongestStreak( calcLongestStreak(
prepUniqueDaysByHours([ prepUniqueDaysByHours([
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(),
moment.utc('9/11/2015 4: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 15:30', 'M/D/YYYY H:mm').valueOf(), moment.utc('9/12/2015 15:30', 'M/D/YYYY H:mm').valueOf(),
moment.utc(moment.utc('9/12/2015 15:30', 'M/D/YYYY H:mm') moment
.add(37, 'hours')).valueOf(), .utc(
moment
.utc('9/12/2015 15:30', '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/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('9/15/2015 4:00', 'M/D/YYYY H:mm').valueOf(),
moment.utc('10/3/2015 2:00', 'M/D/YYYY H:mm').valueOf() moment.utc('10/3/2015 2:00', 'M/D/YYYY H:mm').valueOf()
]) ])
), )
2, ).toEqual(2);
'should return 2 when there is a more than 1.5 days ' + }
'long break of (36 hours)'
); );
t.equal( it(
'should return 4 when the longest streak consist of ' +
'several same day timestamps',
() => {
expect(
calcLongestStreak( calcLongestStreak(
prepUniqueDaysByHours([ prepUniqueDaysByHours([
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(),
@ -315,13 +408,16 @@ test('Longest streak calculation', function(t) {
moment.utc(moment.utc().subtract(1, 'days')).valueOf(), moment.utc(moment.utc().subtract(1, 'days')).valueOf(),
moment.utc().valueOf() moment.utc().valueOf()
]) ])
), )
4, ).toEqual(4);
'should return 4 when the longest streak consist of ' + }
'several same day timestamps'
); );
t.equal( it(
'should return 4 when there are several longest streaks ' +
'(same length)',
() => {
expect(
calcLongestStreak( calcLongestStreak(
prepUniqueDaysByHours([ prepUniqueDaysByHours([
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(),
@ -334,96 +430,151 @@ test('Longest streak calculation', function(t) {
moment.utc('10/13/2015 4: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() moment.utc('10/14/2015 5:00', 'M/D/YYYY H:mm').valueOf()
]) ])
), )
4, ).toEqual(4);
'should return 4 when there are several longest streaks (same length)' }
); );
it(
'should return correct longest streak when there is a very ' +
'long period',
() => {
let cals = []; let cals = [];
const n = 100; const n = 100;
for (var i = 0; i < n; i++) { for (let i = 0; i < n; i++) {
cals.push(moment.utc(moment.utc().subtract(i, 'days')).valueOf()); cals.push(moment.utc(moment.utc().subtract(i, 'days')).valueOf());
} }
t.equal( expect(calcLongestStreak(prepUniqueDaysByHours(cals))).toEqual(n);
calcLongestStreak(prepUniqueDaysByHours(cals)), }
n,
'should return correct longest streak when there is a very long period'
); );
t.equal( it(
'should return correct longest streak in default timezone ' +
'UTC given `undefined` timezone',
() => {
expect(
calcLongestStreak( calcLongestStreak(
prepUniqueDaysByHours([ prepUniqueDaysByHours([
moment.utc(moment.utc().subtract(1, 'days')).valueOf(), moment.utc(moment.utc().subtract(1, 'days')).valueOf(),
moment.utc(moment.utc().subtract(1, 'hours')).valueOf() moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
]) ])
), )
2, ).toEqual(2);
'should return correct longest streak in default timezone ' + }
'UTC given `undefined` timezone'
); );
t.equal( it(
'should return 4 when there is the only one more-than-one-days-long ' +
'streak available given PST',
() => {
expect(
calcLongestStreak( calcLongestStreak(
prepUniqueDaysByHours([ prepUniqueDaysByHours([
moment.utc('9/11/2015 4: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/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/13/2015 3:00', 'M/D/YYYY H:mm').valueOf(),
moment.utc('9/14/2015 1:00', 'M/D/YYYY H:mm').valueOf() moment.utc('9/14/2015 1:00', 'M/D/YYYY H:mm').valueOf()
]), PST ]),
), PST
4, )
'should return 4 when there is the only one more-than-one-days-long ' + ).toEqual(4);
'streak available given PST' }
); );
t.equal( it(
'should return 3 when longest streak is 3 in PST ' +
'(but would be different in default timezone UTC)',
() => {
expect(
calcLongestStreak( calcLongestStreak(
prepUniqueDaysByHours([ prepUniqueDaysByHours(
[
moment.utc('9/11/2015 23:00', 'M/D/YYYY H:mm').valueOf(), 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/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/13/2015 2:00', 'M/D/YYYY H:mm').valueOf(),
moment.utc('9/14/2015 6:00', 'M/D/YYYY H:mm').valueOf() moment.utc('9/14/2015 6:00', 'M/D/YYYY H:mm').valueOf()
], PST), PST ],
),
3,
'should return 3 when longest streak is 3 in PST ' +
'(but would be different in default timezone UTC)'
);
t.equal(
calcLongestStreak(
prepUniqueDaysByHours([
1453174506164, 1453175436725, 1453252466853, 1453294968225,
1453383782844, 1453431903117, 1453471373080, 1453594733026,
1453645014058, 1453746762747, 1453747659197, 1453748029416,
1453818029213, 1453951796007, 1453988570615, 1454069704441,
1454203673979, 1454294055498, 1454333545125, 1454415163903,
1454519128123, moment.tz(PST).valueOf()
], PST),
PST PST
), ),
17, PST
'should return 17 when there is no break in PST ' + )
'(but would be break in UTC) and it is current' ).toEqual(3);
}
); );
t.equal( it(
'should return 17 when there is no break in PST ' +
'(but would be break in UTC) and it is current',
() => {
expect(
calcLongestStreak(
prepUniqueDaysByHours(
[
1453174506164,
1453175436725,
1453252466853,
1453294968225,
1453383782844,
1453431903117,
1453471373080,
1453594733026,
1453645014058,
1453746762747,
1453747659197,
1453748029416,
1453818029213,
1453951796007,
1453988570615,
1454069704441,
1454203673979,
1454294055498,
1454333545125,
1454415163903,
1454519128123,
moment.tz(PST).valueOf()
],
PST
),
PST
)
).toEqual(17);
}
);
it(
'should return a streak of 4 when there is a break in UTC ' +
'(but no break in PST)',
() => {
expect(
calcLongestStreak( calcLongestStreak(
prepUniqueDaysByHours([ prepUniqueDaysByHours([
1453174506164, 1453175436725, 1453252466853, 1453294968225, 1453174506164,
1453383782844, 1453431903117, 1453471373080, 1453594733026, 1453175436725,
1453645014058, 1453746762747, 1453747659197, 1453748029416, 1453252466853,
1453818029213, 1453951796007, 1453988570615, 1454069704441, 1453294968225,
1454203673979, 1454294055498, 1454333545125, 1454415163903, 1453383782844,
1454519128123, moment.utc().valueOf() 1453431903117,
1453471373080,
1453594733026,
1453645014058,
1453746762747,
1453747659197,
1453748029416,
1453818029213,
1453951796007,
1453988570615,
1454069704441,
1454203673979,
1454294055498,
1454333545125,
1454415163903,
1454519128123,
moment.utc().valueOf()
]) ])
), )
4, ).toEqual(4);
'should return 4 when there is a break in UTC (but no break in PST)' }
); );
}); });
test.onFinish(() => {
clock.restore();
}); });