Skip to content

Commit

Permalink
fix(gatsby): Load resources in ProdPageRenderer (#26092)
Browse files Browse the repository at this point in the history
* Load resources in ProdPageRenderer

* Add simple unit tests
  • Loading branch information
sidharthachatterjee authored and KyleAMathews committed Jul 29, 2020
1 parent 47ba295 commit 1b0f7a9
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
42 changes: 42 additions & 0 deletions packages/gatsby/cache-dir/__tests__/public-page-renderer-dev.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import React from "react"
import { render, cleanup } from "@testing-library/react"

jest.mock(`../loader`, () => {
return {
loadPageSync: jest.fn((path: string): {
loadPageSync: boolean
path: string
} => {
return { loadPageSync: true, path }
}),
loadPage: function loadPage(
path: string
): Promise<{ loadPage: boolean; path: string }> {
return Promise.resolve({ loadPage: true, path })
},
}
})

jest.mock(`../query-result-store`, () => {
return {
PageQueryStore: (): string => `PageQueryStore`,
}
})

import DevPageRenderer from "../public-page-renderer-dev"
import loader from "../loader"

describe(`DevPageRenderer`, () => {
it(`loads pages synchronously`, () => {
const location = {
pathname: `/`,
}
render(<DevPageRenderer location={location} />)

expect(loader.loadPageSync).toHaveBeenCalled()
})
})

afterAll(cleanup)

afterAll(jest.clearAllMocks)
38 changes: 38 additions & 0 deletions packages/gatsby/cache-dir/__tests__/public-page-renderer-prod.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React from "react"
import { render, cleanup } from "@testing-library/react"

jest.mock(`../loader`, () => {
return {
loadPageSync: jest.fn((path: string): {
loadPageSync: boolean
path: string
} => {
return { loadPageSync: true, path }
}),
loadPage: function loadPage(
path: string
): Promise<{ loadPage: boolean; path: string }> {
return Promise.resolve({ loadPage: true, path })
},
}
})

jest.mock(`../page-renderer`, (): string => `InternalPageRenderer`)

import ProdPageRenderer from "../public-page-renderer-prod"
import loader from "../loader"

describe(`ProdPageRenderer`, () => {
it(`loads pages synchronously`, () => {
const location = {
pathname: `/`,
}
render(<ProdPageRenderer location={location} />)

expect(loader.loadPageSync).toHaveBeenCalled()
})
})

afterAll(cleanup)

afterAll(jest.clearAllMocks)
4 changes: 3 additions & 1 deletion packages/gatsby/cache-dir/public-page-renderer-prod.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import React from "react"
import PropTypes from "prop-types"

import loader from "./loader"
import InternalPageRenderer from "./page-renderer"

const ProdPageRenderer = ({ location, pageResources }) => {
const ProdPageRenderer = ({ location }) => {
const pageResources = loader.loadPageSync(location.pathname)
if (!pageResources) {
return null
}
Expand Down

0 comments on commit 1b0f7a9

Please sign in to comment.