Skip to content

Commit

Permalink
fix(gatsby-source-graphql): Destructure createContentDigest from firs…
Browse files Browse the repository at this point in the history
…t parameter (#13214)

* 🐛 Destructure createContentDigest from first parameter

* 🚨 Fix lint warning

* test: add a unit test suite to catch these in the future
  • Loading branch information
frinyvonnick authored and DSchau committed Apr 8, 2019
1 parent 9664a8b commit 382fbe1
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`validation throws on missing fieldName 1`] = `"gatsby-source-graphql requires option \`fieldName\` to be specified"`;

exports[`validation throws on missing typename 1`] = `"gatsby-source-graphql requires option \`typeName\` to be specified"`;

exports[`validation throws on missing url 1`] = `"gatsby-source-graphql requires either option \`url\` or \`createLink\` callback"`;
69 changes: 69 additions & 0 deletions packages/gatsby-source-graphql/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
jest.mock(`graphql-tools`, () => {
return {
makeRemoteExecutableSchema: jest.fn(),
transformSchema: jest.fn(),
introspectSchema: jest.fn(),
RenameTypes: jest.fn(),
}
})
jest.mock(`graphql`, () => {
const graphql = jest.requireActual(`graphql`)
return {
...graphql,
buildSchema: jest.fn(),
printSchema: jest.fn(),
}
})
const { sourceNodes } = require(`../gatsby-node`)

const getInternalGatsbyAPI = () => {
const actions = {
addThirdPartySchema: jest.fn(),
createPageDependency: jest.fn(),
createNode: jest.fn(),
}

return {
actions,
cache: {
get: jest.fn(),
set: jest.fn(),
},
createContentDigest: jest.fn(),
createNodeId: jest.fn(),
}
}

describe(`validation`, () => {
;[
[
`throws on missing typename`,
{ fieldName: `github`, url: `https://github.com` },
],
[
`throws on missing fieldName`,
{ typeName: `Github`, url: `https://github.com` },
],
[`throws on missing url`, { typeName: `Github`, fieldName: `github` }],
].forEach(([testName, pluginOptions]) => {
it(testName, () => {
expect(
sourceNodes(getInternalGatsbyAPI(), pluginOptions)
).rejects.toThrowErrorMatchingSnapshot()
})
})
})

describe(`createSchemaNode`, () => {
it(`invokes createContentDigest`, async () => {
const api = getInternalGatsbyAPI()
await sourceNodes(api, {
typeName: `Github`,
fieldName: `github`,
url: `https://github.com`,
})

expect(api.createContentDigest).toHaveBeenCalledWith(expect.any(String))
expect(api.createContentDigest).toHaveBeenCalledTimes(1)
})
})
4 changes: 2 additions & 2 deletions packages/gatsby-source-graphql/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ const {
const { createHttpLink } = require(`apollo-link-http`)
const fetch = require(`node-fetch`)
const invariant = require(`invariant`)

const {
NamespaceUnderFieldTransform,
StripNonQueryTransform,
} = require(`./transforms`)

exports.sourceNodes = async (
{ actions, createNodeId, cache, store },
{ actions, createNodeId, cache, createContentDigest },
options
) => {
const { addThirdPartySchema, createPageDependency, createNode } = actions
Expand All @@ -28,7 +29,6 @@ exports.sourceNodes = async (
createLink,
createSchema,
refetchInterval,
createContentDigest,
} = options

invariant(
Expand Down

0 comments on commit 382fbe1

Please sign in to comment.