chore: Refactor server tests to use jest
This commit is contained in:
committed by
mrugesh mohapatra
parent
4a9d22cee0
commit
438bd94895
@ -2,16 +2,16 @@ module.exports = {
|
||||
plugins: [
|
||||
require.resolve('babel-plugin-transform-function-bind'),
|
||||
require.resolve('@babel/plugin-proposal-class-properties'),
|
||||
require.resolve('@babel/plugin-proposal-object-rest-spread')
|
||||
require.resolve('@babel/plugin-proposal-object-rest-spread'),
|
||||
],
|
||||
presets: [
|
||||
[
|
||||
require.resolve('@babel/preset-env'),
|
||||
{
|
||||
targets: {
|
||||
node: '8',
|
||||
node: '10',
|
||||
},
|
||||
},
|
||||
],
|
||||
],
|
||||
}
|
||||
};
|
||||
|
@ -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
|
||||
);
|
@ -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
22
api-server/jest.config.js
Normal 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)/)'
|
||||
]
|
||||
};
|
2547
api-server/package-lock.json
generated
2547
api-server/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -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 '/****/'",
|
||||
"postonly-once": "gulp generate-migration-map",
|
||||
"postseed": "node post-seed.js",
|
||||
"prelint-js": "npm run ensure-env",
|
||||
"pretest": "npm run lint",
|
||||
"seed": "node ../seed/index.js",
|
||||
"snyk-protect": "snyk protect",
|
||||
"start": "DEBUG=fcc* babel-node server/server.js",
|
||||
"start-production": "node pm2Start",
|
||||
"test": "npm run test-js",
|
||||
"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"
|
||||
"test": "jest"
|
||||
},
|
||||
"license": "(BSD-3-Clause AND CC-BY-SA-4.0)",
|
||||
"dependencies": {
|
||||
@ -88,6 +83,8 @@
|
||||
"@babel/preset-env": "^7.0.0",
|
||||
"@babel/register": "^7.0.0",
|
||||
"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-imports": "^1.5.0",
|
||||
"commitizen": "^2.9.6",
|
||||
@ -96,19 +93,22 @@
|
||||
"eslint-config-freecodecamp": "^1.1.1",
|
||||
"eslint-plugin-import": "^2.9.0",
|
||||
"husky": "^0.14.3",
|
||||
"jest": "^23.6.0",
|
||||
"joi": "^13.1.2",
|
||||
"joi-objectid": "^2.0.0",
|
||||
"loopback-component-explorer": "^6.3.1",
|
||||
"nodemon": "^1.18.4",
|
||||
"pm2": "^3.0.3",
|
||||
"prettier": "^1.14.2",
|
||||
"sinon": "^2.0.0",
|
||||
"sinon": "^7.1.1",
|
||||
"tap-difflet": "^0.7.0",
|
||||
"tap-spec": "^5.0.0",
|
||||
"tape": "^4.2.2",
|
||||
"validate-commit-msg": "^2.12.2"
|
||||
},
|
||||
"snyk": true,
|
||||
"resolutions": {
|
||||
"babel-core": "7.0.0-bridge.0"
|
||||
},
|
||||
"config": {
|
||||
"commitizen": {
|
||||
"path": "./node_modules/cz-freecodecamp"
|
||||
|
@ -1,75 +1,83 @@
|
||||
/* global describe expect it */
|
||||
import moment from 'moment-timezone';
|
||||
|
||||
import { dayCount } from './date-utils';
|
||||
import test from 'tape';
|
||||
|
||||
const PST = 'America/Los_Angeles';
|
||||
|
||||
test('Day count between two epochs (inclusive) calculation', function(t) {
|
||||
t.plan(7);
|
||||
|
||||
t.equal(
|
||||
describe('date utils', () => {
|
||||
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()
|
||||
]),
|
||||
1,
|
||||
'should return 1 day given epochs of the same day'
|
||||
);
|
||||
])
|
||||
).toEqual(1);
|
||||
});
|
||||
|
||||
t.equal(
|
||||
it('should return 1 day given same epochs', () => {
|
||||
expect(
|
||||
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()
|
||||
]),
|
||||
1,
|
||||
'should return 1 day given same epochs'
|
||||
);
|
||||
])
|
||||
).toEqual(1);
|
||||
});
|
||||
|
||||
t.equal(
|
||||
it('should return 2 days when there is a 24 hours difference', () => {});
|
||||
expect(
|
||||
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()
|
||||
]),
|
||||
2,
|
||||
'should return 2 days when there is a 24 hours difference'
|
||||
);
|
||||
])
|
||||
).toEqual(2);
|
||||
|
||||
t.equal(
|
||||
it(
|
||||
'should return 2 days when the diff is less than 24h but ' +
|
||||
'different in UTC',
|
||||
() => {}
|
||||
);
|
||||
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()
|
||||
]),
|
||||
2,
|
||||
'should return 2 days when the diff is less than 24h but different in UTC'
|
||||
);
|
||||
])
|
||||
).toEqual(2);
|
||||
|
||||
t.equal(
|
||||
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,
|
||||
it(
|
||||
'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([
|
||||
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'
|
||||
);
|
||||
])
|
||||
).toEqual(12222);
|
||||
|
||||
t.equal(
|
||||
it(
|
||||
'should return 2 days when there is a 24 hours difference ' +
|
||||
'between dates given `undefined` timezone',
|
||||
() => {}
|
||||
);
|
||||
expect(
|
||||
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()
|
||||
]),
|
||||
2,
|
||||
'should return 2 days when there is a 24 hours difference ' +
|
||||
'between dates given `undefined` timezone'
|
||||
);
|
||||
])
|
||||
).toEqual(2);
|
||||
});
|
||||
});
|
||||
|
@ -1,4 +1,4 @@
|
||||
import test from 'tape';
|
||||
/* global describe it expect afterAll */
|
||||
import moment from 'moment-timezone';
|
||||
import sinon from 'sinon';
|
||||
|
||||
@ -12,108 +12,125 @@ import {
|
||||
const clock = sinon.useFakeTimers(1454526000000);
|
||||
const PST = 'America/Los_Angeles';
|
||||
|
||||
test('Prepare calendar items', function(t) {
|
||||
describe('user stats', () => {
|
||||
afterAll(() => {
|
||||
clock.restore();
|
||||
});
|
||||
|
||||
t.plan(5);
|
||||
|
||||
t.deepEqual(
|
||||
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()
|
||||
]),
|
||||
[1438567200000],
|
||||
'should return correct epoch when all entries fall into one day in UTC'
|
||||
])
|
||||
).toEqual([1438567200000]);
|
||||
}
|
||||
);
|
||||
|
||||
t.deepEqual(
|
||||
it('should return correct epoch when given two identical dates', () => {
|
||||
expect(
|
||||
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()
|
||||
]),
|
||||
[1438567200000],
|
||||
'should return correct epoch when given two identical dates'
|
||||
);
|
||||
])
|
||||
).toEqual([1438567200000]);
|
||||
});
|
||||
|
||||
|
||||
t.deepEqual(
|
||||
prepUniqueDaysByHours([
|
||||
it('should return 2 epochs when dates fall into two days in PST', () => {
|
||||
expect(
|
||||
prepUniqueDaysByHours(
|
||||
[
|
||||
// 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 14:00', 'M/D/YYYY H:mm').valueOf(),
|
||||
moment.utc('8/3/2015 20:00', 'M/D/YYYY H:mm').valueOf()
|
||||
], PST),
|
||||
[1438567200000, 1438610400000],
|
||||
'should return 2 epochs when dates fall into two days in PST'
|
||||
);
|
||||
],
|
||||
PST
|
||||
)
|
||||
).toEqual([1438567200000, 1438610400000]);
|
||||
});
|
||||
|
||||
t.deepEqual(
|
||||
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()
|
||||
]),
|
||||
[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'
|
||||
);
|
||||
|
||||
])
|
||||
).toEqual([1412107200000, 1425391200000, 1438394400000]);
|
||||
});
|
||||
|
||||
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);
|
||||
|
||||
t.equal(
|
||||
describe('calcCurrentStreak', function() {
|
||||
it('should return 1 day when today one challenge was completed', () => {
|
||||
expect(
|
||||
calcCurrentStreak(
|
||||
prepUniqueDaysByHours([
|
||||
moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
|
||||
])
|
||||
),
|
||||
1,
|
||||
'should return 1 day when today one challenge was completed'
|
||||
);
|
||||
)
|
||||
).toEqual(1);
|
||||
});
|
||||
|
||||
t.equal(
|
||||
it(
|
||||
'should return 1 day when today more than one challenge ' +
|
||||
'was completed',
|
||||
() => {
|
||||
expect(
|
||||
calcCurrentStreak(
|
||||
prepUniqueDaysByHours([
|
||||
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'
|
||||
)
|
||||
).toEqual(1);
|
||||
}
|
||||
);
|
||||
|
||||
t.equal(
|
||||
it('should return 0 days when today 0 challenges were completed', () => {
|
||||
expect(
|
||||
calcCurrentStreak(
|
||||
prepUniqueDaysByHours([
|
||||
moment.utc('9/11/2015 4:00', 'M/D/YYYY H:mm').valueOf()
|
||||
])
|
||||
),
|
||||
0,
|
||||
'should return 0 day when today 0 challenges were completed'
|
||||
);
|
||||
)
|
||||
).toEqual(0);
|
||||
});
|
||||
|
||||
t.equal(
|
||||
it(
|
||||
'should return 2 days when today and yesterday challenges were ' +
|
||||
'completed',
|
||||
() => {
|
||||
expect(
|
||||
calcCurrentStreak(
|
||||
prepUniqueDaysByHours([
|
||||
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'
|
||||
)
|
||||
).toEqual(2);
|
||||
}
|
||||
);
|
||||
|
||||
t.equal(
|
||||
it(
|
||||
'should return 3 when today and for two days before user was ' + 'active',
|
||||
() => {
|
||||
expect(
|
||||
calcCurrentStreak(
|
||||
prepUniqueDaysByHours([
|
||||
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, 'hours')).valueOf()
|
||||
])
|
||||
),
|
||||
3,
|
||||
'should return 3 when today and for two days before user was activity'
|
||||
)
|
||||
).toEqual(3);
|
||||
}
|
||||
);
|
||||
|
||||
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(
|
||||
prepUniqueDaysByHours([
|
||||
moment.utc(moment.utc().subtract(47, 'hours')).valueOf(),
|
||||
moment.utc(moment.utc().subtract(11, 'hours')).valueOf()
|
||||
])
|
||||
),
|
||||
1,
|
||||
'should return 1 when there is 1.5 days long break and ' +
|
||||
'dates fall into two days separated by third'
|
||||
)
|
||||
).toEqual(1);
|
||||
}
|
||||
);
|
||||
|
||||
t.equal(
|
||||
it(
|
||||
'should return 2 when the break is more than 1.5 days ' +
|
||||
'but dates fall into two consecutive days',
|
||||
() => {
|
||||
expect(
|
||||
calcCurrentStreak(
|
||||
prepUniqueDaysByHours([
|
||||
moment.utc(moment.utc().subtract(40, '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(2);
|
||||
}
|
||||
);
|
||||
|
||||
t.equal(
|
||||
it(
|
||||
'should return correct count in default timezone UTC ' +
|
||||
'given `undefined` timezone',
|
||||
() => {
|
||||
expect(
|
||||
calcCurrentStreak(
|
||||
prepUniqueDaysByHours([
|
||||
moment.utc(moment.utc().subtract(1, 'hours')).valueOf(),
|
||||
moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
|
||||
])
|
||||
),
|
||||
1,
|
||||
'should return correct count in default timezone UTC ' +
|
||||
'given `undefined` timezone'
|
||||
)
|
||||
).toEqual(1);
|
||||
}
|
||||
);
|
||||
|
||||
t.equal(
|
||||
it(
|
||||
'should return 2 days when today and yesterday ' +
|
||||
'challenges were completed given PST',
|
||||
() => {
|
||||
expect(
|
||||
calcCurrentStreak(
|
||||
prepUniqueDaysByHours([
|
||||
prepUniqueDaysByHours(
|
||||
[
|
||||
moment.utc(moment.utc().subtract(1, 'days')).valueOf(),
|
||||
moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
|
||||
], PST),
|
||||
],
|
||||
PST
|
||||
),
|
||||
2,
|
||||
'should return 2 days when today and yesterday ' +
|
||||
'challenges were completed given PST'
|
||||
PST
|
||||
)
|
||||
).toEqual(2);
|
||||
}
|
||||
);
|
||||
|
||||
t.equal(
|
||||
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,
|
||||
it(
|
||||
'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(
|
||||
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()
|
||||
1453174506164,
|
||||
1453175436725,
|
||||
1453252466853,
|
||||
1453294968225,
|
||||
1453383782844,
|
||||
1453431903117,
|
||||
1453471373080,
|
||||
1453594733026,
|
||||
1453645014058,
|
||||
1453746762747,
|
||||
1453747659197,
|
||||
1453748029416,
|
||||
1453818029213,
|
||||
1453951796007,
|
||||
1453988570615,
|
||||
1454069704441,
|
||||
1454203673979,
|
||||
1454294055498,
|
||||
1454333545125,
|
||||
1454415163903,
|
||||
1454519128123,
|
||||
moment.utc().valueOf()
|
||||
])
|
||||
),
|
||||
4,
|
||||
'should return 4 when there is a break in UTC ' +
|
||||
'(but would be no break in PST)'
|
||||
)
|
||||
).toEqual(4);
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
test('Longest streak calculation', function(t) {
|
||||
t.plan(14);
|
||||
|
||||
t.equal(
|
||||
describe('calcLongestStreak', function() {
|
||||
it(
|
||||
'should return 1 when there is the only one one-day-long ' +
|
||||
'streak available',
|
||||
() => {
|
||||
expect(
|
||||
calcLongestStreak(
|
||||
prepUniqueDaysByHours([
|
||||
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'
|
||||
)
|
||||
).toEqual(1);
|
||||
}
|
||||
);
|
||||
|
||||
t.equal(
|
||||
it(
|
||||
'should return 4 when there is the only one ' +
|
||||
'more-than-one-days-long streak available',
|
||||
() => {
|
||||
expect(
|
||||
calcLongestStreak(
|
||||
prepUniqueDaysByHours([
|
||||
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/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'
|
||||
)
|
||||
).toEqual(4);
|
||||
}
|
||||
);
|
||||
|
||||
t.equal(
|
||||
it(
|
||||
'should return 1 when there is only one one-day-long streak ' +
|
||||
'and it is today',
|
||||
() => {
|
||||
expect(
|
||||
calcLongestStreak(
|
||||
prepUniqueDaysByHours([
|
||||
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'
|
||||
)
|
||||
).toEqual(1);
|
||||
}
|
||||
);
|
||||
|
||||
t.equal(
|
||||
it('should return 2 when yesterday and today makes longest streak', () => {
|
||||
expect(
|
||||
calcLongestStreak(
|
||||
prepUniqueDaysByHours([
|
||||
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'
|
||||
);
|
||||
)
|
||||
).toEqual(2);
|
||||
});
|
||||
|
||||
t.equal(
|
||||
it('should return 4 when there is a month long break', () => {
|
||||
expect(
|
||||
calcLongestStreak(
|
||||
prepUniqueDaysByHours([
|
||||
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('11/3/2015 2:00', 'M/D/YYYY H:mm').valueOf()
|
||||
])
|
||||
),
|
||||
4,
|
||||
'should return 4 when there is a month long break'
|
||||
);
|
||||
)
|
||||
).toEqual(4);
|
||||
});
|
||||
|
||||
t.equal(
|
||||
it(
|
||||
'should return 2 when there is a more than 1.5 days ' +
|
||||
'long break of (36 hours)',
|
||||
() => {
|
||||
expect(
|
||||
calcLongestStreak(
|
||||
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(),
|
||||
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')
|
||||
.add(37, 'hours')).valueOf(),
|
||||
|
||||
moment
|
||||
.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/15/2015 4:00', 'M/D/YYYY H:mm').valueOf(),
|
||||
moment.utc('10/3/2015 2:00', 'M/D/YYYY H:mm').valueOf()
|
||||
])
|
||||
),
|
||||
2,
|
||||
'should return 2 when there is a more than 1.5 days ' +
|
||||
'long break of (36 hours)'
|
||||
)
|
||||
).toEqual(2);
|
||||
}
|
||||
);
|
||||
|
||||
t.equal(
|
||||
it(
|
||||
'should return 4 when the longest streak consist of ' +
|
||||
'several same day timestamps',
|
||||
() => {
|
||||
expect(
|
||||
calcLongestStreak(
|
||||
prepUniqueDaysByHours([
|
||||
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().valueOf()
|
||||
])
|
||||
),
|
||||
4,
|
||||
'should return 4 when the longest streak consist of ' +
|
||||
'several same day timestamps'
|
||||
)
|
||||
).toEqual(4);
|
||||
}
|
||||
);
|
||||
|
||||
t.equal(
|
||||
it(
|
||||
'should return 4 when there are several longest streaks ' +
|
||||
'(same length)',
|
||||
() => {
|
||||
expect(
|
||||
calcLongestStreak(
|
||||
prepUniqueDaysByHours([
|
||||
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/14/2015 5:00', 'M/D/YYYY H:mm').valueOf()
|
||||
])
|
||||
),
|
||||
4,
|
||||
'should return 4 when there are several longest streaks (same length)'
|
||||
)
|
||||
).toEqual(4);
|
||||
}
|
||||
);
|
||||
|
||||
it(
|
||||
'should return correct longest streak when there is a very ' +
|
||||
'long period',
|
||||
() => {
|
||||
let cals = [];
|
||||
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());
|
||||
}
|
||||
|
||||
t.equal(
|
||||
calcLongestStreak(prepUniqueDaysByHours(cals)),
|
||||
n,
|
||||
'should return correct longest streak when there is a very long period'
|
||||
expect(calcLongestStreak(prepUniqueDaysByHours(cals))).toEqual(n);
|
||||
}
|
||||
);
|
||||
|
||||
t.equal(
|
||||
it(
|
||||
'should return correct longest streak in default timezone ' +
|
||||
'UTC given `undefined` timezone',
|
||||
() => {
|
||||
expect(
|
||||
calcLongestStreak(
|
||||
prepUniqueDaysByHours([
|
||||
moment.utc(moment.utc().subtract(1, 'days')).valueOf(),
|
||||
moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
|
||||
])
|
||||
),
|
||||
2,
|
||||
'should return correct longest streak in default timezone ' +
|
||||
'UTC given `undefined` timezone'
|
||||
)
|
||||
).toEqual(2);
|
||||
}
|
||||
);
|
||||
|
||||
t.equal(
|
||||
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
|
||||
),
|
||||
4,
|
||||
'should return 4 when there is the only one more-than-one-days-long ' +
|
||||
'streak available given PST'
|
||||
]),
|
||||
PST
|
||||
)
|
||||
).toEqual(4);
|
||||
}
|
||||
);
|
||||
|
||||
t.equal(
|
||||
it(
|
||||
'should return 3 when longest streak is 3 in PST ' +
|
||||
'(but would be different in default timezone UTC)',
|
||||
() => {
|
||||
expect(
|
||||
calcLongestStreak(
|
||||
prepUniqueDaysByHours([
|
||||
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
|
||||
),
|
||||
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
|
||||
),
|
||||
17,
|
||||
'should return 17 when there is no break in PST ' +
|
||||
'(but would be break in UTC) and it is current'
|
||||
PST
|
||||
)
|
||||
).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(
|
||||
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()
|
||||
1453174506164,
|
||||
1453175436725,
|
||||
1453252466853,
|
||||
1453294968225,
|
||||
1453383782844,
|
||||
1453431903117,
|
||||
1453471373080,
|
||||
1453594733026,
|
||||
1453645014058,
|
||||
1453746762747,
|
||||
1453747659197,
|
||||
1453748029416,
|
||||
1453818029213,
|
||||
1453951796007,
|
||||
1453988570615,
|
||||
1454069704441,
|
||||
1454203673979,
|
||||
1454294055498,
|
||||
1454333545125,
|
||||
1454415163903,
|
||||
1454519128123,
|
||||
moment.utc().valueOf()
|
||||
])
|
||||
),
|
||||
4,
|
||||
'should return 4 when there is a break in UTC (but no break in PST)'
|
||||
)
|
||||
).toEqual(4);
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
test.onFinish(() => {
|
||||
clock.restore();
|
||||
});
|
||||
|
Reference in New Issue
Block a user