Skip to content

Commit

Permalink
fix(gatsby-plugin-feed): respect custom options and require title in …
Browse files Browse the repository at this point in the history
…config (#16814)
  • Loading branch information
eyalroth authored and sidharthachatterjee committed Aug 23, 2019
1 parent efb0198 commit 92df1cf
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Test plugin feed custom query runs 1`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?><rss xmlns:dc=\\"http://purl.org/dc/elements/1.1/\\" xmlns:content=\\"http://purl.org/rss/1.0/modules/content/\\" xmlns:atom=\\"http://www.w3.org/2005/Atom\\" version=\\"2.0\\"><channel><title><![CDATA[a sample title]]></title><description><![CDATA[a description]]></description><link>http://github.com/dylang/node-rss</link><generator>RSS for Node</generator><lastBuildDate>Mon, 01 Jan 2018 00:00:00 GMT</lastBuildDate><item><title><![CDATA[No title]]></title><description><![CDATA[post description]]></description><link>http://dummy.url/a-custom-path</link><guid isPermaLink=\\"true\\">http://dummy.url/a-custom-path</guid></item><item><title><![CDATA[No title]]></title><description><![CDATA[post description]]></description><link>http://dummy.url/another-custom-path</link><guid isPermaLink=\\"true\\">http://dummy.url/another-custom-path</guid></item></channel></rss>"`;
exports[`Test plugin feed custom properties work properly 1`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?><rss xmlns:dc=\\"http://purl.org/dc/elements/1.1/\\" xmlns:content=\\"http://purl.org/rss/1.0/modules/content/\\" xmlns:atom=\\"http://www.w3.org/2005/Atom\\" version=\\"2.0\\"><channel><title><![CDATA[feed title]]></title><description><![CDATA[a description]]></description><link>http://github.com/dylang/node-rss</link><generator>custom generator</generator><lastBuildDate>Mon, 01 Jan 2018 00:00:00 GMT</lastBuildDate><language><![CDATA[en]]></language><item><title><![CDATA[No title]]></title><description><![CDATA[post description]]></description><link>http://dummy.url/a-custom-path</link><guid isPermaLink=\\"true\\">http://dummy.url/a-custom-path</guid></item><item><title><![CDATA[No title]]></title><description><![CDATA[post description]]></description><link>http://dummy.url/another-custom-path</link><guid isPermaLink=\\"true\\">http://dummy.url/another-custom-path</guid></item></channel></rss>"`;
exports[`Test plugin feed default settings work properly 1`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?><rss xmlns:dc=\\"http://purl.org/dc/elements/1.1/\\" xmlns:content=\\"http://purl.org/rss/1.0/modules/content/\\" xmlns:atom=\\"http://www.w3.org/2005/Atom\\" version=\\"2.0\\"><channel><title><![CDATA[a sample title]]></title><description><![CDATA[a description]]></description><link>http://github.com/dylang/node-rss</link><generator>RSS for Node</generator><lastBuildDate>Mon, 01 Jan 2018 00:00:00 GMT</lastBuildDate><item><title><![CDATA[No title]]></title><description><![CDATA[post description]]></description><link>http://dummy.url/a-slug</link><guid isPermaLink=\\"false\\">http://dummy.url/a-slug</guid><content:encoded></content:encoded></item></channel></rss>"`;
exports[`Test plugin feed custom query runs 1`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?><rss xmlns:dc=\\"http://purl.org/dc/elements/1.1/\\" xmlns:content=\\"http://purl.org/rss/1.0/modules/content/\\" xmlns:atom=\\"http://www.w3.org/2005/Atom\\" version=\\"2.0\\"><channel><title><![CDATA[my feed]]></title><description><![CDATA[a description]]></description><link>http://github.com/dylang/node-rss</link><generator>GatsbyJS</generator><lastBuildDate>Mon, 01 Jan 2018 00:00:00 GMT</lastBuildDate><item><title><![CDATA[No title]]></title><description><![CDATA[post description]]></description><link>http://dummy.url/a-custom-path</link><guid isPermaLink=\\"true\\">http://dummy.url/a-custom-path</guid></item><item><title><![CDATA[No title]]></title><description><![CDATA[post description]]></description><link>http://dummy.url/another-custom-path</link><guid isPermaLink=\\"true\\">http://dummy.url/another-custom-path</guid></item></channel></rss>"`;
exports[`Test plugin feed default settings work properly 1`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?><rss xmlns:dc=\\"http://purl.org/dc/elements/1.1/\\" xmlns:content=\\"http://purl.org/rss/1.0/modules/content/\\" xmlns:atom=\\"http://www.w3.org/2005/Atom\\" version=\\"2.0\\"><channel><title><![CDATA[a sample title]]></title><description><![CDATA[a description]]></description><link>http://github.com/dylang/node-rss</link><generator>GatsbyJS</generator><lastBuildDate>Mon, 01 Jan 2018 00:00:00 GMT</lastBuildDate><item><title><![CDATA[No title]]></title><description><![CDATA[post description]]></description><link>http://dummy.url/a-slug</link><guid isPermaLink=\\"false\\">http://dummy.url/a-slug</guid><content:encoded></content:encoded></item></channel></rss>"`;
exports[`Test plugin feed options validation throws when invalid plugin options 1`] = `[Error: [Config Validation]: "output" is required]`;
exports[`Test plugin feed options validation throws when invalid plugin options 2`] = `[Error: [Config Validation]: "query" is required]`;
exports[`Test plugin feed options validation throws when invalid plugin options 3`] = `[Error: [Config Validation]: "title" is required]`;
91 changes: 90 additions & 1 deletion packages/gatsby-plugin-feed/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ describe(`Test plugin feed`, async () => {
{
output: `rss.xml`,
query: `{}`,
title: `my feed`,
},
],
}
Expand All @@ -72,14 +73,25 @@ describe(`Test plugin feed`, async () => {
{
// output is missing
query: `{}`,
title: `my feed`,
},
],
},
{
feeds: [
{
output: `rss.xml`,
// query is misisng
// query is missing
title: `my feed`,
},
],
},
{
feeds: [
{
output: `rss.xml`,
query: `{}`,
// title is missing
},
],
},
Expand Down Expand Up @@ -130,6 +142,82 @@ describe(`Test plugin feed`, async () => {
expect(contents).toMatchSnapshot()
})

it(`custom properties work properly`, async () => {
fs.writeFile = jest.fn()
fs.writeFile.mockResolvedValue(true)
const graphql = jest.fn()
graphql.mockResolvedValue({
data: {
site: {
siteMetadata: {
title: `site title`,
description: `a description`,
siteUrl: `http://dummy.url/`,
},
},
allMarkdownRemark: {
edges: [
{
node: {
frontmatter: {
path: `a-custom-path`,
},
excerpt: `post description`,
},
},
{
node: {
frontmatter: {
path: `another-custom-path`,
},
excerpt: `post description`,
},
},
],
},
},
})
const customQuery = `
{
allMarkdownRemark(
limit: 1000,
) {
edges {
node {
frontmatter {
path
}
excerpt
}
}
}
}
`
const options = {
feeds: [
{
output: `rss.xml`,
title: `feed title`,
language: `en`,
generator: `custom generator`,
query: customQuery,
serialize: ({ query: { site, allMarkdownRemark } }) =>
allMarkdownRemark.edges.map(edge => {
return {
...edge.node.frontmatter,
description: edge.node.excerpt,
url: site.siteMetadata.siteUrl + edge.node.frontmatter.path,
}
}),
},
],
}
await onPostBuild({ graphql }, options)
const [filePath, contents] = fs.writeFile.mock.calls[0]
expect(filePath).toEqual(path.join(`public`, `rss.xml`))
expect(contents).toMatchSnapshot()
})

it(`custom query runs`, async () => {
fs.writeFile = jest.fn()
fs.writeFile.mockResolvedValue(true)
Expand Down Expand Up @@ -194,6 +282,7 @@ describe(`Test plugin feed`, async () => {
}
}),
query: customQuery,
title: `my feed`,
},
],
}
Expand Down
5 changes: 1 addition & 4 deletions packages/gatsby-plugin-feed/src/internals.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export const defaultOptions = {
setup: ({
query: {
site: { siteMetadata },
...rest
},
...rest
}) => {
return {
...siteMetadata,
Expand Down Expand Up @@ -69,9 +69,6 @@ export const defaultOptions = {

// Where we will save the feed generated by this query.
output: `rss.xml`,

// Use a title when reference a RSS feed in Link element.
title: null,
},
],
}
2 changes: 1 addition & 1 deletion packages/gatsby-plugin-feed/src/plugin-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Joi from "@hapi/joi"
const feed = Joi.object({
output: Joi.string().required(),
query: Joi.string().required(),
title: Joi.string(),
title: Joi.string().required(),
serialize: Joi.func(),
match: Joi.string(),
}).unknown(true)
Expand Down

0 comments on commit 92df1cf

Please sign in to comment.