Skip to content
This repository has been archived by the owner on Dec 6, 2022. It is now read-only.

Commit

Permalink
feat: add support for the new pluginOptionsSchema function in gatsby-…
Browse files Browse the repository at this point in the history
…node (#15)
  • Loading branch information
MichaelDeBoey authored Oct 22, 2020
1 parent 2ed6b9a commit 691be05
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 6 deletions.
6 changes: 6 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const jest = require("kcd-scripts/jest");

module.exports = {
...jest,
coverageThreshold: null,
};
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
39 changes: 39 additions & 0 deletions src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -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);
});
25 changes: 19 additions & 6 deletions src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -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);
}
};
}
1 change: 1 addition & 0 deletions tests/setup-env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
process.env.GATSBY_EXPERIMENTAL_PLUGIN_OPTION_VALIDATION = "true";

0 comments on commit 691be05

Please # to comment.