diff --git a/packages/gatsby-plugin-google-tagmanager/package.json b/packages/gatsby-plugin-google-tagmanager/package.json index d6ccdeb71ffef..a763748c5d7db 100644 --- a/packages/gatsby-plugin-google-tagmanager/package.json +++ b/packages/gatsby-plugin-google-tagmanager/package.json @@ -13,7 +13,8 @@ "@babel/cli": "^7.11.6", "@babel/core": "^7.11.6", "babel-preset-gatsby-package": "^0.6.0-next.0", - "cross-env": "^7.0.2" + "cross-env": "^7.0.2", + "gatsby-plugin-utils": "^0.3.0-next.0" }, "homepage": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-google-tagmanager#readme", "keywords": [ diff --git a/packages/gatsby-plugin-google-tagmanager/src/__tests__/gatsby-node.js b/packages/gatsby-plugin-google-tagmanager/src/__tests__/gatsby-node.js new file mode 100644 index 0000000000000..5315f095b1664 --- /dev/null +++ b/packages/gatsby-plugin-google-tagmanager/src/__tests__/gatsby-node.js @@ -0,0 +1,35 @@ +import { pluginOptionsSchema } from "../gatsby-node" +import { testPluginOptionsSchema } from "gatsby-plugin-utils" + +describe(`pluginOptionsSchema`, () => { + it(`should validate valid options`, async () => { + const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, { + id: `YOUR_GOOGLE_TAGMANAGER_ID`, + includeInDevelopment: false, + defaultDataLayer: { platform: `gatsby` }, + gtmAuth: `YOUR_GOOGLE_TAGMANAGER_ENVIRONMENT_AUTH_STRING`, + gtmPreview: `YOUR_GOOGLE_TAGMANAGER_ENVIRONMENT_PREVIEW_NAME`, + dataLayerName: `YOUR_DATA_LAYER_NAME`, + routeChangeEventName: `YOUR_ROUTE_CHANGE_EVENT_NAME`, + }) + + expect(isValid).toEqual(true) + }) + + it(`should support defaultDataLayer as a function`, async () => { + const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, { + defaultDataLayer: () => { + return { + originalLocation: + document.location.protocol + + `//` + + document.location.hostname + + document.location.pathname + + document.location.search, + } + }, + }) + + expect(isValid).toEqual(true) + }) +}) diff --git a/packages/gatsby-plugin-google-tagmanager/src/gatsby-node.js b/packages/gatsby-plugin-google-tagmanager/src/gatsby-node.js index daa6532e1b3f6..82b02708dec20 100644 --- a/packages/gatsby-plugin-google-tagmanager/src/gatsby-node.js +++ b/packages/gatsby-plugin-google-tagmanager/src/gatsby-node.js @@ -22,7 +22,8 @@ exports.pluginOptionsSchema = ({ Joi }) => .description( `Include Google Tag Manager when running in development mode.` ), - defaultDataLayer: Joi.object() + defaultDataLayer: Joi.alternatives() + .try(Joi.object(), Joi.function()) .default(null) .description( `Data layer to be set before Google Tag Manager is loaded. Should be an object or a function.`