Skip to content

Commit

Permalink
Begin stubbing out extend-node.js tests
Browse files Browse the repository at this point in the history
Created a basic framework for creating a markdown node via the
onCreateNode function. This should be expanded to factor in the changes
that occur in the setFieldsOnGraphQLNodeType function.
  • Loading branch information
reccanti committed Nov 19, 2017
1 parent 21e1a84 commit eafdab8
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions packages/gatsby-transformer-remark/src/__tests__/extend-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
const _ = require(`lodash`)
const {
graphql,
GraphQLObjectType,
GraphQLList,
GraphQLSchema,
} = require(`graphql`)
const { onCreateNode } = require(`../gatsby-node`)
const { inferObjectStructureFromNodes } = require(`../../../gatsby/src/schema/infer-graphql-type`)

// given a set of nodes and a query, return the result of the query
async function queryResult(nodes, fragment, { types = [] } = {}) {
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: `RootQueryType`,
fields: () => {
return {
listNode: {
name: `LISTNODE`,
type: new GraphQLList(
new GraphQLObjectType({
name: `MarkdownRemark`,
fields: inferObjectStructureFromNodes({
nodes,
types: [...types],
}),
})
),
resolve() {
return nodes
},
},
}
},
}),
})

const result = await graphql(
schema,
`query {
listNode {
${fragment}
}
}
`,
)
return result
}

describe(`Excerpt is generated correctly from schema`, () => {

const node = {
id: `whatever`,
children: [],
internal: {
contentDigest: `whatever`,
mediaType: `text/markdown`,
},
}

// Make some fake functions its expecting.
const loadNodeContent = node => Promise.resolve(node.content)


it(`correctly loads a default excerpt`, async () => {
const content = `---
title: "my little pony"
date: "2017-09-18T23:19:51.246Z"
---
Where oh where is my little pony?
`

node.content = content

const createNode = markdownNode => {
queryResult([ markdownNode ],
`
excerpt
frontmatter {
title
}
`,
{
types: [ { name: `MarkdownRemark` } ],
}).then(result => {
expect(_.isString(result.data.listNode.excerpt))
})
}
const createParentChildLink = jest.fn()
const boundActionCreators = { createNode, createParentChildLink }

await onCreateNode({
node,
loadNodeContent,
boundActionCreators,
})
})

})

0 comments on commit eafdab8

Please sign in to comment.