Ghost/ghost/member-attribution/test/history.test.js
Rishabh c765c3230e Updated attribution service to handle referrer information
refs TryGhost/Team#1907

- calculates final attribution source and medium using captured referrer information in history
- adds new referrer-translator that goes through available history and based to determine most valid referrer info
- includes referrer url, source and medium in the attribution data for storage
2022-09-19 12:29:22 +05:30

112 lines
2.7 KiB
JavaScript

// Switch these lines once there are useful utils
// const testUtils = require('./utils');
require('./utils');
const UrlHistory = require('../lib/history');
describe('UrlHistory', function () {
it('sets history to empty array if invalid', function () {
const inputs = [
'string',
undefined,
12,
null,
{},
NaN,
[
{
time: 1,
path: '/test'
},
't'
],
[{}],
['test'],
[0],
[undefined],
[NaN],
[[]],
[{
time: 'test',
path: 'test'
}],
[{
path: 'test'
}],
[{
time: 123
}],
[{
time: 123,
type: 'post'
}],
[{
time: 123,
id: 'id'
}],
[{
time: 123,
type: 123,
id: 'test'
}],
[{
time: 123,
type: 'invalid',
id: 'test'
}],
[{
time: 123,
type: 'post',
id: 123
}]
];
for (const input of inputs) {
const history = UrlHistory.create(input);
should(history.history).eql([]);
}
});
it('sets history for valid arrays', function () {
const inputs = [
[],
[{
time: Date.now(),
path: '/test'
}],
[{
time: Date.now(),
type: 'post',
id: '123'
}],
[{
time: Date.now(),
type: 'post',
id: '123',
refSource: 'ghost-explore',
refMedium: null,
refUrl: 'https://ghost.org'
}]
];
for (const input of inputs) {
const history = UrlHistory.create(input);
should(history.history).eql(input);
}
});
it('removes entries older than 24 hours', function () {
const input = [{
time: Date.now() - 1000 * 60 * 60 * 25,
path: '/old'
}, {
time: Date.now() - 1000 * 60 * 60 * 23,
path: '/not-old'
}, {
time: Date.now() - 1000 * 60 * 60 * 25,
type: 'post',
id: 'old'
}];
const history = UrlHistory.create(input);
should(history.history).eql([input[1]]);
});
});