|
| 1 | +import { testPluginOptionsSchema } from "gatsby-plugin-utils" |
| 2 | +import { pluginOptionsSchema } from "../gatsby-node" |
| 3 | + |
| 4 | +describe(`pluginOptionsSchema`, () => { |
| 5 | + it(`should provide meaningful errors when fields are invalid`, () => { |
| 6 | + const expectedErrors = [ |
| 7 | + `"extensions" "[0]" must be a string. "[1]" must be a string. "[2]" must be a string`, |
| 8 | + `"defaultLayout" must be of type object`, |
| 9 | + `"gatsbyRemarkPlugins" "[0]" does not match any of the allowed types. "[1]" does not match any of the allowed types`, |
| 10 | + `"remarkPlugins" must be an array`, |
| 11 | + `"rehypePlugins" must be an array`, |
| 12 | + `"mediaTypes" "[0]" must be a string. "[1]" must be a string`, |
| 13 | + `"shouldBlockNodeFromTransformation" must have an arity lesser or equal to 1`, |
| 14 | + ] |
| 15 | + |
| 16 | + const { errors } = testPluginOptionsSchema(pluginOptionsSchema, { |
| 17 | + extensions: [1, 2, 3], |
| 18 | + defaultLayout: `this should be an object`, |
| 19 | + gatsbyRemarkPlugins: [1, { not: `existing prop` }, `valid one`], |
| 20 | + remarkPlugins: `this should be an array of object`, |
| 21 | + rehypePlugins: `this should be an array of object`, |
| 22 | + mediaTypes: [1, 2], |
| 23 | + shouldBlockNodeFromTransformation: (wrong, number) => null, |
| 24 | + }) |
| 25 | + |
| 26 | + expect(errors).toEqual(expectedErrors) |
| 27 | + }) |
| 28 | + |
| 29 | + it(`should validate the schema`, () => { |
| 30 | + const { isValid } = testPluginOptionsSchema(pluginOptionsSchema, { |
| 31 | + extensions: [`.mdx`, `.mdxx`], |
| 32 | + defaultLayout: { |
| 33 | + posts: `../post-layout.js`, |
| 34 | + default: `../default-layout.js`, |
| 35 | + }, |
| 36 | + gatsbyRemarkPlugins: [ |
| 37 | + { |
| 38 | + resolve: `gatsby-remark-images`, |
| 39 | + options: { |
| 40 | + maxWidth: 590, |
| 41 | + }, |
| 42 | + }, |
| 43 | + `gatsby-remark-other-plugin`, |
| 44 | + ], |
| 45 | + remarkPlugins: [ |
| 46 | + require(`../gatsby-node.js`), |
| 47 | + [require(`../gatsby-node.js`), { target: false }], |
| 48 | + ], |
| 49 | + rehypePlugins: [ |
| 50 | + require(`../gatsby-node.js`), |
| 51 | + [require(`../gatsby-node.js`), { behavior: `wrap` }], |
| 52 | + ], |
| 53 | + mediaTypes: [`text/markdown`, `text/x-markdown`, `custom-media/type`], |
| 54 | + shouldBlockNodeFromTransformation: node => Boolean(node), |
| 55 | + }) |
| 56 | + |
| 57 | + expect(isValid).toBe(true) |
| 58 | + }) |
| 59 | +}) |
0 commit comments