Ghost/ghost/member-attribution/test/attribution.test.js
Simon Backx e986b78458
Added member attributions to activity feed (#15283)
refs https://github.com/TryGhost/Team/issues/1833
refs https://github.com/TryGhost/Team/issues/1834

We've added the attribution property to subscription and signup events when the
flag is enabled. The attributions resource is fetched by creating multiple relations
on the model, rather than polymorphic as we ran into issues with that as they can't
be nullable/optional.

The parse-member-event structure has been updated to make it easier to work with,
specifically `getObject` is only used when the event is clickable, and there is now a 
join property which makes it easier to join the action and the object.
2022-08-24 10:11:25 -04:00

137 lines
4.7 KiB
JavaScript

// Switch these lines once there are useful utils
// const testUtils = require('./utils');
require('./utils');
const UrlHistory = require('../lib/history');
const AttributionBuilder = require('../lib/attribution');
describe('AttributionBuilder', function () {
let attributionBuilder;
before(function () {
attributionBuilder = new AttributionBuilder({
urlTranslator: {
getTypeAndId(path) {
if (path === '/my-post') {
return {
id: 123,
type: 'post'
};
}
if (path === '/my-page') {
return {
id: 845,
type: 'page'
};
}
return;
},
getResourceById(id) {
if (id === 'invalid') {
return null;
}
return {
id,
get() {
return 'Title';
}
};
},
getUrlByResourceId() {
return 'https://absolute/dir/path';
},
relativeToAbsolute(path) {
return 'https://absolute/dir' + path;
},
stripSubdirectoryFromPath(path) {
if (path.startsWith('/dir/')) {
return path.substring('/dir/'.length - 1);
}
return path;
}
}
});
});
it('Returns empty if empty history', function () {
const history = new UrlHistory([]);
should(attributionBuilder.getAttribution(history)).match({id: null, type: null, url: null});
});
it('Returns last url', function () {
const history = new UrlHistory([{path: '/dir/not-last', time: 123}, {path: '/dir/test/', time: 123}]);
should(attributionBuilder.getAttribution(history)).match({type: 'url', id: null, url: '/test/'});
});
it('Returns last post', function () {
const history = new UrlHistory([
{path: '/dir/my-post', time: 123},
{path: '/dir/test', time: 124},
{path: '/dir/unknown-page', time: 125}
]);
should(attributionBuilder.getAttribution(history)).match({type: 'post', id: 123, url: '/my-post'});
});
it('Returns last post even when it found pages', function () {
const history = new UrlHistory([
{path: '/dir/my-post', time: 123},
{path: '/dir/my-page', time: 124},
{path: '/dir/unknown-page', time: 125}
]);
should(attributionBuilder.getAttribution(history)).match({type: 'post', id: 123, url: '/my-post'});
});
it('Returns last page if no posts', function () {
const history = new UrlHistory([
{path: '/dir/other', time: 123},
{path: '/dir/my-page', time: 124},
{path: '/dir/unknown-page', time: 125}
]);
should(attributionBuilder.getAttribution(history)).match({type: 'page', id: 845, url: '/my-page'});
});
it('Returns all null for invalid histories', function () {
const history = new UrlHistory('invalid');
should(attributionBuilder.getAttribution(history)).match({
type: null,
id: null,
url: null
});
});
it('Returns all null for empty histories', function () {
const history = new UrlHistory([]);
should(attributionBuilder.getAttribution(history)).match({
type: null,
id: null,
url: null
});
});
it('Returns post resource', async function () {
should(await attributionBuilder.build({type: 'post', id: '123', url: '/post'}).fetchResource()).match({
type: 'post',
id: '123',
url: 'https://absolute/dir/path',
title: 'Title'
});
});
it('Returns url resource', async function () {
should(await attributionBuilder.build({type: 'url', id: null, url: '/url'}).fetchResource()).match({
type: 'url',
id: null,
url: 'https://absolute/dir/url',
title: '/url'
});
});
it('Returns url resource if not found', async function () {
should(await attributionBuilder.build({type: 'post', id: 'invalid', url: '/post'}).fetchResource()).match({
type: 'url',
id: null,
url: 'https://absolute/dir/post',
title: '/post'
});
});
});