|
1 | 1 | import { testPluginOptionsSchema } from "gatsby-plugin-utils"
|
2 | 2 | import { pluginOptionsSchema } from "../../gatsby-node"
|
| 3 | +import { doMergeDefaults } from "../plugin-options" |
| 4 | + |
| 5 | +const defaults = { |
| 6 | + formats: [`auto`, `webp`], |
| 7 | + placeholder: `dominantColor`, |
| 8 | + quality: 50, |
| 9 | + breakpoints: [100, 200], |
| 10 | + backgroundColor: `rebeccapurple`, |
| 11 | + tracedSVGOptions: {}, |
| 12 | + blurredOptions: { quality: 20 }, |
| 13 | + jpgOptions: { quality: 20 }, |
| 14 | + pngOptions: { quality: 20 }, |
| 15 | + webpOptions: { quality: 20 }, |
| 16 | + avifOptions: { quality: 20 }, |
| 17 | +} |
3 | 18 |
|
4 | 19 | describe(`pluginOptionsSchema`, () => {
|
5 | 20 | it(`should reject incorrect options`, async () => {
|
@@ -40,25 +55,70 @@ describe(`pluginOptionsSchema`, () => {
|
40 | 55 | })
|
41 | 56 |
|
42 | 57 | 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 options = { defaults } |
58 | 59 | const { isValid } = await testPluginOptionsSchema(
|
59 | 60 | pluginOptionsSchema,
|
60 | 61 | options
|
61 | 62 | )
|
62 | 63 | expect(isValid).toBe(true)
|
63 | 64 | })
|
64 | 65 | })
|
| 66 | + |
| 67 | +describe(`plugin defaults`, () => { |
| 68 | + it(`uses defaults`, () => { |
| 69 | + const output = doMergeDefaults({}, defaults) |
| 70 | + expect(output).toMatchInlineSnapshot(` |
| 71 | + Object { |
| 72 | + "avifOptions": Object { |
| 73 | + "quality": 20, |
| 74 | + }, |
| 75 | + "backgroundColor": "rebeccapurple", |
| 76 | + "blurredOptions": Object { |
| 77 | + "quality": 20, |
| 78 | + }, |
| 79 | + "breakpoints": Array [ |
| 80 | + 100, |
| 81 | + 200, |
| 82 | + ], |
| 83 | + "formats": Array [ |
| 84 | + "auto", |
| 85 | + "webp", |
| 86 | + ], |
| 87 | + "jpgOptions": Object { |
| 88 | + "quality": 20, |
| 89 | + }, |
| 90 | + "placeholder": "dominantColor", |
| 91 | + "pngOptions": Object { |
| 92 | + "quality": 20, |
| 93 | + }, |
| 94 | + "quality": 50, |
| 95 | + "tracedSVGOptions": Object {}, |
| 96 | + "webpOptions": Object { |
| 97 | + "quality": 20, |
| 98 | + }, |
| 99 | + } |
| 100 | + `) |
| 101 | + }) |
| 102 | + |
| 103 | + it(`allows overrides`, () => { |
| 104 | + const output = doMergeDefaults({ backgroundColor: `papayawhip` }, defaults) |
| 105 | + expect(output.backgroundColor).toEqual(`papayawhip`) |
| 106 | + expect(output.quality).toEqual(50) |
| 107 | + }) |
| 108 | + |
| 109 | + it(`allows overrides of arrays`, () => { |
| 110 | + const output = doMergeDefaults({ formats: [`auto`, `avif`] }, defaults) |
| 111 | + expect(output.formats).toEqual([`auto`, `avif`]) |
| 112 | + expect(output.breakpoints).toEqual([100, 200]) |
| 113 | + }) |
| 114 | + |
| 115 | + it(`allows override of deep objects`, () => { |
| 116 | + const output = doMergeDefaults({ avifOptions: { quality: 50 } }, defaults) |
| 117 | + expect(output.avifOptions).toEqual({ quality: 50 }) |
| 118 | + }) |
| 119 | + |
| 120 | + it(`allows extra keys in objects`, () => { |
| 121 | + const output = doMergeDefaults({ avifOptions: { speed: 50 } }, defaults) |
| 122 | + expect(output.avifOptions).toEqual({ quality: 20, speed: 50 }) |
| 123 | + }) |
| 124 | +}) |
0 commit comments