2016-01-12 13:28:15 +00:00
"use strict" ;
describe ( "Zotero.FeedReader" , function ( ) {
2016-02-11 11:02:38 +00:00
var htmlUrl = getTestDataUrl ( "test.html" ) ;
2016-01-12 13:28:15 +00:00
2017-10-31 06:14:15 +00:00
var rssFeedURL = getTestDataUrl ( "feed.rss" ) ;
var rssFeedInfo = {
2016-01-12 13:28:15 +00:00
title : 'Liftoff News' ,
subtitle : 'Liftoff to Space Exploration.' ,
updated : new Date ( "Tue, 10 Jun 2003 09:41:01 GMT" ) ,
creators : [ {
firstName : '' ,
lastName : 'editor@example.com' ,
creatorType : 'author' ,
fieldMode : 1
} ] ,
language : 'en-us'
} ;
2017-10-31 06:14:15 +00:00
var detailedRSSFeedURL = getTestDataUrl ( "feedDetailed.rss" ) ;
var detailedRSSFeedInfo = {
2016-01-12 13:28:15 +00:00
title : 'Feed' ,
subtitle : 'Feed Description' ,
creators : [ { firstName : 'Feed' , lastName : 'Author' , creatorType : 'author' } ] ,
publicationTitle : 'Publication' ,
publisher : 'Publisher' ,
rights : '©2016 Published by Publisher' ,
ISSN : '0000-0000' ,
language : 'en'
} ;
2017-10-31 06:14:15 +00:00
2021-05-16 16:47:02 +00:00
var richTextRSSFeedURL = getTestDataUrl ( "feedRichText.rss" ) ;
var cdataRSSFeedURL = getTestDataUrl ( "feedCDATA.rss" ) ;
2017-10-31 06:14:15 +00:00
var atomFeedURL = getTestDataUrl ( "feed.atom" ) ;
2021-05-16 16:47:02 +00:00
var mediaFeedURL = getTestDataUrl ( "feedMedia.xml" ) ;
2017-10-31 06:14:15 +00:00
2021-05-22 17:44:42 +00:00
var win ;
before ( async function ( ) {
// Browser window is needed as parent window to load the feed reader scripts.
win = await loadBrowserWindow ( ) ;
} ) ;
after ( async function ( ) {
if ( win ) {
win . close ( ) ;
}
await clearFeeds ( ) ;
2016-01-13 13:13:29 +00:00
} ) ;
2016-01-12 13:28:15 +00:00
describe ( 'FeedReader()' , function ( ) {
it ( 'should throw if url not provided' , function ( ) {
assert . throw ( ( ) => new Zotero . FeedReader ( ) )
} ) ;
it ( 'should throw if url invalid' , function ( ) {
assert . throw ( ( ) => new Zotero . FeedReader ( 'invalid url' ) )
} ) ;
} ) ;
describe ( '#process()' , function ( ) {
it ( 'should reject if the provided url is not a valid feed' , function * ( ) {
let fr = new Zotero . FeedReader ( htmlUrl ) ;
let e = yield getPromiseError ( fr . process ( ) ) ;
assert . ok ( e ) ;
e = yield getPromiseError ( fr . _feedItems [ fr . _feedItems . length - 1 ] . promise ) ;
assert . ok ( e ) ;
} ) ;
it ( 'should set #feedProperties on FeedReader object' , function * ( ) {
2017-10-31 06:14:15 +00:00
let fr = new Zotero . FeedReader ( rssFeedURL ) ;
2016-01-12 13:28:15 +00:00
assert . throw ( ( ) => fr . feedProperties ) ;
yield fr . process ( ) ;
assert . ok ( fr . feedProperties ) ;
} ) ;
} ) ;
describe ( '#terminate()' , function ( ) {
it ( 'should reject last feed item and feed processing promise if feed not processed yet' , function * ( ) {
2017-10-31 06:14:15 +00:00
let fr = new Zotero . FeedReader ( rssFeedURL ) ;
2016-01-12 13:28:15 +00:00
fr . terminate ( "test" ) ;
let e = yield getPromiseError ( fr . process ( ) ) ;
assert . ok ( e ) ;
e = yield getPromiseError ( fr . _feedItems [ fr . _feedItems . length - 1 ] . promise ) ;
assert . ok ( e ) ;
} ) ;
it ( 'should reject last feed item if feed processed' , function * ( ) {
2017-10-31 06:14:15 +00:00
let fr = new Zotero . FeedReader ( rssFeedURL ) ;
2016-01-12 13:28:15 +00:00
yield fr . process ( ) ;
fr . terminate ( "test" ) ;
let e = yield getPromiseError ( fr . _feedItems [ fr . _feedItems . length - 1 ] . promise ) ;
assert . ok ( e ) ;
} ) ;
} ) ;
describe ( '#feedProperties' , function ( ) {
it ( 'should throw if accessed before feed is processed' , function ( ) {
2017-10-31 06:14:15 +00:00
let fr = new Zotero . FeedReader ( rssFeedURL ) ;
2016-01-12 13:28:15 +00:00
assert . throw ( ( ) => fr . feedProperties ) ;
} ) ;
it ( 'should have correct values for a sparse feed' , function * ( ) {
2017-10-31 06:14:15 +00:00
let fr = new Zotero . FeedReader ( rssFeedURL ) ;
2016-01-12 13:28:15 +00:00
yield fr . process ( ) ;
2017-10-31 06:14:15 +00:00
assert . deepEqual ( fr . feedProperties , rssFeedInfo ) ;
2016-01-12 13:28:15 +00:00
} ) ;
it ( 'should have correct values for a detailed feed' , function * ( ) {
2017-10-31 06:14:15 +00:00
let fr = new Zotero . FeedReader ( detailedRSSFeedURL ) ;
2016-01-12 13:28:15 +00:00
yield fr . process ( ) ;
2017-10-31 06:14:15 +00:00
assert . deepEqual ( fr . feedProperties , detailedRSSFeedInfo ) ;
2016-01-12 13:28:15 +00:00
} ) ;
} ) ;
describe ( '#ItemIterator()' , function ( ) {
it ( 'should throw if called before feed is resolved' , function ( ) {
2017-10-31 06:14:15 +00:00
let fr = new Zotero . FeedReader ( rssFeedURL ) ;
2016-01-12 13:28:15 +00:00
assert . throw ( ( ) => new fr . ItemIterator ) ;
} ) ;
2017-10-31 06:14:15 +00:00
it ( 'should parse items correctly for a sparse RSS feed' , function * ( ) {
2016-02-11 11:02:38 +00:00
let expected = { guid : 'http://liftoff.msfc.nasa.gov/2003/06/03.html#item573' ,
2016-01-12 13:28:15 +00:00
title : 'Star City' ,
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.' ,
url : 'http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp' ,
2016-02-11 11:02:38 +00:00
creators : [ { firstName : '' , lastName : 'editor@example.com' , creatorType : 'author' , fieldMode : 1 } ] ,
2016-02-05 18:29:15 +00:00
date : 'Tue, 03 Jun 2003 09:39:21 GMT' ,
2016-01-12 13:28:15 +00:00
language : 'en-us' ,
2016-02-11 11:02:38 +00:00
itemType : 'journalArticle' ,
enclosedItems : [ { url : 'http://www.example.com/example.pdf' , contentType : 'application/pdf' } ]
2016-01-12 13:28:15 +00:00
} ;
2017-10-31 06:14:15 +00:00
let fr = new Zotero . FeedReader ( rssFeedURL ) ;
2016-01-12 13:28:15 +00:00
yield fr . process ( ) ;
let itemIterator = new fr . ItemIterator ( ) ;
let item = yield itemIterator . next ( ) . value ;
assert . deepEqual ( item , expected ) ;
} ) ;
2017-10-31 06:14:15 +00:00
it ( 'should parse items correctly for a detailed RSS feed' , function * ( ) {
2016-01-13 13:13:29 +00:00
let expected = {
guid : 'http://www.example.com/item1' ,
2016-01-12 13:28:15 +00:00
title : 'Title 1' ,
abstractNote : 'Description 1' ,
url : 'http://www.example.com/item1' ,
creators : [
{ firstName : 'Author1 A. T.' , lastName : 'Rohtua' , creatorType : 'author' } ,
{ firstName : 'Author2 A.' , lastName : 'Auth' , creatorType : 'author' } ,
{ firstName : 'Author3' , lastName : 'Autho' , creatorType : 'author' } ,
{ firstName : 'Contributor1 A. T.' , lastName : 'Rotubirtnoc' , creatorType : 'contributor' } ,
{ firstName : 'Contributor2 C.' , lastName : 'Contrib' , creatorType : 'contributor' } ,
{ firstName : 'Contributor3' , lastName : 'Contr' , creatorType : 'contributor' }
] ,
2016-01-13 13:13:29 +00:00
date : '2016-01-07' ,
2016-01-12 13:28:15 +00:00
publicationTitle : 'Publication' ,
ISSN : '0000-0000' ,
publisher : 'Publisher' ,
rights : '©2016 Published by Publisher' ,
language : 'en' ,
2016-02-11 11:02:38 +00:00
itemType : 'journalArticle' ,
enclosedItems : [ ]
2016-01-12 13:28:15 +00:00
} ;
2017-10-31 06:14:15 +00:00
let fr = new Zotero . FeedReader ( detailedRSSFeedURL ) ;
2016-01-12 13:28:15 +00:00
yield fr . process ( ) ;
let itemIterator = new fr . ItemIterator ( ) ;
let item = yield itemIterator . next ( ) . value ;
assert . deepEqual ( item , expected ) ;
} ) ;
2017-10-31 06:14:15 +00:00
it ( "should parse item from an Atom feed" , function * ( ) {
let expected = {
guid : 'http://www.example.com/item1' ,
title : 'Title 1' ,
abstractNote : 'Abstract 1' ,
url : 'http://www.example.com/item1' ,
creators : [
{ firstName : 'Author1 A. T.' , lastName : 'Rohtua' , creatorType : 'author' } ,
{ firstName : 'Author2 A.' , lastName : 'Auth' , creatorType : 'author' }
] ,
// TODO: DOI?
date : '2017-10-27T12:27:09Z' ,
itemType : 'journalArticle' ,
enclosedItems : [ ]
} ;
let fr = new Zotero . FeedReader ( atomFeedURL ) ;
yield fr . process ( ) ;
let itemIterator = new fr . ItemIterator ( ) ;
let item = yield itemIterator . next ( ) . value ;
assert . deepEqual ( item , expected ) ;
} ) ;
2016-01-12 13:28:15 +00:00
it ( 'should resolve last item with null' , function * ( ) {
2017-10-31 06:14:15 +00:00
let fr = new Zotero . FeedReader ( rssFeedURL ) ;
2016-01-12 13:28:15 +00:00
yield fr . process ( ) ;
let itemIterator = new fr . ItemIterator ( ) ;
let item ;
while ( item = yield itemIterator . next ( ) . value ) ;
assert . isNull ( item ) ;
} ) ;
2021-05-16 16:47:02 +00:00
it ( 'should decode entities' , async ( ) => {
const fr = new Zotero . FeedReader ( richTextRSSFeedURL ) ;
await fr . process ( ) ;
const itemIterator = new fr . ItemIterator ( ) ;
const item = await itemIterator . next ( ) . value ;
assert . equal ( item . title , ` Encoded "entity" ` ) ;
assert . equal ( item . abstractNote , "They take a crash course in language & protocol." ) ;
} ) ;
it ( 'should remove tags' , async ( ) => {
const fr = new Zotero . FeedReader ( richTextRSSFeedURL ) ;
await fr . process ( ) ;
const itemIterator = new fr . ItemIterator ( ) ;
let item ;
for ( let i = 0 ; i < 2 ; i ++ ) {
// eslint-disable-next-line no-await-in-loop
item = await itemIterator . next ( ) . value ;
}
// The entry title is text only, so tags are just more text.
assert . equal ( item . title , "Embedded <b>tags</b>" ) ;
// The entry description is XHTML, so tags are removed there.
assert . equal ( item . abstractNote , "The proposed VASIMR engine would do that." ) ;
} ) ;
it ( 'should parse CDATA as text' , async ( ) => {
const fr = new Zotero . FeedReader ( cdataRSSFeedURL ) ;
await fr . process ( ) ;
const itemIterator = new fr . ItemIterator ( ) ;
const item = await itemIterator . next ( ) . value ;
assert . equal ( item . title , ` "The Descent of Man," 150 years on ` ) ;
assert . equal ( item . creators [ 0 ] . lastName , "Fuentes" ) ;
} ) ;
it ( 'should parse enclosed media' , async ( ) => {
const fr = new Zotero . FeedReader ( mediaFeedURL ) ;
await fr . process ( ) ;
const itemIterator = new fr . ItemIterator ( ) ;
const item = await itemIterator . next ( ) . value ;
assert . equal ( item . enclosedItems . length , 1 ) ;
assert . equal ( item . enclosedItems [ 0 ] . url , "https://static01.nyt.com/images/2021/06/16/world/16biden-photos1/16biden-photos1-moth.jpg" ) ;
} ) ;
2016-01-12 13:28:15 +00:00
} ) ;
2021-05-16 16:47:02 +00:00
} )