Skip to content

test: Add test case for the creation of redirects for SSR and DSG pages. #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 29, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 106 additions & 35 deletions src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,117 @@
import { testPluginOptionsSchema } from "gatsby-plugin-utils"
jest.mock('../plugin-data', () => {
return {
__esModule: true,
default: jest.fn().mockReturnValue({
publicFolder: jest.fn().mockReturnValue('mock-file-path'),
}),
}
})
jest.mock('../build-headers-program', () => {
return {
__esModule: true,
default: jest.fn(),
}
})
jest.mock('fs-extra', () => {
return {
__esModule: true,
default: jest.fn(),
existsSync: jest.fn(),
readFile: jest.fn(),
writeFile: jest.fn(),
}
})

import { pluginOptionsSchema } from "../gatsby-node"
// Importing writeFile here gives us access to the mocked method to assert the correct content is written to the file within test cases
import { writeFile } from 'fs-extra'
import { testPluginOptionsSchema } from 'gatsby-plugin-utils'
import { pluginOptionsSchema, onPostBuild } from '../gatsby-node'

describe(`gatsby-node.js`, () => {
it(`should provide meaningful errors when fields are invalid`, async () => {
const expectedErrors = [
`"headers" must be of type object`,
`"allPageHeaders" must be an array`,
`"mergeSecurityHeaders" must be a boolean`,
`"mergeLinkHeaders" must be a boolean`,
`"mergeCachingHeaders" must be a boolean`,
`"transformHeaders" must have an arity lesser or equal to 2`,
`"generateMatchPathRewrites" must be a boolean`,
]

const { errors } = await testPluginOptionsSchema(pluginOptionsSchema, {
headers: `this should be an object`,
allPageHeaders: `this should be an array`,
mergeSecurityHeaders: `this should be a boolean`,
mergeLinkHeaders: `this should be a boolean`,
mergeCachingHeaders: `this should be a boolean`,
transformHeaders: (too, many, args) => ``,
generateMatchPathRewrites: `this should be a boolean`,
describe('testPluginOptionsSchema', () => {
it(`should provide meaningful errors when fields are invalid`, async () => {
const expectedErrors = [
`"headers" must be of type object`,
`"allPageHeaders" must be an array`,
`"mergeSecurityHeaders" must be a boolean`,
`"mergeLinkHeaders" must be a boolean`,
`"mergeCachingHeaders" must be a boolean`,
`"transformHeaders" must have an arity lesser or equal to 2`,
`"generateMatchPathRewrites" must be a boolean`,
]

const { errors } = await testPluginOptionsSchema(pluginOptionsSchema, {
headers: `this should be an object`,
allPageHeaders: `this should be an array`,
mergeSecurityHeaders: `this should be a boolean`,
mergeLinkHeaders: `this should be a boolean`,
mergeCachingHeaders: `this should be a boolean`,
transformHeaders: (too, many, args) => ``,
generateMatchPathRewrites: `this should be a boolean`,
})

expect(errors).toEqual(expectedErrors)
})

expect(errors).toEqual(expectedErrors)
it(`should validate the schema`, async () => {
const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, {
headers: {
'/some-page': [`Bearer: Some-Magic-Token`],
'/some-other-page': [`some`, `great`, `headers`],
},
allPageHeaders: [`First header`, `Second header`],
mergeSecurityHeaders: true,
mergeLinkHeaders: false,
mergeCachingHeaders: true,
transformHeaders: () => null,
generateMatchPathRewrites: false,
})

expect(isValid).toBe(true)
})
})

it(`should validate the schema`, async () => {
const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, {
headers: {
"/some-page": [`Bearer: Some-Magic-Token`],
"/some-other-page": [`some`, `great`, `headers`],
},
allPageHeaders: [`First header`, `Second header`],
mergeSecurityHeaders: true,
mergeLinkHeaders: false,
mergeCachingHeaders: true,
transformHeaders: () => null,
generateMatchPathRewrites: false,
describe('onPostBuild', () => {
let store = {}
const reporter = {
info: jest.fn(),
}
beforeEach(() => {
store = {
getState: jest.fn().mockReturnValue({
redirects: [],
pages: [
{
mode: 'SSR',
path: 'some/path',
},
{
mode: 'DSG',
path: 'some/other/path',
},
],
functions: [],
program: { directory: '' },
}),
}
})

afterEach(() => {
jest.resetAllMocks()
})

expect(isValid).toBe(true)
it('creates redirects for SSR and DSG pages in format Netlify expects', async () => {
const expectedValue = [
'',
'## Created with gatsby-plugin-netlify',
'some/path /.netlify/functions/__ssr 200',
'/page-data/some/path/page-data.json /.netlify/functions/__ssr 200',
'some/other/path /.netlify/functions/__dsg 200',
'/page-data/some/other/path/page-data.json /.netlify/functions/__dsg 200',
].join(`\n`)

await onPostBuild({ store, pathPrefix: '', reporter }, {})
expect(writeFile).toHaveBeenCalledWith('mock-file-path', expectedValue)
})
})
})