Skip to content

Commit 2d967cb

Browse files
tests: Update pluginOptionsSchema tests (#27904)
Co-authored-by: LekoArts <[email protected]>
1 parent 2bd523a commit 2d967cb

File tree

15 files changed

+448
-355
lines changed

15 files changed

+448
-355
lines changed

docs/docs/how-to/plugins-and-themes/configuring-usage-with-plugin-options.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -249,17 +249,19 @@ describe(`pluginOptionsSchema`, () => {
249249
message: 123, // Should be a string
250250
optionB: `not a boolean`, // Should be a boolean
251251
}
252+
const expectedErrors = [
253+
`"optionA" is required`,
254+
`"message" must be a string`,
255+
`"optionB" must be a boolean`,
256+
]
257+
252258
const { isValid, errors } = await testPluginOptionsSchema(
253259
pluginOptionsSchema,
254260
options
255261
)
256262

257263
expect(isValid).toBe(false)
258-
expect(errors).toEqual([
259-
`"optionA" is required`,
260-
`"message" must be a string`,
261-
`"optionB" must be a boolean`,
262-
])
264+
expect(errors).toEqual(expectedErrors)
263265
})
264266

265267
it(`should validate correct options`, async () => {

packages/gatsby-plugin-cxs/src/__tests__/gatsby-node.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,26 @@ import { pluginOptionsSchema } from "../gatsby-node"
55
it(`should provide meaningful errors when fields are invalid`, async () => {
66
const expectedWarnings = [`"optionA" is not allowed`]
77

8-
const { warnings, isValid, hasWarnings } = await testPluginOptionsSchema(
9-
pluginOptionsSchema,
10-
{
8+
const { warnings, isValid, hasWarnings, errors } =
9+
await testPluginOptionsSchema(pluginOptionsSchema, {
1110
optionA: `This options shouldn't exist`,
12-
}
13-
)
11+
})
1412
expect(isValid).toBe(true)
1513
expect(hasWarnings).toBe(true)
1614
expect(warnings).toEqual(expectedWarnings)
15+
expect(errors).toEqual([])
1716
})
1817

1918
it.each`
2019
options
2120
${undefined}
2221
${{}}
2322
`(`should validate the schema: $options`, async ({ options }) => {
24-
const { isValid } = await testPluginOptionsSchema(
23+
const { isValid, errors } = await testPluginOptionsSchema(
2524
pluginOptionsSchema,
2625
options
2726
)
2827

2928
expect(isValid).toBe(true)
29+
expect(errors).toEqual([])
3030
})

packages/gatsby-plugin-google-tagmanager/src/__tests__/gatsby-node.js

+31-23
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,43 @@ import { testPluginOptionsSchema } from "gatsby-plugin-utils"
33

44
describe(`pluginOptionsSchema`, () => {
55
it(`should validate valid options`, async () => {
6-
const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, {
7-
id: `YOUR_GOOGLE_TAGMANAGER_ID`,
8-
includeInDevelopment: false,
9-
defaultDataLayer: { platform: `gatsby` },
10-
gtmAuth: `YOUR_GOOGLE_TAGMANAGER_ENVIRONMENT_AUTH_STRING`,
11-
gtmPreview: `YOUR_GOOGLE_TAGMANAGER_ENVIRONMENT_PREVIEW_NAME`,
12-
dataLayerName: `YOUR_DATA_LAYER_NAME`,
13-
routeChangeEventName: `YOUR_ROUTE_CHANGE_EVENT_NAME`,
14-
enableWebVitalsTracking: true,
15-
selfHostedOrigin: `YOUR_SELF_HOSTED_ORIGIN`,
16-
})
6+
const { isValid, errors } = await testPluginOptionsSchema(
7+
pluginOptionsSchema,
8+
{
9+
id: `YOUR_GOOGLE_TAGMANAGER_ID`,
10+
includeInDevelopment: false,
11+
defaultDataLayer: { platform: `gatsby` },
12+
gtmAuth: `YOUR_GOOGLE_TAGMANAGER_ENVIRONMENT_AUTH_STRING`,
13+
gtmPreview: `YOUR_GOOGLE_TAGMANAGER_ENVIRONMENT_PREVIEW_NAME`,
14+
dataLayerName: `YOUR_DATA_LAYER_NAME`,
15+
routeChangeEventName: `YOUR_ROUTE_CHANGE_EVENT_NAME`,
16+
enableWebVitalsTracking: true,
17+
selfHostedOrigin: `YOUR_SELF_HOSTED_ORIGIN`,
18+
}
19+
)
1720

1821
expect(isValid).toEqual(true)
22+
expect(errors).toEqual([])
1923
})
2024

2125
it(`should support defaultDataLayer as a function`, async () => {
22-
const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, {
23-
defaultDataLayer: () => {
24-
return {
25-
originalLocation:
26-
document.location.protocol +
27-
`//` +
28-
document.location.hostname +
29-
document.location.pathname +
30-
document.location.search,
31-
}
32-
},
33-
})
26+
const { isValid, errors } = await testPluginOptionsSchema(
27+
pluginOptionsSchema,
28+
{
29+
defaultDataLayer: () => {
30+
return {
31+
originalLocation:
32+
document.location.protocol +
33+
`//` +
34+
document.location.hostname +
35+
document.location.pathname +
36+
document.location.search,
37+
}
38+
},
39+
}
40+
)
3441

3542
expect(isValid).toEqual(true)
43+
expect(errors).toEqual([])
3644
})
3745
})

packages/gatsby-plugin-manifest/src/__tests__/gatsby-node.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -527,11 +527,12 @@ describe(`Test plugin manifest options`, () => {
527527

528528
describe(`pluginOptionsSchema`, () => {
529529
it(`validates options correctly`, async () => {
530-
const { isValid } = await testPluginOptionsSchema(
530+
const { isValid, errors } = await testPluginOptionsSchema(
531531
pluginOptionsSchema,
532532
manifestOptions
533533
)
534534

535535
expect(isValid).toBe(true)
536+
expect(errors).toEqual([])
536537
})
537538
})

packages/gatsby-plugin-offline/src/__tests__/gatsby-node.js

+68-60
Original file line numberDiff line numberDiff line change
@@ -133,74 +133,82 @@ describe(`pluginOptionsSchema`, () => {
133133
`"workboxConfig.clientsClaim" must be a boolean`,
134134
]
135135

136-
const { errors } = await testPluginOptionsSchema(pluginOptionsSchema, {
137-
precachePages: [1, 2, 3],
138-
appendScript: 1223,
139-
debug: `This should be a boolean`,
140-
workboxConfig: {
141-
importWorkboxFrom: 123,
142-
globDirectory: 456,
143-
globPatterns: [1, 2, 3],
144-
modifyURLPrefix: {
145-
"/": 123,
146-
},
147-
cacheId: 123,
148-
dontCacheBustURLsMatching: `This should be a regexp`,
149-
runtimeCaching: [
150-
{
151-
urlPattern: /(\.js$|\.css$|static\/)/,
152-
handler: `Something Invalid`,
136+
const { isValid, errors } = await testPluginOptionsSchema(
137+
pluginOptionsSchema,
138+
{
139+
precachePages: [1, 2, 3],
140+
appendScript: 1223,
141+
debug: `This should be a boolean`,
142+
workboxConfig: {
143+
importWorkboxFrom: 123,
144+
globDirectory: 456,
145+
globPatterns: [1, 2, 3],
146+
modifyURLPrefix: {
147+
"/": 123,
153148
},
154-
2,
155-
3,
156-
],
157-
skipWaiting: `This should be a boolean`,
158-
clientsClaim: `This should be a boolean`,
159-
},
160-
})
149+
cacheId: 123,
150+
dontCacheBustURLsMatching: `This should be a regexp`,
151+
runtimeCaching: [
152+
{
153+
urlPattern: /(\.js$|\.css$|static\/)/,
154+
handler: `Something Invalid`,
155+
},
156+
2,
157+
3,
158+
],
159+
skipWaiting: `This should be a boolean`,
160+
clientsClaim: `This should be a boolean`,
161+
},
162+
}
163+
)
161164

165+
expect(isValid).toBe(false)
162166
expect(errors).toEqual(expectedErrors)
163167
})
164168

165169
it(`should validate the schema`, async () => {
166-
const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, {
167-
precachePages: [`/about-us/`, `/projects/*`],
168-
appendScript: `src/custom-sw-code.js`,
169-
debug: true,
170-
workboxConfig: {
171-
importWorkboxFrom: `local`,
172-
globDirectory: `rootDir`,
173-
globPatterns: [`a`, `b`, `c`],
174-
modifyURLPrefix: {
175-
"/": `pathPrefix/`,
176-
},
177-
cacheId: `gatsby-plugin-offline`,
178-
dontCacheBustURLsMatching: /(\.js$|\.css$|static\/)/,
179-
maximumFileSizeToCacheInBytes: 4800,
180-
runtimeCaching: [
181-
{
182-
urlPattern: /(\.js$|\.css$|static\/)/,
183-
handler: `CacheFirst`,
184-
},
185-
{
186-
urlPattern: /^https?:.*\/page-data\/.*\.json/,
187-
handler: `StaleWhileRevalidate`,
188-
},
189-
{
190-
urlPattern:
191-
/^https?:.*\.(png|jpg|jpeg|webp|svg|gif|tiff|js|woff|woff2|json|css)$/,
192-
handler: `StaleWhileRevalidate`,
193-
},
194-
{
195-
urlPattern: /^https?:\/\/fonts\.googleapis\.com\/css/,
196-
handler: `StaleWhileRevalidate`,
170+
const { isValid, errors } = await testPluginOptionsSchema(
171+
pluginOptionsSchema,
172+
{
173+
precachePages: [`/about-us/`, `/projects/*`],
174+
appendScript: `src/custom-sw-code.js`,
175+
debug: true,
176+
workboxConfig: {
177+
importWorkboxFrom: `local`,
178+
globDirectory: `rootDir`,
179+
globPatterns: [`a`, `b`, `c`],
180+
modifyURLPrefix: {
181+
"/": `pathPrefix/`,
197182
},
198-
],
199-
skipWaiting: true,
200-
clientsClaim: true,
201-
},
202-
})
183+
cacheId: `gatsby-plugin-offline`,
184+
dontCacheBustURLsMatching: /(\.js$|\.css$|static\/)/,
185+
maximumFileSizeToCacheInBytes: 4800,
186+
runtimeCaching: [
187+
{
188+
urlPattern: /(\.js$|\.css$|static\/)/,
189+
handler: `CacheFirst`,
190+
},
191+
{
192+
urlPattern: /^https?:.*\/page-data\/.*\.json/,
193+
handler: `StaleWhileRevalidate`,
194+
},
195+
{
196+
urlPattern:
197+
/^https?:.*\.(png|jpg|jpeg|webp|svg|gif|tiff|js|woff|woff2|json|css)$/,
198+
handler: `StaleWhileRevalidate`,
199+
},
200+
{
201+
urlPattern: /^https?:\/\/fonts\.googleapis\.com\/css/,
202+
handler: `StaleWhileRevalidate`,
203+
},
204+
],
205+
skipWaiting: true,
206+
clientsClaim: true,
207+
},
208+
}
209+
)
203210

204211
expect(isValid).toBe(true)
212+
expect(errors).toEqual([])
205213
})
206214
})

0 commit comments

Comments
 (0)