astro/packages/astro-rss/test/rss.test.js
Ben Holmes fbfb6190ab
Feat: @astrojs/rss package! (#3271)
* feat: introduce @astrojs/rss package!

* feat: add config "site" to env variable

* docs: add @astrojs/rss readme

* chore: changeset

* fix: testing script

* deps: add mocha, chai, chai-promises

* tests: add rss test!

* feat: add canonicalUrl arg

* chore: remove console.log

* fix: remove null check on env (breaks build)

* docs: stray `

* chore: update error message to doc link

* chore: remove getStylesheet

* docs: update stylesheet reference
2022-05-03 18:26:13 -04:00

122 lines
4 KiB
JavaScript

import rss from '../dist/index.js';
import chai from 'chai';
import chaiPromises from 'chai-as-promised';
chai.use(chaiPromises);
const title = 'My RSS feed';
const description = 'This sure is a nice RSS feed';
const canonicalUrl = '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 web1FeedItem = {
link: '/web1',
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.',
};
// note: I spent 30 minutes looking for a nice node-based snapshot tool
// ...and I gave up. Enjoy big strings!
const validXmlResult = `<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"><channel><title><![CDATA[My RSS feed]]></title><description><![CDATA[This sure is a nice RSS feed]]></description><link>https://example.com/</link><item><title><![CDATA[Remember PHP?]]></title><link>https://example.com/php/</link><guid>https://example.com/php/</guid><description><![CDATA[PHP is a general-purpose scripting language geared toward web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1994.]]></description><pubDate>Tue, 03 May 1994 00:00:00 GMT</pubDate></item><item><title><![CDATA[Web 1.0]]></title><link>https://example.com/web1/</link><guid>https://example.com/web1/</guid><description><![CDATA[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.]]></description><pubDate>Sat, 03 May 1997 00:00:00 GMT</pubDate></item></channel></rss>`;
describe('rss', () => {
it('should fail on missing "site" and/or "canonicalUrl"', () => {
return chai.expect(
rss({
title,
description,
items: [],
})
).to.be.rejected;
});
it('should generate on valid RSSFeedItem array', async () => {
const { body } = await rss({
title,
description,
items: [phpFeedItem, web1FeedItem],
canonicalUrl,
});
chai.expect(body).to.equal(validXmlResult);
})
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,
canonicalUrl,
});
chai.expect(body).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,
canonicalUrl,
})
).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,
canonicalUrl,
})
).to.be.rejected;
});
});
})