diff options
Diffstat (limited to '')
-rw-r--r-- | packages/astro-rss/test/rss.test.js | 320 |
1 files changed, 58 insertions, 262 deletions
diff --git a/packages/astro-rss/test/rss.test.js b/packages/astro-rss/test/rss.test.js index bd4c9cba7..744471f8c 100644 --- a/packages/astro-rss/test/rss.test.js +++ b/packages/astro-rss/test/rss.test.js @@ -2,43 +2,20 @@ import rss from '../dist/index.js'; import chai from 'chai'; import chaiPromises from 'chai-as-promised'; import chaiXml from 'chai-xml'; +import { + title, + description, + site, + phpFeedItem, + phpFeedItemWithContent, + phpFeedItemWithCustomData, + web1FeedItem, + web1FeedItemWithContent, +} from './test-utils.js'; chai.use(chaiPromises); chai.use(chaiXml); -const title = 'My RSS feed'; -const description = 'This sure is a nice RSS feed'; -const site = 'https://example.com'; - -const phpFeedItem = { - link: '/php', - title: 'Remember PHP?', - pubDate: '1994-05-03', - description: - 'PHP is a general-purpose scripting language geared toward web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1994.', -}; -const phpFeedItemWithContent = { - ...phpFeedItem, - content: `<h1>${phpFeedItem.title}</h1><p>${phpFeedItem.description}</p>`, -}; -const phpFeedItemWithCustomData = { - ...phpFeedItem, - customData: '<dc:creator><![CDATA[Buster Bluth]]></dc:creator>', -}; - -const web1FeedItem = { - // Should support empty string as a URL (possible for homepage route) - link: '', - title: 'Web 1.0', - pubDate: '1997-05-03', - description: - 'Web 1.0 is the term used for the earliest version of the Internet as it emerged from its origins with Defense Advanced Research Projects Agency (DARPA) and became, for the first time, a global network representing the future of digital communications.', -}; -const web1FeedItemWithContent = { - ...web1FeedItem, - content: `<h1>${web1FeedItem.title}</h1><p>${web1FeedItem.description}</p>`, -}; - // note: I spent 30 minutes looking for a nice node-based snapshot tool // ...and I gave up. Enjoy big strings! // prettier-ignore @@ -115,244 +92,63 @@ describe('rss', () => { chai.expect(body).xml.to.equal(validXmlWithXSLStylesheet); }); - describe('glob result', () => { - it('should generate on valid result', async () => { - const globResult = { - './posts/php.md': () => - new Promise((resolve) => - resolve({ - url: phpFeedItem.link, - frontmatter: { - title: phpFeedItem.title, - pubDate: phpFeedItem.pubDate, - description: phpFeedItem.description, - }, - }) - ), - './posts/nested/web1.md': () => - new Promise((resolve) => - resolve({ - url: web1FeedItem.link, - frontmatter: { - title: web1FeedItem.title, - pubDate: web1FeedItem.pubDate, - description: web1FeedItem.description, - }, - }) - ), - }; - - const { body } = await rss({ - title, - description, - items: globResult, - site, - }); - - chai.expect(body).xml.to.equal(validXmlResult); - }); - - it('should fail on missing "title" key', () => { - const globResult = { - './posts/php.md': () => - new Promise((resolve) => - resolve({ - url: phpFeedItem.link, - frontmatter: { - pubDate: phpFeedItem.pubDate, - description: phpFeedItem.description, - }, - }) - ), - }; - return chai.expect( - rss({ - title, - description, - items: globResult, - site, - }) - ).to.be.rejected; - }); - - it('should fail on missing "pubDate" key', () => { - const globResult = { - './posts/php.md': () => - new Promise((resolve) => - resolve({ - url: phpFeedItem.link, - frontmatter: { - title: phpFeedItem.title, - description: phpFeedItem.description, - }, - }) - ), - }; - return chai.expect( - rss({ - title, - description, - items: globResult, - site, - }) - ).to.be.rejected; + it('should filter out entries marked as `draft`', async () => { + const { body } = await rss({ + title, + description, + items: [phpFeedItem, { ...web1FeedItem, draft: true }], + site, }); - it('should filter out draft', async () => { - const globResult = { - './posts/php.md': () => - new Promise((resolve) => - resolve({ - url: phpFeedItem.link, - frontmatter: { - title: phpFeedItem.title, - pubDate: phpFeedItem.pubDate, - description: phpFeedItem.description, - }, - }) - ), - './posts/nested/web1.md': () => - new Promise((resolve) => - resolve({ - url: web1FeedItem.link, - frontmatter: { - title: web1FeedItem.title, - pubDate: web1FeedItem.pubDate, - description: web1FeedItem.description, - draft: true, - }, - }) - ), - }; - - const { body } = await rss({ - title, - description, - items: globResult, - site, - }); + chai.expect(body).xml.to.equal(validXmlWithoutWeb1FeedResult); + }); - chai.expect(body).xml.to.equal(validXmlWithoutWeb1FeedResult); + it('should respect drafts option', async () => { + const { body } = await rss({ + title, + description, + drafts: true, + items: [phpFeedItem, { ...web1FeedItem, draft: true }], + site, + drafts: true, }); - it('should respect drafts option', async () => { - const globResult = { - './posts/php.md': () => - new Promise((resolve) => - resolve({ - url: phpFeedItem.link, - frontmatter: { - title: phpFeedItem.title, - pubDate: phpFeedItem.pubDate, - description: phpFeedItem.description, - }, - }) - ), - './posts/nested/web1.md': () => - new Promise((resolve) => - resolve({ - url: web1FeedItem.link, - frontmatter: { - title: web1FeedItem.title, - pubDate: web1FeedItem.pubDate, - description: web1FeedItem.description, - draft: true, - }, - }) - ), - }; - - const { body } = await rss({ - title, - description, - items: globResult, - site, - drafts: true, - }); - - chai.expect(body).xml.to.equal(validXmlResult); - }); + chai.expect(body).xml.to.equal(validXmlResult); }); - describe('errors', () => { - it('should provide a error message when a "site" option is missing', async () => { - try { - await rss({ - title, - description, - items: [phpFeedItem, web1FeedItem], - }); - - chai.expect(false).to.equal(true, 'Should have errored'); - } catch (err) { - chai - .expect(err.message) - .to.contain('[RSS] the "site" option is required, but no value was given.'); - } - }); - - it('should provide a good error message when a link is not provided', async () => { - try { - await rss({ - title: 'Your Website Title', - description: 'Your Website Description', - site: 'https://astro-demo', - items: [ - { - pubDate: new Date(), - title: 'Some title', - slug: 'foo', + it('Deprecated import.meta.glob mapping still works', async () => { + const globResult = { + './posts/php.md': () => + new Promise((resolve) => + resolve({ + url: phpFeedItem.link, + frontmatter: { + title: phpFeedItem.title, + pubDate: phpFeedItem.pubDate, + description: phpFeedItem.description, }, - ], - }); - chai.expect(false).to.equal(true, 'Should have errored'); - } catch (err) { - chai.expect(err.message).to.contain('Required field [link] is missing'); - } - }); - - it('should provide a good error message when passing glob result form outside pages/', async () => { - const globResult = { - './posts/php.md': () => - new Promise((resolve) => - resolve({ - // "undefined" when outside pages/ - url: undefined, - frontmatter: { - title: phpFeedItem.title, - pubDate: phpFeedItem.pubDate, - description: phpFeedItem.description, - }, - }) - ), - './posts/nested/web1.md': () => - new Promise((resolve) => - resolve({ - url: undefined, - frontmatter: { - title: web1FeedItem.title, - pubDate: web1FeedItem.pubDate, - description: web1FeedItem.description, - }, - }) - ), - }; + }) + ), + './posts/nested/web1.md': () => + new Promise((resolve) => + resolve({ + url: web1FeedItem.link, + frontmatter: { + title: web1FeedItem.title, + pubDate: web1FeedItem.pubDate, + description: web1FeedItem.description, + }, + }) + ), + }; - try { - await rss({ - title: 'Your Website Title', - description: 'Your Website Description', - site: 'https://astro-demo', - items: globResult, - }); - chai.expect(false).to.equal(true, 'Should have errored'); - } catch (err) { - chai - .expect(err.message) - .to.contain( - 'you can only glob ".md" (or alternative extensions for markdown files like ".markdown") files within /pages' - ); - } + const { body } = await rss({ + title, + description, + items: globResult, + site, }); + + chai.expect(body).xml.to.equal(validXmlResult); }); }); |