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);
// note: I spent 30 minutes looking for a nice node-based snapshot tool
// ...and I gave up. Enjoy big strings!
// prettier-ignore
const validXmlResult = `${site}/- ${site}${phpFeedItem.link}/${site}${phpFeedItem.link}/${new Date(phpFeedItem.pubDate).toUTCString()}
- ${site}${web1FeedItem.link}/${site}${web1FeedItem.link}/${new Date(web1FeedItem.pubDate).toUTCString()}
`;
// prettier-ignore
const validXmlWithoutWeb1FeedResult = `${site}/- ${site}${phpFeedItem.link}/${site}${phpFeedItem.link}/${new Date(phpFeedItem.pubDate).toUTCString()}
`;
// prettier-ignore
const validXmlWithContentResult = `${site}/- ${site}${phpFeedItemWithContent.link}/${site}${phpFeedItemWithContent.link}/${new Date(phpFeedItemWithContent.pubDate).toUTCString()}
- ${site}${web1FeedItemWithContent.link}/${site}${web1FeedItemWithContent.link}/${new Date(web1FeedItemWithContent.pubDate).toUTCString()}
`;
// prettier-ignore
const validXmlWithCustomDataResult = `${site}/- ${site}${phpFeedItemWithCustomData.link}/${site}${phpFeedItemWithCustomData.link}/${new Date(phpFeedItemWithCustomData.pubDate).toUTCString()}${phpFeedItemWithCustomData.customData}
- ${site}${web1FeedItemWithContent.link}/${site}${web1FeedItemWithContent.link}/${new Date(web1FeedItemWithContent.pubDate).toUTCString()}
`;
// prettier-ignore
const validXmlWithStylesheet = `${site}/`;
// prettier-ignore
const validXmlWithXSLStylesheet = `${site}/`;
describe('rss', () => {
it('should generate on valid RSSFeedItem array', async () => {
const { body } = await rss({
title,
description,
items: [phpFeedItem, web1FeedItem],
site,
});
chai.expect(body).xml.to.equal(validXmlResult);
});
it('should generate on valid RSSFeedItem array with HTML content included', async () => {
const { body } = await rss({
title,
description,
items: [phpFeedItemWithContent, web1FeedItemWithContent],
site,
});
chai.expect(body).xml.to.equal(validXmlWithContentResult);
});
it('should generate on valid RSSFeedItem array with custom data included', async () => {
const { body } = await rss({
xmlns: {
dc: 'http://purl.org/dc/elements/1.1/',
},
title,
description,
items: [phpFeedItemWithCustomData, web1FeedItemWithContent],
site,
});
chai.expect(body).xml.to.equal(validXmlWithCustomDataResult);
});
it('should include xml-stylesheet instruction when stylesheet is defined', async () => {
const { body } = await rss({
title,
description,
items: [],
site,
stylesheet: '/feedstylesheet.css',
});
chai.expect(body).xml.to.equal(validXmlWithStylesheet);
});
it('should include xml-stylesheet instruction with xsl type when stylesheet is set to xsl file', async () => {
const { body } = await rss({
title,
description,
items: [],
site,
stylesheet: '/feedstylesheet.xsl',
});
chai.expect(body).xml.to.equal(validXmlWithXSLStylesheet);
});
it('should filter out entries marked as `draft`', async () => {
const { body } = await rss({
title,
description,
items: [phpFeedItem, { ...web1FeedItem, draft: true }],
site,
});
chai.expect(body).xml.to.equal(validXmlWithoutWeb1FeedResult);
});
it('should respect drafts option', async () => {
const { body } = await rss({
title,
description,
items: [phpFeedItem, { ...web1FeedItem, draft: true }],
site,
drafts: true,
});
chai.expect(body).xml.to.equal(validXmlResult);
});
it('should not append trailing slash to URLs with the given option', async () => {
const { body } = await rss({
title,
description,
items: [phpFeedItem, { ...web1FeedItem, draft: true }],
site,
drafts: true,
trailingSlash: false,
});
chai.expect(body).xml.to.contain('https://example.com/<');
chai.expect(body).xml.to.contain('https://example.com/php<');
});
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,
},
})
),
'./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);
});
});