diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 0000000..95825c6 --- /dev/null +++ b/jest.config.js @@ -0,0 +1,6 @@ +const jest = require("kcd-scripts/jest"); + +module.exports = { + ...jest, + coverageThreshold: null, +}; diff --git a/package.json b/package.json index 94b016f..01d78ff 100644 --- a/package.json +++ b/package.json @@ -28,12 +28,15 @@ "scripts": { "build": "kcd-scripts build --out-dir .", "lint": "kcd-scripts lint", + "test": "kcd-scripts test", + "test:update": "npm test -- --updateSnapshot --coverage", "validate": "kcd-scripts validate" }, "dependencies": { "@babel/runtime": "^7.12.1" }, "devDependencies": { + "gatsby-plugin-utils": "^0.2.38", "kcd-scripts": "^6.6.0" }, "peerDependencies": { diff --git a/src/__tests__/gatsby-node.js b/src/__tests__/gatsby-node.js new file mode 100644 index 0000000..745fbce --- /dev/null +++ b/src/__tests__/gatsby-node.js @@ -0,0 +1,39 @@ +import { testPluginOptionsSchema } from "gatsby-plugin-utils"; + +import { pluginOptionsSchema } from "../gatsby-node"; + +test.each` + saveButton | expectedErrors + ${"This should be a boolean or an object"} | ${['"saveButton" must be one of [boolean, object]']} + ${{ round: "This should be a boolean", tall: "This should be a boolean" }} | ${['"saveButton" does not match any of the allowed types']} + ${{ round: "This should be a boolean", tall: true }} | ${['"saveButton.round" must be a boolean']} + ${{ round: true, tall: "This should be a boolean" }} | ${['"saveButton.tall" must be a boolean']} +`( + "should provide meaningful errors when fields are invalid: $saveButton", + async ({ expectedErrors, saveButton }) => { + const { errors } = await testPluginOptionsSchema(pluginOptionsSchema, { + saveButton, + }); + + expect(errors).toEqual(expectedErrors); + }, +); + +test.each` + saveButton + ${undefined} + ${true} + ${false} + ${{}} + ${{ round: true }} + ${{ round: false }} + ${{ tall: true }} + ${{ tall: false }} + ${{ round: true, tall: true }} +`("should validate the schema: $saveButton", async ({ saveButton }) => { + const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, { + saveButton, + }); + + expect(isValid).toBe(true); +}); diff --git a/src/gatsby-node.js b/src/gatsby-node.js index c644175..8296ce4 100644 --- a/src/gatsby-node.js +++ b/src/gatsby-node.js @@ -1,8 +1,21 @@ -const deprecationWarning = `[gatsby-plugin-pinterest] From now on, you can use the 'saveButton' option to show Pinterest's save button on images. +if (process.env.GATSBY_EXPERIMENTAL_PLUGIN_OPTION_VALIDATION) { + exports.pluginOptionsSchema = ({ Joi }) => + Joi.object({ + saveButton: Joi.alternatives().try( + Joi.boolean(), + Joi.object({ + round: Joi.boolean(), + tall: Joi.boolean(), + }), + ), + }); +} else { + const deprecationWarning = `[gatsby-plugin-pinterest] From now on, you can use the 'saveButton' option to show Pinterest's save button on images. See https://github.com/robinmetral/gatsby-plugin-pinterest#usage`; -exports.onPreInit = ({ reporter }, { round, tall } = {}) => { - if (round || tall) { - reporter.warn(deprecationWarning); - } -}; + exports.onPreInit = ({ reporter }, { round, tall } = {}) => { + if (round || tall) { + reporter.warn(deprecationWarning); + } + }; +} diff --git a/tests/setup-env.js b/tests/setup-env.js new file mode 100644 index 0000000..1eaf066 --- /dev/null +++ b/tests/setup-env.js @@ -0,0 +1 @@ +process.env.GATSBY_EXPERIMENTAL_PLUGIN_OPTION_VALIDATION = "true";