Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve coverage #8580

Merged
merged 6 commits into from
Oct 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ module.exports = {
coverageReporters: [`json-summary`, `text`, `html`],
coverageThreshold: {
global: {
lines: 42,
statements: 43,
functions: 40,
branches: 42,
lines: 45,
statements: 44,
functions: 42,
branches: 43,
},
},
collectCoverageFrom: coverageDirs,
Expand Down
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 packages/gatsby-plugin-feed/src/__tests__/gatsby-node.js
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()
oorestisime marked this conversation as resolved.
Show resolved Hide resolved
expect(graphql).toMatchSnapshot()
})
})
4 changes: 1 addition & 3 deletions packages/gatsby-plugin-feed/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ exports.onPostBuild = async ({ graphql }, pluginOptions) => {
...pluginOptions,
}

if (`query` in options) {
options.query = await runQuery(graphql, options.query)
}
options.query = await runQuery(graphql, options.query)

for (let f of options.feeds) {
if (f.query) {
Expand Down
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` })
})
})
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]`;
16 changes: 16 additions & 0 deletions packages/gatsby/src/utils/__tests__/get-hash-fn.js
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()
}
})
})