-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add tests for hash function * Add tests for gatsby-plugin-remove-trailing-slashes * Add tests for plugin feed * Update coverage threshold * Fix trailing comma eslint warning * Add a snapshot to verify graphql was called with custom query
- Loading branch information
1 parent
e36cc2a
commit c60c773
Showing
7 changed files
with
240 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
packages/gatsby-plugin-feed/src/__tests__/__snapshots__/gatsby-node.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Test plugin feed custom query runs 1`] = ` | ||
[MockFunction] { | ||
"calls": Array [ | ||
Array [ | ||
"public/rss_new.xml", | ||
"<?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>", | ||
], | ||
], | ||
"results": Array [ | ||
Object { | ||
"isThrow": false, | ||
"value": Promise {}, | ||
}, | ||
], | ||
} | ||
`; | ||
|
||
exports[`Test plugin feed custom query runs 2`] = ` | ||
[MockFunction] { | ||
"calls": Array [ | ||
Array [ | ||
" | ||
{ | ||
site { | ||
siteMetadata { | ||
title | ||
description | ||
siteUrl | ||
site_url: siteUrl | ||
} | ||
} | ||
} | ||
", | ||
], | ||
Array [ | ||
" | ||
{ | ||
allMarkdownRemark( | ||
limit: 1000, | ||
) { | ||
edges { | ||
node { | ||
frontmatter { | ||
path | ||
} | ||
excerpt | ||
} | ||
} | ||
} | ||
} | ||
", | ||
], | ||
], | ||
"results": Array [ | ||
Object { | ||
"isThrow": false, | ||
"value": Promise {}, | ||
}, | ||
Object { | ||
"isThrow": false, | ||
"value": Promise {}, | ||
}, | ||
], | ||
} | ||
`; | ||
|
||
exports[`Test plugin feed default settings work properly 1`] = ` | ||
[MockFunction] { | ||
"calls": Array [ | ||
Array [ | ||
"public/rss.xml", | ||
"<?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>", | ||
], | ||
], | ||
"results": Array [ | ||
Object { | ||
"isThrow": false, | ||
"value": Promise {}, | ||
}, | ||
], | ||
} | ||
`; |
110 changes: 110 additions & 0 deletions
110
packages/gatsby-plugin-feed/src/__tests__/gatsby-node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
const fs = require(`fs`) | ||
const { onPostBuild } = require(`../gatsby-node`) | ||
const internals = require(`../internals`) | ||
jest.mock(`fs`) | ||
const DATE_TO_USE = new Date(`2018`) | ||
const _Date = Date | ||
global.Date = jest.fn(() => DATE_TO_USE) | ||
global.Date.UTC = _Date.UTC | ||
global.Date.now = _Date.now | ||
|
||
|
||
describe(`Test plugin feed`, async () => { | ||
fs.existsSync = jest.fn() | ||
fs.existsSync.mockReturnValue(true) | ||
|
||
it(`default settings work properly`, async () => { | ||
internals.writeFile = jest.fn() | ||
internals.writeFile.mockResolvedValue(true) | ||
const graphql = jest.fn() | ||
graphql.mockResolvedValue({ data: { | ||
site : { | ||
siteMetadata: { | ||
title: `a sample title`, | ||
description: `a description`, | ||
siteUrl: `http://dummy.url/`, | ||
}, | ||
}, | ||
allMarkdownRemark: { | ||
edges: [ | ||
{ node: { | ||
fields: { | ||
slug: `a-slug`, | ||
}, | ||
excerpt: `post description`, | ||
} }, | ||
], | ||
}, | ||
} }) | ||
await onPostBuild({ graphql }, {}) | ||
expect(internals.writeFile).toMatchSnapshot() | ||
}) | ||
|
||
it(`custom query runs`, async () => { | ||
internals.writeFile = jest.fn() | ||
internals.writeFile.mockResolvedValue(true) | ||
const graphql = jest.fn() | ||
graphql.mockResolvedValue({ data: { | ||
site : { | ||
siteMetadata: { | ||
title: `a sample 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 options = { | ||
feeds: [ | ||
{ | ||
output: `rss_new.xml`, | ||
serialize: ({ query: { site, allMarkdownRemark } }) => | ||
allMarkdownRemark.edges.map(edge => { | ||
return { | ||
...edge.node.frontmatter, | ||
description: edge.node.excerpt, | ||
url: site.siteMetadata.siteUrl + edge.node.frontmatter.path, | ||
} | ||
}), | ||
query: ` | ||
{ | ||
allMarkdownRemark( | ||
limit: 1000, | ||
) { | ||
edges { | ||
node { | ||
frontmatter { | ||
path | ||
} | ||
excerpt | ||
} | ||
} | ||
} | ||
} | ||
`, | ||
}], | ||
} | ||
await onPostBuild({ graphql }, options) | ||
expect(internals.writeFile).toMatchSnapshot() | ||
expect(graphql).toMatchSnapshot() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
packages/gatsby-plugin-remove-trailing-slashes/src/__tests__/gatsby-node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const { onCreatePage } = require(`../gatsby-node`) | ||
|
||
describe(`gatsby-plugin-remove-trailing-slashes`, () => { | ||
const actions = { | ||
createPage: jest.fn(), | ||
deletePage: jest.fn(), | ||
} | ||
|
||
it(`correctly keeps index /`, () => { | ||
onCreatePage({ actions, page: { page: `/` } }) | ||
expect(actions.createPage).not.toBeCalled() | ||
expect(actions.deletePage).not.toBeCalled() | ||
}) | ||
|
||
it(`correctly removes slash and recreated page`, () => { | ||
onCreatePage({ actions, page: { path: `/home/` } }) | ||
expect(actions.deletePage).toBeCalledWith({ path: `/home/` }) | ||
expect(actions.createPage).toBeCalledWith({ path: `/home` }) | ||
}) | ||
}) |
5 changes: 5 additions & 0 deletions
5
packages/gatsby/src/utils/__tests__/__snapshots__/get-hash-fn.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Test hashing function default parameters 1`] = `174577032270956`; | ||
|
||
exports[`Test hashing function guards against collisions 1`] = `[Error: Hash collision at f(my input) = 174577032270956]`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const getHashFn = require(`../get-hash-fn`) | ||
|
||
describe(`Test hashing function`, () => { | ||
it(`default parameters`, () => { | ||
const hash = getHashFn({})(`my input`) | ||
expect(hash).toMatchSnapshot() | ||
}) | ||
it(`guards against collisions`, () => { | ||
const hash = getHashFn({})(`my input`) | ||
try { | ||
getHashFn({ cache: new Set([hash]) })(`my input`) | ||
} catch(err) { | ||
expect(err).toMatchSnapshot() | ||
} | ||
}) | ||
}) |