feedReaderTest.js (6665B)
1 "use strict"; 2 3 describe("Zotero.FeedReader", function () { 4 5 var htmlUrl = getTestDataUrl("test.html"); 6 7 var rssFeedURL = getTestDataUrl("feed.rss"); 8 var rssFeedInfo = { 9 title: 'Liftoff News', 10 subtitle: 'Liftoff to Space Exploration.', 11 updated: new Date("Tue, 10 Jun 2003 09:41:01 GMT"), 12 creators: [{ 13 firstName: '', 14 lastName: 'editor@example.com', 15 creatorType: 'author', 16 fieldMode: 1 17 }], 18 language: 'en-us' 19 }; 20 21 var detailedRSSFeedURL = getTestDataUrl("feedDetailed.rss"); 22 var detailedRSSFeedInfo = { 23 title: 'Feed', 24 subtitle: 'Feed Description', 25 creators: [{firstName: 'Feed', lastName: 'Author', creatorType: 'author'}], 26 publicationTitle: 'Publication', 27 publisher: 'Publisher', 28 rights: '©2016 Published by Publisher', 29 ISSN: '0000-0000', 30 language: 'en' 31 }; 32 33 var atomFeedURL = getTestDataUrl("feed.atom"); 34 var atomFeedInfo = { 35 title: 'Incircular nets and confocal conics', 36 updated: new Date("Tue, 10 Jun 2003 09:41:01 GMT"), 37 creators: [{ 38 firstName: '', 39 lastName: 'editor@example.com', 40 creatorType: 'author', 41 fieldMode: 1 42 }], 43 language: 'en-us' 44 }; 45 46 after(function* () { 47 yield clearFeeds(); 48 }); 49 50 describe('FeedReader()', function () { 51 it('should throw if url not provided', function() { 52 assert.throw(() => new Zotero.FeedReader()) 53 }); 54 55 it('should throw if url invalid', function() { 56 assert.throw(() => new Zotero.FeedReader('invalid url')) 57 }); 58 }); 59 60 describe('#process()', function() { 61 it('should reject if the provided url is not a valid feed', function* () { 62 let fr = new Zotero.FeedReader(htmlUrl); 63 let e = yield getPromiseError(fr.process()); 64 assert.ok(e); 65 e = yield getPromiseError(fr._feedItems[fr._feedItems.length-1].promise); 66 assert.ok(e); 67 }); 68 69 it('should set #feedProperties on FeedReader object', function* () { 70 let fr = new Zotero.FeedReader(rssFeedURL); 71 assert.throw(() => fr.feedProperties); 72 yield fr.process(); 73 assert.ok(fr.feedProperties); 74 }); 75 }); 76 77 describe('#terminate()', function() { 78 it('should reject last feed item and feed processing promise if feed not processed yet', function* () { 79 let fr = new Zotero.FeedReader(rssFeedURL); 80 fr.terminate("test"); 81 let e = yield getPromiseError(fr.process()); 82 assert.ok(e); 83 e = yield getPromiseError(fr._feedItems[fr._feedItems.length-1].promise); 84 assert.ok(e); 85 }); 86 87 it('should reject last feed item if feed processed', function* () { 88 let fr = new Zotero.FeedReader(rssFeedURL); 89 yield fr.process(); 90 fr.terminate("test"); 91 let e = yield getPromiseError(fr._feedItems[fr._feedItems.length-1].promise); 92 assert.ok(e); 93 }); 94 }); 95 96 describe('#feedProperties', function() { 97 it('should throw if accessed before feed is processed', function () { 98 let fr = new Zotero.FeedReader(rssFeedURL); 99 assert.throw(() => fr.feedProperties); 100 }); 101 102 it('should have correct values for a sparse feed', function* () { 103 let fr = new Zotero.FeedReader(rssFeedURL); 104 yield fr.process(); 105 assert.deepEqual(fr.feedProperties, rssFeedInfo); 106 }); 107 108 it('should have correct values for a detailed feed', function* () { 109 let fr = new Zotero.FeedReader(detailedRSSFeedURL); 110 yield fr.process(); 111 assert.deepEqual(fr.feedProperties, detailedRSSFeedInfo); 112 }); 113 }); 114 115 describe('#ItemIterator()', function() { 116 it('should throw if called before feed is resolved', function() { 117 let fr = new Zotero.FeedReader(rssFeedURL); 118 assert.throw(() => new fr.ItemIterator); 119 }); 120 121 it('should parse items correctly for a sparse RSS feed', function* () { 122 let expected = { guid: 'http://liftoff.msfc.nasa.gov/2003/06/03.html#item573', 123 title: 'Star City', 124 abstractNote: 'How do Americans get ready to work with Russians aboard the International Space Station? They take a crash course in culture, language and protocol at Russia\'s Star City.', 125 url: 'http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp', 126 creators: [{ firstName: '', lastName: 'editor@example.com', creatorType: 'author', fieldMode: 1 }], 127 date: 'Tue, 03 Jun 2003 09:39:21 GMT', 128 language: 'en-us', 129 itemType: 'journalArticle', 130 enclosedItems: [{ url: 'http://www.example.com/example.pdf', contentType: 'application/pdf' }] 131 }; 132 133 let fr = new Zotero.FeedReader(rssFeedURL); 134 yield fr.process(); 135 let itemIterator = new fr.ItemIterator(); 136 let item = yield itemIterator.next().value; 137 assert.deepEqual(item, expected); 138 }); 139 140 it('should parse items correctly for a detailed RSS feed', function* () { 141 let expected = { 142 guid: 'http://www.example.com/item1', 143 title: 'Title 1', 144 abstractNote: 'Description 1', 145 url: 'http://www.example.com/item1', 146 creators: [ 147 { firstName: 'Author1 A. T.', lastName: 'Rohtua', creatorType: 'author' }, 148 { firstName: 'Author2 A.', lastName: 'Auth', creatorType: 'author' }, 149 { firstName: 'Author3', lastName: 'Autho', creatorType: 'author' }, 150 { firstName: 'Contributor1 A. T.', lastName: 'Rotubirtnoc', creatorType: 'contributor' }, 151 { firstName: 'Contributor2 C.', lastName: 'Contrib', creatorType: 'contributor' }, 152 { firstName: 'Contributor3', lastName: 'Contr', creatorType: 'contributor' } 153 ], 154 date: '2016-01-07', 155 publicationTitle: 'Publication', 156 ISSN: '0000-0000', 157 publisher: 'Publisher', 158 rights: '©2016 Published by Publisher', 159 language: 'en', 160 itemType: 'journalArticle', 161 enclosedItems: [] 162 }; 163 164 let fr = new Zotero.FeedReader(detailedRSSFeedURL); 165 yield fr.process(); 166 let itemIterator = new fr.ItemIterator(); 167 let item = yield itemIterator.next().value; 168 assert.deepEqual(item, expected); 169 }); 170 171 it("should parse item from an Atom feed", function* () { 172 let expected = { 173 guid: 'http://www.example.com/item1', 174 title: 'Title 1', 175 abstractNote: 'Abstract 1', 176 url: 'http://www.example.com/item1', 177 creators: [ 178 { firstName: 'Author1 A. T.', lastName: 'Rohtua', creatorType: 'author' }, 179 { firstName: 'Author2 A.', lastName: 'Auth', creatorType: 'author' } 180 ], 181 // TODO: DOI? 182 date: '2017-10-27T12:27:09Z', 183 itemType: 'journalArticle', 184 enclosedItems: [] 185 }; 186 187 let fr = new Zotero.FeedReader(atomFeedURL); 188 yield fr.process(); 189 let itemIterator = new fr.ItemIterator(); 190 let item = yield itemIterator.next().value; 191 192 assert.deepEqual(item, expected); 193 }); 194 195 it('should resolve last item with null', function* () { 196 let fr = new Zotero.FeedReader(rssFeedURL); 197 yield fr.process(); 198 let itemIterator = new fr.ItemIterator(); 199 let item; 200 while(item = yield itemIterator.next().value); 201 assert.isNull(item); 202 }); 203 }); 204 })