2021-03-02 00:31:07 +03:00
|
|
|
// Switch these lines once there are useful utils
|
|
|
|
// const testUtils = require('./utils');
|
|
|
|
require('./utils');
|
|
|
|
|
|
|
|
const sinon = require('sinon');
|
|
|
|
|
|
|
|
const {
|
2022-11-29 13:15:19 +03:00
|
|
|
EmailAnalyticsService
|
2021-03-02 00:31:07 +03:00
|
|
|
} = require('..');
|
2023-05-02 23:43:47 +03:00
|
|
|
const EventProcessingResult = require('../lib/EventProcessingResult');
|
2021-03-02 00:31:07 +03:00
|
|
|
|
|
|
|
describe('EmailAnalyticsService', function () {
|
2022-11-29 13:15:19 +03:00
|
|
|
let eventProcessor;
|
|
|
|
beforeEach(function () {
|
|
|
|
eventProcessor = {};
|
|
|
|
eventProcessor.handleDelivered = sinon.stub().callsFake(({emailId}) => {
|
|
|
|
return {
|
|
|
|
emailId,
|
|
|
|
emailRecipientId: emailId,
|
|
|
|
memberId: 1
|
|
|
|
};
|
|
|
|
});
|
|
|
|
eventProcessor.handleOpened = sinon.stub().callsFake(({emailId}) => {
|
|
|
|
return {
|
|
|
|
emailId,
|
|
|
|
emailRecipientId: emailId,
|
|
|
|
memberId: 1
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-03-02 00:31:07 +03:00
|
|
|
describe('fetchLatest', function () {
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('processEventBatch', function () {
|
|
|
|
it('uses passed-in event processor', async function () {
|
|
|
|
const service = new EmailAnalyticsService({
|
|
|
|
eventProcessor
|
|
|
|
});
|
|
|
|
|
2023-02-20 18:44:13 +03:00
|
|
|
const result = new EventProcessingResult();
|
|
|
|
const fetchData = {
|
|
|
|
|
|
|
|
};
|
|
|
|
await service.processEventBatch([{
|
2021-03-02 00:31:07 +03:00
|
|
|
type: 'delivered',
|
2023-02-20 18:44:13 +03:00
|
|
|
emailId: 1,
|
|
|
|
timestamp: new Date(1)
|
2021-03-02 00:31:07 +03:00
|
|
|
}, {
|
|
|
|
type: 'delivered',
|
2023-02-20 18:44:13 +03:00
|
|
|
emailId: 2,
|
|
|
|
timestamp: new Date(2)
|
2021-03-02 00:31:07 +03:00
|
|
|
}, {
|
|
|
|
type: 'opened',
|
2023-02-20 18:44:13 +03:00
|
|
|
emailId: 1,
|
|
|
|
timestamp: new Date(3)
|
|
|
|
}], result, fetchData);
|
2021-03-02 00:31:07 +03:00
|
|
|
|
|
|
|
eventProcessor.handleDelivered.callCount.should.eql(2);
|
|
|
|
|
|
|
|
result.should.deepEqual(new EventProcessingResult({
|
|
|
|
delivered: 2,
|
2022-11-29 13:15:19 +03:00
|
|
|
opened: 1,
|
|
|
|
unprocessable: 0,
|
|
|
|
emailIds: [1, 2],
|
|
|
|
memberIds: [1]
|
2021-03-02 00:31:07 +03:00
|
|
|
}));
|
2023-02-20 18:44:13 +03:00
|
|
|
|
|
|
|
fetchData.should.deepEqual({
|
|
|
|
lastEventTimestamp: new Date(3)
|
|
|
|
});
|
2021-03-02 00:31:07 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('aggregateStats', function () {
|
|
|
|
let service;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
service = new EmailAnalyticsService({
|
|
|
|
queries: {
|
|
|
|
aggregateEmailStats: sinon.spy(),
|
|
|
|
aggregateMemberStats: sinon.spy()
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('calls appropriate query for each email id and member id', async function () {
|
|
|
|
await service.aggregateStats({
|
|
|
|
emailIds: ['e-1', 'e-2'],
|
|
|
|
memberIds: ['m-1', 'm-2']
|
|
|
|
});
|
|
|
|
|
|
|
|
service.queries.aggregateEmailStats.calledTwice.should.be.true();
|
|
|
|
service.queries.aggregateEmailStats.calledWith('e-1').should.be.true();
|
|
|
|
service.queries.aggregateEmailStats.calledWith('e-2').should.be.true();
|
|
|
|
|
|
|
|
service.queries.aggregateMemberStats.calledTwice.should.be.true();
|
|
|
|
service.queries.aggregateMemberStats.calledWith('m-1').should.be.true();
|
|
|
|
service.queries.aggregateMemberStats.calledWith('m-2').should.be.true();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|