Skip to content

Commit

Permalink
Changes addGlobal to addFunction as requested
Browse files Browse the repository at this point in the history
  • Loading branch information
BenSurgisonGDS committed Mar 9, 2023
1 parent 9c83b66 commit 3428afa
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('Single Plugin Test', async () => {
.should('have.html', '<strong>abc</strong>')
})

it('Loads plugin-foo global correctly', () => {
it('Loads plugin-foo function correctly', () => {
waitForApplication()
cy.visit('/plugin-foo')
cy.get('#test-foo-emphasize-global-function')
Expand Down
4 changes: 2 additions & 2 deletions cypress/fixtures/plugins/plugin-foo/globals.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
const { addGlobal } = require('govuk-prototype-kit').views
addGlobal('fooEmphasize', (content) => `<em>${content}</em>`, { renderAsHtml: true })
const { addFunction } = require('govuk-prototype-kit').views
addFunction('fooEmphasize', (content) => `<em>${content}</em>`, { renderAsHtml: true })
16 changes: 10 additions & 6 deletions lib/globals/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -55,8 +59,8 @@ function setEnvironment (env) {

module.exports = {
external: {
addGlobal,
getGlobal
addFunction,
getFunction
},
setEnvironment
}
20 changes: 10 additions & 10 deletions lib/globals/globals.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand All @@ -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', () => {
Expand All @@ -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]')
})
Expand All @@ -61,15 +61,15 @@ describe('Globals', () => {
api.setEnvironment(env)

expect(() => {
api.external.getGlobal('my-great-global')
api.external.getFunction('my-great-global')
}).toThrow(error)
})

it('Should allow adding globals before setting environment', () => {
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)
Expand All @@ -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'])
Expand Down
2 changes: 1 addition & 1 deletion prototype-starter/app/globals.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
const govukPrototypeKit = require('govuk-prototype-kit')
const addGlobal = govukPrototypeKit.views.addGlobal
const addGlobal = govukPrototypeKit.views.addFunction

0 comments on commit 3428afa

Please sign in to comment.