Ghost/ghost/member-attribution/test/history.test.js
Simon Backx 972c25edc7
Wired up member attribution from email clicks (#15407)
refs https://github.com/TryGhost/Team/issues/1899

- Added `addEmailAttributionToUrl` method to MemberAttributionService. This adds both the source attribution (`rel=newsletter`) and member attribution (`?attribution_id=123&attribution_type=post`) to a URL.
- The URLHistory can now contain a new sort of items: `{type: 'post', id: 'post-id', time: 123}`.
- Updated frontend script to read `?attribution_id=123&attribution_type=post` from the URL and add it to the URLHistory + clear it from the URL.
- Wired up some external dependencies to LinkReplacementService and added some dummy code.
- Increased test coverage of attribution service
- Moved all logic that removes the subdirectory from a URL to the UrlTranslator instead of the AttributionBuilder
- The UrlTranslator now parses a URLHistoryItem to an object that can be used to build an Attribution instance
- Excluded sites with different domain from member id and attribution tracking
2022-09-14 15:50:54 -04:00

104 lines
2.5 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'
}]
];
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]]);
});
});