Skip to content

Commit

Permalink
chore: add tests to static-query filepaths (#25978)
Browse files Browse the repository at this point in the history
* chore: add tests to static-query filepaths

* fix windows paths
  • Loading branch information
wardpeet authored Jul 24, 2020
1 parent 60492e1 commit 5e78b4f
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import path from "path"
import { slash } from "gatsby-core-utils"
import mapTemplatesToStaticQueryHashes from "../map-templates-to-static-query-hashes"

const page1 = slash(`${process.cwd()}/src/pages/index.js`)
const page2 = slash(`${process.cwd()}/src/pages/pages-2.js`)
const components = new Map()
components.set(page1, {
componentPath: page1,
query: ``,
pages: new Set([`/`]),
})

components.set(page2, {
componentPath: page2,
query: ``,
pages: new Set([`/`]),
})

const staticQueryComponents = new Map()
staticQueryComponents.set(`sq--src-components-image-js`, {
id: `sq--src-components-image-js`,
componentPath: slash(`${process.cwd()}/src/components/image.js`),
query: `query { placeholderImage: file(relativePath: {eq: "gatsby-astronaut.png"}) { public { publicUrl } } }`,
hash: 1,
})
staticQueryComponents.set(`sq--src-components-seo-js`, {
id: `sq--src-components-seo-js`,
componentPath: slash(`${process.cwd()}/src/components/seo.js`),
query: `query { site { siteMetadata { title } } }`,
hash: 2,
})

const createModule = (resource, reasons = []) => {
return {
hasReasons: () => !!reasons.length,
identifier: () => resource,
resource: path.join(process.cwd(), resource),
reasons: reasons.map(r => {
return {
module: r,
}
}),
}
}

describe(`map-templates-to-static-query-hashes`, () => {
it(`should map static-queries to a component file on all platforms`, () => {
const asyncRequires = createModule(
`_this_is_virtual_fs_path_/$virtual/async-requires.js`
)

const templateMap = mapTemplatesToStaticQueryHashes(
{
components,
staticQueryComponents,
},
{
modules: [
createModule(`src/components/layout.js`, [
createModule(`src/pages/index.js`, [asyncRequires]),
createModule(`src/pages/pages-2.js`, [asyncRequires]),
]),
createModule(`src/components/image.js`, [
createModule(`src/pages/index.js`, [asyncRequires]),
]),
createModule(`src/components/seo.js`, [
createModule(`src/pages/pages-2.js`, [asyncRequires]),
]),
],
}
)

expect(templateMap.size).toBe(2)
expect(templateMap.get(page1)).toEqual([`1`])
expect(templateMap.get(page2)).toEqual([`2`])
})
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { uniqBy, List } from "lodash"
import path from "path"
import { slash } from "gatsby-core-utils"
import { IGatsbyState } from "../redux/types"
import { Stats } from "webpack"

Expand Down Expand Up @@ -107,11 +109,11 @@ export default function mapTemplatesToStaticQueryHashes(

for (const uniqDependent of uniqDependents) {
if (uniqDependent.resource) {
result.add(uniqDependent.resource)
result.add(slash(uniqDependent.resource))
// Queries used in gatsby-browser are global and should be added to all pages
if (isGatsbyBrowser(uniqDependent)) {
if (staticQueryModuleComponentPath) {
globalStaticQueries.add(staticQueryModuleComponentPath)
globalStaticQueries.add(slash(staticQueryModuleComponentPath))
}
} else {
seen.add(uniqDependent.resource)
Expand All @@ -131,10 +133,12 @@ export default function mapTemplatesToStaticQueryHashes(

// For every known static query, we get its dependents.
staticQueryComponents.forEach(({ componentPath }) => {
// componentPaths are slashed by gatsby-core-utils we undo it
const nonSlashedPath = path.resolve(componentPath)

const staticQueryComponentModule = modules.find(
m => m.resource === componentPath
m => m.resource === nonSlashedPath
)

const dependants = staticQueryComponentModule
? getDeps(staticQueryComponentModule)
: new Set()
Expand Down

0 comments on commit 5e78b4f

Please sign in to comment.