/
plugin-options.ts
124 lines (116 loc) · 3.69 KB
/
plugin-options.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { testPluginOptionsSchema } from "gatsby-plugin-utils"
import { pluginOptionsSchema } from "../../gatsby-node"
import { doMergeDefaults, PluginOptionsDefaults } from "../plugin-options"
const defaults: PluginOptionsDefaults = {
formats: [`auto`, `webp`],
placeholder: `dominantColor`,
quality: 50,
breakpoints: [100, 200],
backgroundColor: `rebeccapurple`,
tracedSVGOptions: {},
blurredOptions: { width: 20 },
jpgOptions: { quality: 20 },
pngOptions: { quality: 20 },
webpOptions: { quality: 20 },
avifOptions: { quality: 20 },
}
describe(`pluginOptionsSchema`, () => {
it(`should reject incorrect options`, async () => {
const options = {
defaults: {
formats: [`gif`, `webp`],
placeholder: `base64`,
quality: `great`,
breakpoints: [`mobile`],
backgroundColor: 0,
tracedSVGOptions: null,
transformOptions: false,
blurredOptions: 1,
jpgOptions: `none`,
pngOptions: [{}],
webpOptions: /a/,
avifOptions: 1,
},
}
const { isValid, errors } = await testPluginOptionsSchema(
pluginOptionsSchema,
options
)
expect(isValid).toBe(false)
expect(errors).toEqual([
`"defaults.formats[0]" must be one of [auto, png, jpg, webp, avif]`,
`"defaults.placeholder" must be one of [tracedSVG, dominantColor, blurred, none]`,
`"defaults.quality" must be a number`,
`"defaults.breakpoints[0]" must be a number`,
`"defaults.backgroundColor" must be a string`,
`"defaults.transformOptions" must be of type object`,
`"defaults.tracedSVGOptions" must be of type object`,
`"defaults.blurredOptions" must be of type object`,
`"defaults.jpgOptions" must be of type object`,
`"defaults.pngOptions" must be of type object`,
`"defaults.avifOptions" must be of type object`,
])
})
it(`should accept correct options`, async () => {
const options = { defaults }
const { isValid } = await testPluginOptionsSchema(
pluginOptionsSchema,
options
)
expect(isValid).toBe(true)
})
})
describe(`plugin defaults`, () => {
it(`uses defaults`, () => {
const output = doMergeDefaults({}, defaults)
expect(output).toMatchInlineSnapshot(`
Object {
"avifOptions": Object {
"quality": 20,
},
"backgroundColor": "rebeccapurple",
"blurredOptions": Object {
"width": 20,
},
"breakpoints": Array [
100,
200,
],
"formats": Array [
"auto",
"webp",
],
"jpgOptions": Object {
"quality": 20,
},
"placeholder": "dominantColor",
"pngOptions": Object {
"quality": 20,
},
"quality": 50,
"tracedSVGOptions": Object {},
"webpOptions": Object {
"quality": 20,
},
}
`)
})
it(`allows overrides`, () => {
const output = doMergeDefaults({ backgroundColor: `papayawhip` }, defaults)
expect(output.backgroundColor).toEqual(`papayawhip`)
expect(output.quality).toEqual(50)
})
it(`allows overrides of arrays`, () => {
const output = doMergeDefaults({ formats: [`auto`, `avif`] }, defaults)
expect(output.formats).toEqual([`auto`, `avif`])
expect(output.breakpoints).toEqual([100, 200])
})
it(`allows override of deep objects`, () => {
const output = doMergeDefaults({ avifOptions: { quality: 50 } }, defaults)
expect(output.avifOptions).toEqual({ quality: 50 })
})
it(`allows extra keys in objects`, () => {
const output = doMergeDefaults({ avifOptions: { speed: 50 } }, defaults)
expect(output.avifOptions).toEqual({ quality: 20, speed: 50 })
})
})