Skip to content

Commit

Permalink
add tests for inferring File from string paths
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh committed Jan 28, 2018
1 parent 5bb3da5 commit 2e18f75
Showing 1 changed file with 97 additions and 2 deletions.
99 changes: 97 additions & 2 deletions packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const {
GraphQLList,
GraphQLSchema,
} = require(`graphql`)
const path = require(`path`)
const normalizePath = require(`normalize-path`)

const { inferObjectStructureFromNodes } = require(`../infer-graphql-type`)

function queryResult(nodes, fragment, { types = [] } = {}) {
Expand Down Expand Up @@ -39,7 +42,9 @@ function queryResult(nodes, fragment, { types = [] } = {}) {
${fragment}
}
}
`
`,
null,
{ path: `/` }
)
}

Expand Down Expand Up @@ -207,7 +212,97 @@ describe(`GraphQL type inferance`, () => {
})

xdescribe(`Linked inference from config mappings`)
xdescribe(`Linked inference from file URIs`)

describe(`Linked inference from file URIs`, () => {
let store, types, dir

beforeEach(() => {
;({ store } = require(`../../redux`))

const { setFileNodeRootType } = require(`../types/type-file`)
const fileType = {
name: `File`,
nodeObjectType: new GraphQLObjectType({
name: `File`,
fields: inferObjectStructureFromNodes({
nodes: [{ id: `file_1`, absolutePath: `path`, dir: `path` }],
types: [{ name: `File` }],
}),
}),
}

types = [fileType]
setFileNodeRootType(fileType.nodeObjectType)

dir = normalizePath(path.resolve(`/path/`))

store.dispatch({
type: `CREATE_NODE`,
payload: {
id: `parent`,
internal: { type: `File` },
absolutePath: normalizePath(path.resolve(dir, `index.md`)),
dir: dir,
},
})
store.dispatch({
type: `CREATE_NODE`,
payload: {
id: `file_1`,
internal: { type: `File` },
absolutePath: normalizePath(path.resolve(dir, `file_1.jpg`)),
dir,
},
})
store.dispatch({
type: `CREATE_NODE`,
payload: {
id: `file_2`,
internal: { type: `File` },
absolutePath: normalizePath(path.resolve(dir, `file_2.txt`)),
dir,
},
})
})

it(`Links to file node`, async () => {
let result = await queryResult(
[{ file: `./file_1.jpg`, parent: `parent` }],
`
file {
absolutePath
}
`,
{ types }
)

expect(result.errors).not.toBeDefined()
expect(result.data.listNode[0].file.absolutePath).toEqual(
normalizePath(path.resolve(dir, `file_1.jpg`))
)
})

it(`Links to array of file nodes`, async () => {
let result = await queryResult(
[{ files: [`./file_1.jpg`, `./file_2.txt`], parent: `parent` }],
`
files {
absolutePath
}
`,
{ types }
)

expect(result.errors).not.toBeDefined()
expect(result.data.listNode[0].files.length).toEqual(2)
expect(result.data.listNode[0].files[0].absolutePath).toEqual(
normalizePath(path.resolve(dir, `file_1.jpg`))
)
expect(result.data.listNode[0].files[1].absolutePath).toEqual(
normalizePath(path.resolve(dir, `file_2.txt`))
)
})
})

describe(`Linked inference by __NODE convention`, () => {
let store, types
Expand Down

0 comments on commit 2e18f75

Please sign in to comment.