Skip to content

Commit

Permalink
Merge pull request #4117 from alphagov/initall-support-error-log
Browse files Browse the repository at this point in the history
  • Loading branch information
romaricpascal authored Aug 24, 2023
2 parents c8eb86f + 275a24e commit 380ddc6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
5 changes: 4 additions & 1 deletion packages/govuk-frontend/src/govuk/all.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable no-new */

import { version } from './common/govuk-frontend-version.mjs'
import { isSupported } from './common/index.mjs'
import { Accordion } from './components/accordion/accordion.mjs'
import { Button } from './components/button/button.mjs'
import { CharacterCount } from './components/character-count/character-count.mjs'
Expand All @@ -12,6 +13,7 @@ import { NotificationBanner } from './components/notification-banner/notificatio
import { Radios } from './components/radios/radios.mjs'
import { SkipLink } from './components/skip-link/skip-link.mjs'
import { Tabs } from './components/tabs/tabs.mjs'
import { SupportError } from './errors/index.mjs'

/**
* Initialise all components
Expand All @@ -25,7 +27,8 @@ function initAll(config) {
config = typeof config !== 'undefined' ? config : {}

// Skip initialisation when GOV.UK Frontend is not supported
if (!document.body.classList.contains('govuk-frontend-supported')) {
if (!isSupported()) {
console.log(new SupportError())
return
}

Expand Down
24 changes: 20 additions & 4 deletions packages/govuk-frontend/src/govuk/all.unit.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,28 @@ describe('initAll', () => {
}
)

it('returns early if govuk-frontend-supported class is not present', () => {
document.body.innerHTML = '<div data-module="govuk-accordion"></div>'
describe('govuk-frontend-supported not present', () => {
it('returns early', () => {
document.body.innerHTML = '<div data-module="govuk-accordion"></div>'

GOVUKFrontend.initAll()

expect(GOVUKFrontend.Accordion).not.toHaveBeenCalled()
})

GOVUKFrontend.initAll()
it('logs why it did not initialise components', () => {
// Silence warnings in test output, and allow us to 'expect' them
jest.spyOn(global.console, 'log').mockImplementation()

expect(GOVUKFrontend.Accordion).not.toHaveBeenCalled()
GOVUKFrontend.initAll()

// Only validate the message as it's the important part for the user
expect(global.console.log).toHaveBeenCalledWith(
expect.objectContaining({
message: 'GOV.UK Frontend is not supported in this browser'
})
)
})
})

it('only initialises components within a given scope', () => {
Expand Down

0 comments on commit 380ddc6

Please sign in to comment.