From 2e18f750658c2b536ad051427a04a22ed1351fda Mon Sep 17 00:00:00 2001 From: Michal Piechowiak Date: Fri, 26 Jan 2018 09:12:15 +0100 Subject: [PATCH] add tests for inferring File from string paths --- .../__tests__/infer-graphql-type-test.js | 99 ++++++++++++++++++- 1 file changed, 97 insertions(+), 2 deletions(-) diff --git a/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js b/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js index 5952717c8b847..8fc2190ce8c27 100644 --- a/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js +++ b/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js @@ -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 = [] } = {}) { @@ -39,7 +42,9 @@ function queryResult(nodes, fragment, { types = [] } = {}) { ${fragment} } } - ` + `, + null, + { path: `/` } ) } @@ -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