From 7ee5fe65c9b6c231080893b2d500c1acf7e4938e Mon Sep 17 00:00:00 2001 From: Sidhartha Chatterjee Date: Wed, 29 Jul 2020 15:11:53 +0530 Subject: [PATCH 1/2] Load resources in ProdPageRenderer --- packages/gatsby/cache-dir/public-page-renderer-prod.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/gatsby/cache-dir/public-page-renderer-prod.js b/packages/gatsby/cache-dir/public-page-renderer-prod.js index 2ab693856ddf3..7ba651e5f9e81 100644 --- a/packages/gatsby/cache-dir/public-page-renderer-prod.js +++ b/packages/gatsby/cache-dir/public-page-renderer-prod.js @@ -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 } From 7db0d25b838013ccc27f667465c70ba40cd681d0 Mon Sep 17 00:00:00 2001 From: Sidhartha Chatterjee Date: Wed, 29 Jul 2020 16:22:24 +0530 Subject: [PATCH 2/2] Add simple unit tests --- .../__tests__/public-page-renderer-dev.tsx | 42 +++++++++++++++++++ .../__tests__/public-page-renderer-prod.tsx | 38 +++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 packages/gatsby/cache-dir/__tests__/public-page-renderer-dev.tsx create mode 100644 packages/gatsby/cache-dir/__tests__/public-page-renderer-prod.tsx diff --git a/packages/gatsby/cache-dir/__tests__/public-page-renderer-dev.tsx b/packages/gatsby/cache-dir/__tests__/public-page-renderer-dev.tsx new file mode 100644 index 0000000000000..6df3ff0521645 --- /dev/null +++ b/packages/gatsby/cache-dir/__tests__/public-page-renderer-dev.tsx @@ -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() + + expect(loader.loadPageSync).toHaveBeenCalled() + }) +}) + +afterAll(cleanup) + +afterAll(jest.clearAllMocks) diff --git a/packages/gatsby/cache-dir/__tests__/public-page-renderer-prod.tsx b/packages/gatsby/cache-dir/__tests__/public-page-renderer-prod.tsx new file mode 100644 index 0000000000000..0d9866a81d0f7 --- /dev/null +++ b/packages/gatsby/cache-dir/__tests__/public-page-renderer-prod.tsx @@ -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() + + expect(loader.loadPageSync).toHaveBeenCalled() + }) +}) + +afterAll(cleanup) + +afterAll(jest.clearAllMocks)