feedsTest.js (7190B)
1 describe("Zotero.Feeds", function () { 2 3 after(function* () { 4 yield clearFeeds(); 5 }); 6 7 describe('#importFromOPML', function() { 8 var opmlUrl = getTestDataUrl("feeds.opml"); 9 var opmlString; 10 11 before(function* (){ 12 opmlString = yield Zotero.File.getContentsFromURLAsync(opmlUrl); 13 sinon.stub(Zotero.Feeds, 'updateFeeds').resolves(); 14 }); 15 16 beforeEach(function* () { 17 yield clearFeeds(); 18 }); 19 20 after(function() { 21 Zotero.Feeds.updateFeeds.restore(); 22 }); 23 24 it('imports feeds correctly', function* (){ 25 let shouldExist = { 26 "http://example.com/feed1.rss": "A title 1", 27 "http://example.com/feed2.rss": "A title 2", 28 "http://example.com/feed3.rss": "A title 3", 29 "http://example.com/feed4.rss": "A title 4" 30 }; 31 yield Zotero.Feeds.importFromOPML(opmlString); 32 let feeds = Zotero.Feeds.getAll(); 33 for (let feed of feeds) { 34 assert.equal(shouldExist[feed.url], feed.name, "Feed exists and title matches"); 35 delete shouldExist[feed.url]; 36 } 37 assert.equal(Object.keys(shouldExist).length, 0, "All feeds from opml have been created"); 38 }); 39 40 it("doesn't fail if some feeds already exist", function* (){ 41 yield createFeed({url: "http://example.com/feed1.rss"}); 42 yield Zotero.Feeds.importFromOPML(opmlString) 43 }); 44 }); 45 46 describe("#restoreFromJSON", function() { 47 var json, expiredFeedURL, existingFeedURL; 48 49 beforeEach(function* () { 50 yield clearFeeds(); 51 52 json = {}; 53 for (let i = 0; i < 2; i++) { 54 let url = "http://" + Zotero.Utilities.randomString(10, 'abcdefgh') + ".com/feed.rss"; 55 json[url] = { 56 url, 57 name: Zotero.Utilities.randomString(), 58 refreshInterval: 5, 59 cleanupReadAfter: 3, 60 cleanupUnreadAfter: 30, 61 }; 62 if (i == 0) { 63 existingFeedURL = url; 64 yield createFeed({url}); 65 } 66 } 67 expiredFeedURL = (yield createFeed()).url; 68 }); 69 70 it("restores correctly when merge is true", function* () { 71 let feeds = Zotero.Feeds.getAll(); 72 assert.equal(feeds.length, 2); 73 74 yield Zotero.Feeds.restoreFromJSON(json, true); 75 feeds = Zotero.Feeds.getAll(); 76 77 for (let url in json) { 78 let feed = Zotero.Feeds.getByURL(url); 79 assert.ok(feed, "new feed created"); 80 } 81 82 let expiredFeed = Zotero.Feeds.getByURL(expiredFeedURL); 83 assert.ok(expiredFeed, "does not remove feeds not in JSON"); 84 85 let existingFeed = Zotero.Feeds.getByURL(existingFeedURL); 86 assert.ok(existingFeed, "does not remove feeds in database and JSON"); 87 }); 88 89 it("restores correctly when merge is false", function* () { 90 let feeds = Zotero.Feeds.getAll(); 91 assert.equal(feeds.length, 2); 92 93 yield Zotero.Feeds.restoreFromJSON(json); 94 feeds = Zotero.Feeds.getAll(); 95 96 for (let url in json) { 97 let feed = Zotero.Feeds.getByURL(url); 98 assert.ok(feed, "new feed created"); 99 } 100 101 let expiredFeed = Zotero.Feeds.getByURL(expiredFeedURL); 102 assert.notOk(expiredFeed, "removes feeds not in JSON"); 103 104 let existingFeed = Zotero.Feeds.getByURL(existingFeedURL); 105 assert.ok(existingFeed, "does not remove feeds in database and JSON"); 106 }); 107 }); 108 109 describe("#haveFeeds()", function() { 110 it("should return false for a DB without feeds", function* () { 111 yield clearFeeds(); 112 assert.isFalse(Zotero.Feeds.haveFeeds(), 'no feeds in empty DB'); 113 114 let group = yield createGroup(); 115 116 assert.isFalse(Zotero.Feeds.haveFeeds(), 'no feeds in DB with groups'); 117 }); 118 it("should return true for a DB containing feeds", function* () { 119 let feed = yield createFeed(); 120 121 assert.isTrue(Zotero.Feeds.haveFeeds()); 122 }); 123 }); 124 describe("#getAll()", function() { 125 it("should return an empty array for a DB without feeds", function* () { 126 yield clearFeeds(); 127 let feeds = Zotero.Feeds.getAll(); 128 assert.lengthOf(feeds, 0, 'no feeds in an empty DB'); 129 130 let group = yield createGroup(); 131 132 feeds = Zotero.Feeds.getAll(); 133 assert.lengthOf(feeds, 0, 'no feeds in DB with group libraries'); 134 }); 135 it("should return an array of feeds", function* () { 136 yield clearFeeds(); 137 let feed1 = yield createFeed(); 138 let feed2 = yield createFeed(); 139 140 let feeds = Zotero.Feeds.getAll(); 141 assert.lengthOf(feeds, 2); 142 assert.sameMembers(feeds, [feed1, feed2]); 143 }); 144 }); 145 146 describe('#getByURL', function() { 147 it("should return a feed by url", function* () { 148 let url = 'http://' + Zotero.Utilities.randomString(10, 'abcdefg') + '.com/feed.rss'; 149 yield createFeed({url}); 150 let feed = Zotero.Feeds.getByURL(url); 151 assert.ok(feed); 152 assert.equal(feed.url, url); 153 }); 154 it("should return undefined if feed does not exist", function* () { 155 var feed; 156 assert.doesNotThrow(function() { 157 feed = Zotero.Feeds.getByURL('doesnotexist'); 158 }); 159 assert.isUndefined(feed); 160 }); 161 }); 162 describe('#updateFeeds', function() { 163 var freshFeed, recentFeed, oldFeed; 164 var _updateFeed; 165 166 before(function* () { 167 yield clearFeeds(); 168 169 sinon.stub(Zotero.Feeds, 'scheduleNextFeedCheck').resolves(); 170 _updateFeed = sinon.stub(Zotero.Feed.prototype, '_updateFeed').resolves(); 171 let url = getTestDataUrl("feed.rss"); 172 173 freshFeed = yield createFeed({refreshInterval: 2}); 174 freshFeed._feedUrl = url; 175 freshFeed.lastCheck = null; 176 yield freshFeed.saveTx(); 177 178 recentFeed = yield createFeed({refreshInterval: 2}); 179 recentFeed._feedUrl = url; 180 recentFeed.lastCheck = Zotero.Date.dateToSQL(new Date(), true); 181 yield recentFeed.saveTx(); 182 183 oldFeed = yield createFeed({refreshInterval: 2}); 184 oldFeed._feedUrl = url; 185 oldFeed.lastCheck = Zotero.Date.dateToSQL(new Date(Date.now() - 1000*60*60*6), true); 186 yield oldFeed.saveTx(); 187 188 yield Zotero.Feeds.updateFeeds(); 189 assert.isTrue(_updateFeed.called); 190 }); 191 192 after(function() { 193 Zotero.Feeds.scheduleNextFeedCheck.restore(); 194 _updateFeed.restore(); 195 }); 196 197 it('should update feeds that have never been updated', function() { 198 for (var feed of _updateFeed.thisValues) { 199 if (feed.id == freshFeed.id) { 200 break; 201 } 202 } 203 assert.isTrue(feed._updateFeed.called); 204 }); 205 it('should update feeds that need updating since last check', function() { 206 for (var feed of _updateFeed.thisValues) { 207 if (feed.id == oldFeed.id) { 208 break; 209 } 210 } 211 assert.isTrue(feed._updateFeed.called); 212 }); 213 it("should not update feeds that don't need updating", function() { 214 for (var feed of _updateFeed.thisValues) { 215 if (feed.id != recentFeed.id) { 216 break; 217 } 218 // should never reach 219 assert.ok(null, "does not update feed that did not need updating") 220 } 221 }); 222 }); 223 describe('#scheduleNextFeedCheck()', function() { 224 it('schedules next feed check', function* () { 225 sinon.spy(Zotero.Feeds, 'scheduleNextFeedCheck'); 226 sinon.spy(Zotero.Promise, 'delay'); 227 228 yield clearFeeds(); 229 let feed = yield createFeed({refreshInterval: 1}); 230 feed._set('_feedLastCheck', Zotero.Date.dateToSQL(new Date(), true)); 231 yield feed.saveTx(); 232 233 yield Zotero.Feeds.scheduleNextFeedCheck(); 234 235 // Allow a propagation delay of 5000ms 236 assert.isTrue(Zotero.Promise.delay.args[0][0] - 1000*60*60 <= 5000); 237 238 Zotero.Feeds.scheduleNextFeedCheck.restore(); 239 Zotero.Promise.delay.restore(); 240 }); 241 }) 242 })