Skip to content

Commit

Permalink
tests(artifacts): add basic cases for query modules
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh committed Jul 29, 2020
1 parent 4a9b658 commit 62ab61f
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { spawn } = require(`child_process`)
const { spawn, spawnSync } = require(`child_process`)
const path = require(`path`)
const { murmurhash } = require(`babel-plugin-remove-graphql-queries`)
const { readPageData } = require(`gatsby/dist/utils/page-data`)
Expand Down Expand Up @@ -58,21 +58,25 @@ function hashQuery(query) {

const globalQueries = [githubQuery, moreInfoQuery]

describe(`Static Queries`, () => {
beforeAll(async done => {
const gatsbyProcess = spawn(gatsbyBin, [`build`], {
stdio: [`inherit`, `inherit`, `inherit`, `inherit`],
env: {
...process.env,
NODE_ENV: `production`,
},
})
beforeAll(async done => {
spawnSync(gatsbyBin, [`clean`], {
stdio: [`inherit`, `inherit`, `inherit`, `inherit`],
})

const gatsbyProcess = spawn(gatsbyBin, [`build`], {
stdio: [`inherit`, `inherit`, `inherit`, `inherit`],
env: {
...process.env,
NODE_ENV: `production`,
},
})

gatsbyProcess.on(`exit`, exitCode => {
done()
})
gatsbyProcess.on(`exit`, exitCode => {
done()
})
})

describe(`Static Queries`, () => {
test(`are written correctly when inline`, async () => {
const queries = [titleQuery, ...globalQueries]
const pagePath = `/inline/`
Expand Down Expand Up @@ -181,3 +185,17 @@ describe(`Static Queries`, () => {
expect(staticQueryHashes.sort()).toEqual(queries.map(hashQuery).sort())
})
})

describe(`Modules`, () => {
test(`are written correctly when inline`, async () => {
const pagePath = `/page-query-modules/`

const { moduleDependencies } = await readPageData(publicDir, pagePath)

expect(moduleDependencies).toMatchInlineSnapshot(`
Array [
"module---src-query-modules-module-a-js-default-",
]
`)
})
})
21 changes: 21 additions & 0 deletions integration-tests/artifacts/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const path = require(`path`)

exports.createResolvers = ({ createResolvers }) => {
createResolvers({
Query: {
queryModule: {
type: `String`,
args: {
moduleFileName: {
type: `String`,
},
},
resolve: (source, args, context) => {
return context.pageModel.setModule({
source: path.resolve(`./src/query-modules/${args.moduleFileName}`),
})
},
},
},
})
}
13 changes: 13 additions & 0 deletions integration-tests/artifacts/src/pages/page-query-modules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from "react"
import { graphql, getModule } from "gatsby"

export default function PageQueryModules({ data }) {
const Component = getModule(data.queryModule)
return <Component />
}

export const pageQuery = graphql`
{
queryModule(moduleFileName: "module-a.js")
}
`
13 changes: 13 additions & 0 deletions integration-tests/artifacts/src/pages/static-query-modules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from "react"
import { useStaticQuery, graphql, getModule } from "gatsby"

export default function StaticQueryModules() {
const { queryModule } = useStaticQuery(graphql`
{
queryModule(moduleFileName: "module-b.js")
}
`)

const Component = getModule(queryModule)
return <Component />
}
5 changes: 5 additions & 0 deletions integration-tests/artifacts/src/query-modules/module-a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from "react"

export default function ModuleAComponent() {
return <div>This is module "A"</div>
}
5 changes: 5 additions & 0 deletions integration-tests/artifacts/src/query-modules/module-b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from "react"

export default function ModuleBComponent() {
return <div>This is module "B"</div>
}

0 comments on commit 62ab61f

Please sign in to comment.