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