Skip to content

Commit

Permalink
tests: Update pluginOptionsSchema tests (#27904)
Browse files Browse the repository at this point in the history
Co-authored-by: LekoArts <[email protected]>
  • Loading branch information
MichaelDeBoey and LekoArts authored Nov 23, 2022
1 parent 2bd523a commit 2d967cb
Show file tree
Hide file tree
Showing 15 changed files with 448 additions and 355 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -249,17 +249,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
12 changes: 6 additions & 6 deletions packages/gatsby-plugin-cxs/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@ import { pluginOptionsSchema } from "../gatsby-node"
it(`should provide meaningful errors when fields are invalid`, async () => {
const expectedWarnings = [`"optionA" is not allowed`]

const { warnings, isValid, hasWarnings } = await testPluginOptionsSchema(
pluginOptionsSchema,
{
const { warnings, isValid, hasWarnings, errors } =
await testPluginOptionsSchema(pluginOptionsSchema, {
optionA: `This options shouldn't exist`,
}
)
})
expect(isValid).toBe(true)
expect(hasWarnings).toBe(true)
expect(warnings).toEqual(expectedWarnings)
expect(errors).toEqual([])
})

it.each`
options
${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,35 +3,43 @@ 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`,
enableWebVitalsTracking: true,
selfHostedOrigin: `YOUR_SELF_HOSTED_ORIGIN`,
})
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`,
enableWebVitalsTracking: true,
selfHostedOrigin: `YOUR_SELF_HOSTED_ORIGIN`,
}
)

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([])
})
})
3 changes: 2 additions & 1 deletion packages/gatsby-plugin-manifest/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,11 +527,12 @@ describe(`Test plugin manifest options`, () => {

describe(`pluginOptionsSchema`, () => {
it(`validates options correctly`, async () => {
const { isValid } = await testPluginOptionsSchema(
const { isValid, errors } = await testPluginOptionsSchema(
pluginOptionsSchema,
manifestOptions
)

expect(isValid).toBe(true)
expect(errors).toEqual([])
})
})
128 changes: 68 additions & 60 deletions packages/gatsby-plugin-offline/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,74 +133,82 @@ describe(`pluginOptionsSchema`, () => {
`"workboxConfig.clientsClaim" must be a boolean`,
]

const { errors } = await testPluginOptionsSchema(pluginOptionsSchema, {
precachePages: [1, 2, 3],
appendScript: 1223,
debug: `This should be a boolean`,
workboxConfig: {
importWorkboxFrom: 123,
globDirectory: 456,
globPatterns: [1, 2, 3],
modifyURLPrefix: {
"/": 123,
},
cacheId: 123,
dontCacheBustURLsMatching: `This should be a regexp`,
runtimeCaching: [
{
urlPattern: /(\.js$|\.css$|static\/)/,
handler: `Something Invalid`,
const { isValid, errors } = await testPluginOptionsSchema(
pluginOptionsSchema,
{
precachePages: [1, 2, 3],
appendScript: 1223,
debug: `This should be a boolean`,
workboxConfig: {
importWorkboxFrom: 123,
globDirectory: 456,
globPatterns: [1, 2, 3],
modifyURLPrefix: {
"/": 123,
},
2,
3,
],
skipWaiting: `This should be a boolean`,
clientsClaim: `This should be a boolean`,
},
})
cacheId: 123,
dontCacheBustURLsMatching: `This should be a regexp`,
runtimeCaching: [
{
urlPattern: /(\.js$|\.css$|static\/)/,
handler: `Something Invalid`,
},
2,
3,
],
skipWaiting: `This should be a boolean`,
clientsClaim: `This should be a boolean`,
},
}
)

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

it(`should validate the schema`, async () => {
const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, {
precachePages: [`/about-us/`, `/projects/*`],
appendScript: `src/custom-sw-code.js`,
debug: true,
workboxConfig: {
importWorkboxFrom: `local`,
globDirectory: `rootDir`,
globPatterns: [`a`, `b`, `c`],
modifyURLPrefix: {
"/": `pathPrefix/`,
},
cacheId: `gatsby-plugin-offline`,
dontCacheBustURLsMatching: /(\.js$|\.css$|static\/)/,
maximumFileSizeToCacheInBytes: 4800,
runtimeCaching: [
{
urlPattern: /(\.js$|\.css$|static\/)/,
handler: `CacheFirst`,
},
{
urlPattern: /^https?:.*\/page-data\/.*\.json/,
handler: `StaleWhileRevalidate`,
},
{
urlPattern:
/^https?:.*\.(png|jpg|jpeg|webp|svg|gif|tiff|js|woff|woff2|json|css)$/,
handler: `StaleWhileRevalidate`,
},
{
urlPattern: /^https?:\/\/fonts\.googleapis\.com\/css/,
handler: `StaleWhileRevalidate`,
const { isValid, errors } = await testPluginOptionsSchema(
pluginOptionsSchema,
{
precachePages: [`/about-us/`, `/projects/*`],
appendScript: `src/custom-sw-code.js`,
debug: true,
workboxConfig: {
importWorkboxFrom: `local`,
globDirectory: `rootDir`,
globPatterns: [`a`, `b`, `c`],
modifyURLPrefix: {
"/": `pathPrefix/`,
},
],
skipWaiting: true,
clientsClaim: true,
},
})
cacheId: `gatsby-plugin-offline`,
dontCacheBustURLsMatching: /(\.js$|\.css$|static\/)/,
maximumFileSizeToCacheInBytes: 4800,
runtimeCaching: [
{
urlPattern: /(\.js$|\.css$|static\/)/,
handler: `CacheFirst`,
},
{
urlPattern: /^https?:.*\/page-data\/.*\.json/,
handler: `StaleWhileRevalidate`,
},
{
urlPattern:
/^https?:.*\.(png|jpg|jpeg|webp|svg|gif|tiff|js|woff|woff2|json|css)$/,
handler: `StaleWhileRevalidate`,
},
{
urlPattern: /^https?:\/\/fonts\.googleapis\.com\/css/,
handler: `StaleWhileRevalidate`,
},
],
skipWaiting: true,
clientsClaim: true,
},
}
)

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

0 comments on commit 2d967cb

Please sign in to comment.