|
| 1 | +import { testPluginOptionsSchema } from "gatsby-plugin-utils" |
| 2 | +import { pluginOptionsSchema } from "../../gatsby-node" |
| 3 | + |
| 4 | +describe(`pluginOptionsSchema`, () => { |
| 5 | + it(`should reject incorrect options`, async () => { |
| 6 | + const options = { |
| 7 | + defaults: { |
| 8 | + formats: [`gif`, `webp`], |
| 9 | + placeholder: `base64`, |
| 10 | + quality: `great`, |
| 11 | + breakpoints: [`mobile`], |
| 12 | + backgroundColor: 0, |
| 13 | + tracedSVGOptions: null, |
| 14 | + transformOptions: false, |
| 15 | + blurredOptions: 1, |
| 16 | + jpgOptions: `none`, |
| 17 | + pngOptions: [{}], |
| 18 | + webpOptions: /a/, |
| 19 | + avifOptions: 1, |
| 20 | + }, |
| 21 | + } |
| 22 | + const { isValid, errors } = await testPluginOptionsSchema( |
| 23 | + pluginOptionsSchema, |
| 24 | + options |
| 25 | + ) |
| 26 | + expect(isValid).toBe(false) |
| 27 | + expect(errors).toEqual([ |
| 28 | + `"defaults.formats[0]" must be one of [auto, png, jpg, webp, avif]`, |
| 29 | + `"defaults.placeholder" must be one of [tracedSVG, dominantColor, blurred, none]`, |
| 30 | + `"defaults.quality" must be a number`, |
| 31 | + `"defaults.breakpoints[0]" must be a number`, |
| 32 | + `"defaults.backgroundColor" must be a string`, |
| 33 | + `"defaults.transformOptions" must be of type object`, |
| 34 | + `"defaults.tracedSVGOptions" must be of type object`, |
| 35 | + `"defaults.blurredOptions" must be of type object`, |
| 36 | + `"defaults.jpgOptions" must be of type object`, |
| 37 | + `"defaults.pngOptions" must be of type object`, |
| 38 | + `"defaults.avifOptions" must be of type object`, |
| 39 | + ]) |
| 40 | + }) |
| 41 | + |
| 42 | + it(`should accept correct options`, async () => { |
| 43 | + const options = { |
| 44 | + defaults: { |
| 45 | + formats: [`auto`, `webp`], |
| 46 | + placeholder: `dominantColor`, |
| 47 | + quality: 50, |
| 48 | + breakpoints: [100, 200], |
| 49 | + backgroundColor: `rebeccapurple`, |
| 50 | + tracedSVGOptions: {}, |
| 51 | + blurredOptions: { quality: 20 }, |
| 52 | + jpgOptions: { quality: 20 }, |
| 53 | + pngOptions: { quality: 20 }, |
| 54 | + webpOptions: { quality: 20 }, |
| 55 | + avifOptions: { quality: 20 }, |
| 56 | + }, |
| 57 | + } |
| 58 | + const { isValid } = await testPluginOptionsSchema( |
| 59 | + pluginOptionsSchema, |
| 60 | + options |
| 61 | + ) |
| 62 | + expect(isValid).toBe(true) |
| 63 | + }) |
| 64 | +}) |
0 commit comments