Skip to content

Commit 92df1cf

Browse files
eyalrothsidharthachatterjee
authored andcommittedAug 23, 2019
fix(gatsby-plugin-feed): respect custom options and require title in config (#16814)
1 parent efb0198 commit 92df1cf

File tree

4 files changed

+98
-8
lines changed

4 files changed

+98
-8
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
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>"`;
3+
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>"`;
44
5-
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>"`;
5+
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>"`;
6+
7+
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>"`;
68
79
exports[`Test plugin feed options validation throws when invalid plugin options 1`] = `[Error: [Config Validation]: "output" is required]`;
810
911
exports[`Test plugin feed options validation throws when invalid plugin options 2`] = `[Error: [Config Validation]: "query" is required]`;
12+
13+
exports[`Test plugin feed options validation throws when invalid plugin options 3`] = `[Error: [Config Validation]: "title" is required]`;

‎packages/gatsby-plugin-feed/src/__tests__/gatsby-node.js

+90-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ describe(`Test plugin feed`, async () => {
5353
{
5454
output: `rss.xml`,
5555
query: `{}`,
56+
title: `my feed`,
5657
},
5758
],
5859
}
@@ -72,14 +73,25 @@ describe(`Test plugin feed`, async () => {
7273
{
7374
// output is missing
7475
query: `{}`,
76+
title: `my feed`,
7577
},
7678
],
7779
},
7880
{
7981
feeds: [
8082
{
8183
output: `rss.xml`,
82-
// query is misisng
84+
// query is missing
85+
title: `my feed`,
86+
},
87+
],
88+
},
89+
{
90+
feeds: [
91+
{
92+
output: `rss.xml`,
93+
query: `{}`,
94+
// title is missing
8395
},
8496
],
8597
},
@@ -130,6 +142,82 @@ describe(`Test plugin feed`, async () => {
130142
expect(contents).toMatchSnapshot()
131143
})
132144

145+
it(`custom properties work properly`, async () => {
146+
fs.writeFile = jest.fn()
147+
fs.writeFile.mockResolvedValue(true)
148+
const graphql = jest.fn()
149+
graphql.mockResolvedValue({
150+
data: {
151+
site: {
152+
siteMetadata: {
153+
title: `site title`,
154+
description: `a description`,
155+
siteUrl: `http://dummy.url/`,
156+
},
157+
},
158+
allMarkdownRemark: {
159+
edges: [
160+
{
161+
node: {
162+
frontmatter: {
163+
path: `a-custom-path`,
164+
},
165+
excerpt: `post description`,
166+
},
167+
},
168+
{
169+
node: {
170+
frontmatter: {
171+
path: `another-custom-path`,
172+
},
173+
excerpt: `post description`,
174+
},
175+
},
176+
],
177+
},
178+
},
179+
})
180+
const customQuery = `
181+
{
182+
allMarkdownRemark(
183+
limit: 1000,
184+
) {
185+
edges {
186+
node {
187+
frontmatter {
188+
path
189+
}
190+
excerpt
191+
}
192+
}
193+
}
194+
}
195+
`
196+
const options = {
197+
feeds: [
198+
{
199+
output: `rss.xml`,
200+
title: `feed title`,
201+
language: `en`,
202+
generator: `custom generator`,
203+
query: customQuery,
204+
serialize: ({ query: { site, allMarkdownRemark } }) =>
205+
allMarkdownRemark.edges.map(edge => {
206+
return {
207+
...edge.node.frontmatter,
208+
description: edge.node.excerpt,
209+
url: site.siteMetadata.siteUrl + edge.node.frontmatter.path,
210+
}
211+
}),
212+
},
213+
],
214+
}
215+
await onPostBuild({ graphql }, options)
216+
const [filePath, contents] = fs.writeFile.mock.calls[0]
217+
expect(filePath).toEqual(path.join(`public`, `rss.xml`))
218+
expect(contents).toMatchSnapshot()
219+
})
220+
133221
it(`custom query runs`, async () => {
134222
fs.writeFile = jest.fn()
135223
fs.writeFile.mockResolvedValue(true)
@@ -194,6 +282,7 @@ describe(`Test plugin feed`, async () => {
194282
}
195283
}),
196284
query: customQuery,
285+
title: `my feed`,
197286
},
198287
],
199288
}

‎packages/gatsby-plugin-feed/src/internals.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ export const defaultOptions = {
2929
setup: ({
3030
query: {
3131
site: { siteMetadata },
32-
...rest
3332
},
33+
...rest
3434
}) => {
3535
return {
3636
...siteMetadata,
@@ -69,9 +69,6 @@ export const defaultOptions = {
6969

7070
// Where we will save the feed generated by this query.
7171
output: `rss.xml`,
72-
73-
// Use a title when reference a RSS feed in Link element.
74-
title: null,
7572
},
7673
],
7774
}

‎packages/gatsby-plugin-feed/src/plugin-options.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Joi from "@hapi/joi"
44
const feed = Joi.object({
55
output: Joi.string().required(),
66
query: Joi.string().required(),
7-
title: Joi.string(),
7+
title: Joi.string().required(),
88
serialize: Joi.func(),
99
match: Joi.string(),
1010
}).unknown(true)

0 commit comments

Comments
 (0)
Please sign in to comment.