From 382fbe172c2ecdbd2588cb222f048fc4b0e9c3e7 Mon Sep 17 00:00:00 2001 From: FRIN Yvonnick Date: Mon, 8 Apr 2019 20:41:19 +0200 Subject: [PATCH] fix(gatsby-source-graphql): Destructure createContentDigest from first parameter (#13214) * :bug: Destructure createContentDigest from first parameter * :rotating_light: Fix lint warning * test: add a unit test suite to catch these in the future --- .../__snapshots__/gatsby-node.js.snap | 7 ++ .../src/__tests__/gatsby-node.js | 69 +++++++++++++++++++ .../gatsby-source-graphql/src/gatsby-node.js | 4 +- 3 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 packages/gatsby-source-graphql/src/__tests__/__snapshots__/gatsby-node.js.snap create mode 100644 packages/gatsby-source-graphql/src/__tests__/gatsby-node.js diff --git a/packages/gatsby-source-graphql/src/__tests__/__snapshots__/gatsby-node.js.snap b/packages/gatsby-source-graphql/src/__tests__/__snapshots__/gatsby-node.js.snap new file mode 100644 index 0000000000000..cae0f68a0bcc0 --- /dev/null +++ b/packages/gatsby-source-graphql/src/__tests__/__snapshots__/gatsby-node.js.snap @@ -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"`; diff --git a/packages/gatsby-source-graphql/src/__tests__/gatsby-node.js b/packages/gatsby-source-graphql/src/__tests__/gatsby-node.js new file mode 100644 index 0000000000000..08ee189b86219 --- /dev/null +++ b/packages/gatsby-source-graphql/src/__tests__/gatsby-node.js @@ -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) + }) +}) diff --git a/packages/gatsby-source-graphql/src/gatsby-node.js b/packages/gatsby-source-graphql/src/gatsby-node.js index b7fdec499d1b1..27ab42af25b68 100644 --- a/packages/gatsby-source-graphql/src/gatsby-node.js +++ b/packages/gatsby-source-graphql/src/gatsby-node.js @@ -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 @@ -28,7 +29,6 @@ exports.sourceNodes = async ( createLink, createSchema, refetchInterval, - createContentDigest, } = options invariant(