-
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.
Begin stubbing out extend-node.js tests
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
Showing
1 changed file
with
100 additions
and
0 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
packages/gatsby-transformer-remark/src/__tests__/extend-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,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, | ||
}) | ||
}) | ||
|
||
}) | ||
|