diff --git a/cypress/e2e/plugins/0-mock-plugin-tests/single-plugin-test.cypress.js b/cypress/e2e/plugins/0-mock-plugin-tests/single-plugin-test.cypress.js index 23347642d8..b64ca78a0a 100644 --- a/cypress/e2e/plugins/0-mock-plugin-tests/single-plugin-test.cypress.js +++ b/cypress/e2e/plugins/0-mock-plugin-tests/single-plugin-test.cypress.js @@ -57,7 +57,7 @@ describe('Single Plugin Test', async () => { .should('have.html', 'abc') }) - it('Loads plugin-foo global correctly', () => { + it('Loads plugin-foo function correctly', () => { waitForApplication() cy.visit('/plugin-foo') cy.get('#test-foo-emphasize-global-function') diff --git a/cypress/fixtures/plugins/plugin-foo/globals.js b/cypress/fixtures/plugins/plugin-foo/globals.js index 604a3e30c4..1ba0031835 100644 --- a/cypress/fixtures/plugins/plugin-foo/globals.js +++ b/cypress/fixtures/plugins/plugin-foo/globals.js @@ -1,2 +1,2 @@ -const { addGlobal } = require('govuk-prototype-kit').views -addGlobal('fooEmphasize', (content) => `${content}`, { renderAsHtml: true }) +const { addFunction } = require('govuk-prototype-kit').views +addFunction('fooEmphasize', (content) => `${content}`, { renderAsHtml: true }) diff --git a/lib/globals/api.js b/lib/globals/api.js index ca7553f70f..37a2d2b90a 100644 --- a/lib/globals/api.js +++ b/lib/globals/api.js @@ -23,15 +23,19 @@ function addGlobalToEnvironment (name, fn, config) { environment.addGlobal(name, fnToAdd) } -function addGlobal (name, fn, config) { +function addFunction (name, fn, config) { runWhenEnvIsAvailable(() => { - addGlobalToEnvironment(name, fn, config) + if (typeof fn === 'function') { + addGlobalToEnvironment(name, fn, config) + } else { + console.warn(`Couldn't add function [${name}] as it is not a function`) + } }) } -function getGlobal (name) { +function getFunction (name) { if (!environment) { - console.warn(`Trying to get global before the environment is set, couldn't retrieve global [${name}]`) + console.warn(`Trying to get a function before the environment is set, couldn't retrieve function [${name}]`) } else { try { return environment.getGlobal(name) @@ -55,8 +59,8 @@ function setEnvironment (env) { module.exports = { external: { - addGlobal, - getGlobal + addFunction, + getFunction }, setEnvironment } diff --git a/lib/globals/globals.test.js b/lib/globals/globals.test.js index ac567814c8..cae406f9cf 100644 --- a/lib/globals/globals.test.js +++ b/lib/globals/globals.test.js @@ -14,7 +14,7 @@ describe('Globals', () => { const fn = () => {} api.setEnvironment(env) - api.external.addGlobal('my-great-global', fn) + api.external.addFunction('my-great-global', fn) expect(env.addGlobal).toHaveBeenCalledWith('my-great-global', fn) }) @@ -25,15 +25,15 @@ describe('Globals', () => { api.setEnvironment(env) - expect(api.external.getGlobal('my-great-global')).toBe(fn) + expect(api.external.getFunction('my-great-global')).toBe(fn) }) it('Should log a warning when trying to get a global before the environment is set', () => { jest.spyOn(console, 'warn').mockImplementation(() => {}) - api.external.getGlobal('my-great-global') + api.external.getFunction('my-great-global') - expect(console.warn).toHaveBeenCalledWith('Trying to get global before the environment is set, couldn\'t retrieve global [my-great-global]') + expect(console.warn).toHaveBeenCalledWith('Trying to get a function before the environment is set, couldn\'t retrieve function [my-great-global]') }) it('Should log a warning when trying to get a global that doesn\'t exist', () => { @@ -44,7 +44,7 @@ describe('Globals', () => { } api.setEnvironment(env) - api.external.getGlobal('my-great-global') + api.external.getFunction('my-great-global') expect(console.warn).toHaveBeenCalledWith('Couldn\'t retrieve global [my-great-global]') }) @@ -61,7 +61,7 @@ describe('Globals', () => { api.setEnvironment(env) expect(() => { - api.external.getGlobal('my-great-global') + api.external.getFunction('my-great-global') }).toThrow(error) }) @@ -69,7 +69,7 @@ describe('Globals', () => { const env = { addGlobal: jest.fn() } const fn = () => {} - api.external.addGlobal('my-great-global', fn) + api.external.addFunction('my-great-global', fn) api.setEnvironment(env) expect(env.addGlobal).toHaveBeenCalledWith('my-great-global', fn) @@ -79,9 +79,9 @@ describe('Globals', () => { const namesInOrder = [] const env = { addGlobal: jest.fn((name) => { namesInOrder.push(name) }) } - api.external.addGlobal('my-great-global', () => {}) - api.external.addGlobal('someone-elses-global', () => {}) - api.external.addGlobal('a-third-global', () => {}) + api.external.addFunction('my-great-global', () => {}) + api.external.addFunction('someone-elses-global', () => {}) + api.external.addFunction('a-third-global', () => {}) api.setEnvironment(env) expect(namesInOrder).toEqual(['my-great-global', 'someone-elses-global', 'a-third-global']) diff --git a/prototype-starter/app/globals.js b/prototype-starter/app/globals.js index 68bcb5e71f..9bbe06cb53 100644 --- a/prototype-starter/app/globals.js +++ b/prototype-starter/app/globals.js @@ -1,2 +1,2 @@ const govukPrototypeKit = require('govuk-prototype-kit') -const addGlobal = govukPrototypeKit.views.addGlobal +const addGlobal = govukPrototypeKit.views.addFunction