Skip to content

Commit

Permalink
Merge pull request #17 from slategroup/add-bus-events
Browse files Browse the repository at this point in the history
Publishes schedulePage and unschedulePage to event bus
  • Loading branch information
jpope19 authored Feb 25, 2020
2 parents 42be254 + e548cc0 commit 61d58c8
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 5 deletions.
7 changes: 7 additions & 0 deletions lib/services/bus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

function setPublish(publish) {
module.exports.publish = publish;
}

module.exports.setPublish = setPublish;
15 changes: 15 additions & 0 deletions lib/services/bus.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

const bus = require('./bus');

describe('services/bus', () => {
const fn = () => 'a setter';

describe('setPublish', () => {
test('if setPublish is a setter', () => {
bus.setPublish(fn);
expect(bus.publish).toBe(fn);
});
});

});
5 changes: 4 additions & 1 deletion lib/services/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

const db = require('./db'),
routes = require('./routes'),
bus = require('./bus'),
schedule = require('./schedule');

/**
* Initializes plugin.
* @param {Object} router
* @param {Object} storage
* @param {Function} publish to the event bus
* @return {Promise}
*/
function onInit(router, storage) {
function onInit(router, storage, publish) {
bus.setPublish(publish);
return db(storage)
.then(() => routes(router))
.then(() => {
Expand Down
3 changes: 3 additions & 0 deletions lib/services/init.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

const init = require('./init'),
db = require('./db'),
bus = require('./bus'),
routes = require('./routes'),
schedule = require('./schedule'),
router = {},
storage = {};

jest.mock('./db');
jest.mock('./bus');
jest.mock('./routes');
jest.mock('./schedule');

Expand All @@ -32,6 +34,7 @@ describe('init', () => {

return init(router, storage).then(() => {
expect(db.mock.calls.length).toBe(1);
expect(bus.setPublish.mock.calls.length).toBe(1);
expect(db.mock.calls[0][0]).toEqual(storage);
expect(routes.mock.calls.length).toBe(1);
expect(routes.mock.calls[0][0]).toEqual(router);
Expand Down
13 changes: 11 additions & 2 deletions lib/services/schedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const _ = require('lodash'),
db = require('./db'),
references = require('./references'),
buf = require('./buffer'),
bus = require('./bus'),
rest = require('./rest'),
publishProperty = 'publish',
scheduledAtProperty = 'at',
Expand Down Expand Up @@ -41,7 +42,7 @@ function publishExternally(url) {
log('error', `failed to publish url from schedule: [${res.status}] ${res.statusText}`, { url, status: res.status });
}

return db.db.patchMeta(uri, { scheduled: false, scheduledTime: null }).then((meta) => res.json());
return db.db.patchMeta(uri, { scheduled: false, scheduledTime: null }).then(() => res.json());
});
});
}
Expand Down Expand Up @@ -105,6 +106,7 @@ function unschedule(uri, user) {
return del(uri)
.then((data) => {
const publish = data[publishProperty];

pageUri = publish.replace(/http:\/\/|https:\/\//g, '');

return db.db.getMeta(pageUri);
Expand All @@ -120,7 +122,11 @@ function unschedule(uri, user) {
});

return db.db.patchMeta(pageUri, meta);
}).catch((e) => {
})
.then(() => {
return bus.publish('unschedulePage', { uri: pageUri, user });
})
.catch((e) => {
errorLogger(e);

return {};
Expand Down Expand Up @@ -154,6 +160,9 @@ function post(uri, data, user) {

return db.db.patchMeta(pageUri, meta);
})
.then(() => {
return bus.publish('schedulePage', { uri: pageUri, data, user });
})
.then(() => data)
.catch(errorLogger);
}
Expand Down
8 changes: 6 additions & 2 deletions lib/services/schedule.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ const {
} = require('./schedule'),
rest = require('./rest'),
db = require('./db'),
bus = require('./bus'),
logger = jest.fn();

jest.mock('./rest');
jest.mock('./db');
setLog(logger);
bus.publish = jest.fn( () => Promise.resolve({}) );

describe('services/schedule', () => {
describe('createScheduleObjectKey', () => {
Expand Down Expand Up @@ -189,7 +191,7 @@ describe('services/schedule', () => {
db.deleteItem.mockResolvedValue(Promise.resolve({}));

return unschedule(uri, user).then(() => {
expect(db.deleteItem.mock.calls.length).toBe(1)
expect(db.deleteItem.mock.calls.length).toBe(1);
});
});

Expand All @@ -215,8 +217,9 @@ describe('services/schedule', () => {
expect(db.db.getMeta.mock.calls[0][0]).toBe('some.com/url');
expect(db.db.patchMeta.mock.calls.length).toBe(1);
expect(db.db.patchMeta.mock.calls[0][1].scheduled).toBe(false);
expect(bus.publish.mock.calls.length).toBe(1);
});
})
});
});

describe('post', () => {
Expand All @@ -232,6 +235,7 @@ describe('services/schedule', () => {

return post(uri, data, {}).then(() => {
expect(db.insertItem.mock.calls.length).toBe(1);
expect(bus.publish.mock.calls.length).toBe(1);
expect(db.insertItem.mock.calls[0][0]).toBe(newRef);
expect(db.insertItem.mock.calls[0][1]).toBe(data);
});
Expand Down

0 comments on commit 61d58c8

Please sign in to comment.