Skip to content

Commit

Permalink
fix: using both skipHours/Days and emitOnStart emits items on fir…
Browse files Browse the repository at this point in the history
…st request
  • Loading branch information
fent committed Sep 8, 2018
1 parent 3f222c8 commit 20cc92e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 18 deletions.
3 changes: 2 additions & 1 deletion lib/feedsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ module.exports = class FeedReader extends EventEmitter {

// If skipHours or skipDays are enabled and feed provides hours/days
// to skip and it's one of those hours/days, abort.
if (this.options.hoursToSkip || this.options.daysToSkip) {
if ((!this.first || !this.options.emitOnStart) &&
(this.options.hoursToSkip || this.options.daysToSkip)) {
let now = new Date();
if ((this.options.hoursToSkip &&
this.options.hoursToSkip.indexOf(now.getHours()) !== -1) ||
Expand Down
60 changes: 43 additions & 17 deletions test/skip-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,51 @@ const path = require('path');
describe('Use skipHours', () => {
const feed = path.join(__dirname, 'assets', 'skiphours.rss');
describe('With hours that match time now', () => {
it('Should return no items', (done) => {
muk(Date.prototype, 'getHours', () => 4);
after(muk.restore);
const host = 'http://www.google.com';
const path = '/reader/public/atom/';
const reader = new FeedSub(host + path, {
emitOnStart: true, skipHours: true
describe('With `emitOnStart`', () => {
it('Should return some items', (done) => {
muk(Date.prototype, 'getHours', () => 4);
after(muk.restore);
const host = 'http://www.google.com';
const path = '/reader/public/atom/';
const reader = new FeedSub(host + path, {
emitOnStart: true, skipHours: true
});

const scope = nock(host)
.get(path)
.replyWithFile(200, feed);

reader.read((err, items) => {
assert.ifError(err);
assert.ok(Array.isArray(items));
assert.equal(items.length, 20);
scope.done();
done();
});
});
});

const scope = nock(host)
.get(path)
.replyWithFile(200, feed);
describe('Without `emitOnStart`', () => {
it('Should return no items', (done) => {
muk(Date.prototype, 'getHours', () => 4);
after(muk.restore);
const host = 'http://www.google.com';
const path = '/reader/public/atom/';
const reader = new FeedSub(host + path, {
emitOnStart: false, skipHours: true
});

reader.read((err, items) => {
assert.ifError(err);
assert.ok(Array.isArray(items));
assert.equal(items.length, 0);
scope.done();
done();
const scope = nock(host)
.get(path)
.replyWithFile(200, feed);

reader.read((err, items) => {
assert.ifError(err);
assert.ok(Array.isArray(items));
assert.equal(items.length, 0);
scope.done();
done();
});
});
});
});
Expand Down Expand Up @@ -65,7 +91,7 @@ describe('Use skipDays', () => {
const host = 'http://blog.nodejs.org';
const path = '/feed/';
const reader = new FeedSub(host + path, {
emitOnStart: true, skipDays: true
emitOnStart: false, skipDays: true
});

const scope = nock(host)
Expand Down

0 comments on commit 20cc92e

Please sign in to comment.