Skip to content

Commit

Permalink
tests: update pluginOptionsSchema tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDeBoey committed Apr 12, 2021
1 parent 5c3faff commit 57b2428
Show file tree
Hide file tree
Showing 18 changed files with 552 additions and 426 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -246,17 +246,19 @@ describe(`pluginOptionsSchema`, () => {
message: 123, // Should be a string
optionB: `not a boolean`, // Should be a boolean
}
const expectedErrors = [
`"optionA" is required`,
`"message" must be a string`,
`"optionB" must be a boolean`,
]
const { isValid, errors } = await testPluginOptionsSchema(
pluginOptionsSchema,
options
)
expect(isValid).toBe(false)
expect(errors).toEqual([
`"optionA" is required`,
`"message" must be a string`,
`"optionB" must be a boolean`,
])
expect(errors).toEqual(expectedErrors)
})
it(`should validate correct options`, async () => {
Expand Down
13 changes: 9 additions & 4 deletions packages/gatsby-plugin-cxs/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import { pluginOptionsSchema } from "../gatsby-node"
it(`should provide meaningful errors when fields are invalid`, async () => {
const expectedErrors = [`"optionA" is not allowed`]

const { errors } = await testPluginOptionsSchema(pluginOptionsSchema, {
optionA: `This options shouldn't exist`,
})
const { isValid, errors } = await testPluginOptionsSchema(
pluginOptionsSchema,
{
optionA: `This options shouldn't exist`,
}
)

expect(isValid).toBe(false)
expect(errors).toEqual(expectedErrors)
})

Expand All @@ -17,10 +21,11 @@ it.each`
${undefined}
${{}}
`(`should validate the schema: $options`, async ({ options }) => {
const { isValid } = await testPluginOptionsSchema(
const { isValid, errors } = await testPluginOptionsSchema(
pluginOptionsSchema,
options
)

expect(isValid).toBe(true)
expect(errors).toEqual([])
})
13 changes: 9 additions & 4 deletions packages/gatsby-plugin-glamor/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ describe(`pluginOptionsSchema`, () => {
it(`should provide meaningful errors when fields are invalid`, async () => {
const expectedErrors = [`"optionA" is not allowed`]

const { errors } = await testPluginOptionsSchema(pluginOptionsSchema, {
optionA: `This options shouldn't exist`,
})
const { isValid, errors } = await testPluginOptionsSchema(
pluginOptionsSchema,
{
optionA: `This options shouldn't exist`,
}
)

expect(isValid).toBe(false)
expect(errors).toEqual(expectedErrors)
})

Expand All @@ -18,11 +22,12 @@ describe(`pluginOptionsSchema`, () => {
${undefined}
${{}}
`(`should validate the schema: $options`, async ({ options }) => {
const { isValid } = await testPluginOptionsSchema(
const { isValid, errors } = await testPluginOptionsSchema(
pluginOptionsSchema,
options
)

expect(isValid).toBe(true)
expect(errors).toEqual([])
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,41 @@ import { testPluginOptionsSchema } from "gatsby-plugin-utils"

describe(`pluginOptionsSchema`, () => {
it(`should validate valid options`, async () => {
const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, {
id: `YOUR_GOOGLE_TAGMANAGER_ID`,
includeInDevelopment: false,
defaultDataLayer: { platform: `gatsby` },
gtmAuth: `YOUR_GOOGLE_TAGMANAGER_ENVIRONMENT_AUTH_STRING`,
gtmPreview: `YOUR_GOOGLE_TAGMANAGER_ENVIRONMENT_PREVIEW_NAME`,
dataLayerName: `YOUR_DATA_LAYER_NAME`,
routeChangeEventName: `YOUR_ROUTE_CHANGE_EVENT_NAME`,
})
const { isValid, errors } = await testPluginOptionsSchema(
pluginOptionsSchema,
{
id: `YOUR_GOOGLE_TAGMANAGER_ID`,
includeInDevelopment: false,
defaultDataLayer: { platform: `gatsby` },
gtmAuth: `YOUR_GOOGLE_TAGMANAGER_ENVIRONMENT_AUTH_STRING`,
gtmPreview: `YOUR_GOOGLE_TAGMANAGER_ENVIRONMENT_PREVIEW_NAME`,
dataLayerName: `YOUR_DATA_LAYER_NAME`,
routeChangeEventName: `YOUR_ROUTE_CHANGE_EVENT_NAME`,
}
)

expect(isValid).toEqual(true)
expect(errors).toEqual([])
})

it(`should support defaultDataLayer as a function`, async () => {
const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, {
defaultDataLayer: () => {
return {
originalLocation:
document.location.protocol +
`//` +
document.location.hostname +
document.location.pathname +
document.location.search,
}
},
})
const { isValid, errors } = await testPluginOptionsSchema(
pluginOptionsSchema,
{
defaultDataLayer: () => {
return {
originalLocation:
document.location.protocol +
`//` +
document.location.hostname +
document.location.pathname +
document.location.search,
}
},
}
)

expect(isValid).toEqual(true)
expect(errors).toEqual([])
})
})
14 changes: 7 additions & 7 deletions packages/gatsby-plugin-manifest/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,12 +532,12 @@ describe(`Test plugin manifest options`, () => {

describe(`pluginOptionsSchema`, () => {
it(`validates options correctly`, async () => {
expect(await testPluginOptionsSchema(pluginOptionsSchema, manifestOptions))
.toMatchInlineSnapshot(`
Object {
"errors": Array [],
"isValid": true,
}
`)
const { isValid, errors } = await testPluginOptionsSchema(
pluginOptionsSchema,
manifestOptions
)

expect(isValid).toBe(true)
expect(errors).toEqual([])
})
})
86 changes: 47 additions & 39 deletions packages/gatsby-plugin-mdx/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,52 +19,60 @@ describe(`pluginOptionsSchema`, () => {
`"root" must be a string`,
]

const { errors } = await testPluginOptionsSchema(pluginOptionsSchema, {
extensions: [1, 2, 3],
defaultLayouts: `this should be an object`,
gatsbyRemarkPlugins: [1, { not: `existing prop` }, `valid one`],
remarkPlugins: `this should be an array of object`,
rehypePlugins: `this should be an array of object`,
plugins: [2],
mediaTypes: [1, 2],
shouldBlockNodeFromTransformation: (wrong, number) => null,
root: 1,
})
const { isValid, errors } = await testPluginOptionsSchema(
pluginOptionsSchema,
{
extensions: [1, 2, 3],
defaultLayouts: `this should be an object`,
gatsbyRemarkPlugins: [1, { not: `existing prop` }, `valid one`],
remarkPlugins: `this should be an array of object`,
rehypePlugins: `this should be an array of object`,
plugins: [2],
mediaTypes: [1, 2],
shouldBlockNodeFromTransformation: (wrong, number) => null,
root: 1,
}
)

expect(isValid).toBe(false)
expect(errors).toEqual(expectedErrors)
})

it(`should validate the schema`, async () => {
const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, {
extensions: [`.mdx`, `.mdxx`],
defaultLayouts: {
posts: `../post-layout.js`,
default: `../default-layout.js`,
},
gatsbyRemarkPlugins: [
{
resolve: `gatsby-remark-images`,
options: {
maxWidth: 590,
},
const { isValid, errors } = await testPluginOptionsSchema(
pluginOptionsSchema,
{
extensions: [`.mdx`, `.mdxx`],
defaultLayouts: {
posts: `../post-layout.js`,
default: `../default-layout.js`,
},
`gatsby-remark-other-plugin`,
],
lessBabel: false,
remarkPlugins: [
require(`../gatsby-node.js`),
[require(`../gatsby-node.js`), { target: false }],
],
plugins: [{ resolve: `remark-autolink-plugin` }],
rehypePlugins: [
require(`../gatsby-node.js`),
[require(`../gatsby-node.js`), { behavior: `wrap` }],
],
mediaTypes: [`text/markdown`, `text/x-markdown`, `custom-media/type`],
shouldBlockNodeFromTransformation: node => Boolean(node),
root: `james-holden`,
})
gatsbyRemarkPlugins: [
{
resolve: `gatsby-remark-images`,
options: {
maxWidth: 590,
},
},
`gatsby-remark-other-plugin`,
],
lessBabel: false,
remarkPlugins: [
require(`../gatsby-node.js`),
[require(`../gatsby-node.js`), { target: false }],
],
plugins: [{ resolve: `remark-autolink-plugin` }],
rehypePlugins: [
require(`../gatsby-node.js`),
[require(`../gatsby-node.js`), { behavior: `wrap` }],
],
mediaTypes: [`text/markdown`, `text/x-markdown`, `custom-media/type`],
shouldBlockNodeFromTransformation: node => Boolean(node),
root: `james-holden`,
}
)

expect(isValid).toBe(true)
expect(errors).toEqual([])
})
})
50 changes: 29 additions & 21 deletions packages/gatsby-plugin-netlify/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,41 @@ describe(`gatsby-node.js`, () => {
`"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`,
})
const { isValid, 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(isValid).toBe(false)
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,
})
const { isValid, errors } = 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)
expect(errors).toEqual([])
})
})
Loading

0 comments on commit 57b2428

Please # to comment.