Skip to content

Commit

Permalink
Merge pull request #5306 from alphagov/change-on-error-test
Browse files Browse the repository at this point in the history
Change `onError` tests to test `console.log` not called
  • Loading branch information
patrickpatrickpatrick authored Sep 12, 2024
2 parents 2de2cee + d6f0617 commit 0a70233
Showing 1 changed file with 21 additions and 33 deletions.
54 changes: 21 additions & 33 deletions packages/govuk-frontend/src/govuk/init.jsdom.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,7 @@ describe('initAll', () => {
})

it('executes onError if specified', () => {
const errorCallback = jest.fn((error, context) => {
console.log(error)
console.log(context)
})
const errorCallback = jest.fn((_error, _context) => {})

initAll({
accordion: {
Expand All @@ -119,14 +116,12 @@ describe('initAll', () => {
onError: errorCallback
})

expect(errorCallback).toHaveBeenCalled()
expect(global.console.log).not.toHaveBeenCalled()

expect(global.console.log).toHaveBeenCalledWith(
expect(errorCallback).toHaveBeenCalledWith(
expect.objectContaining({
message: 'GOV.UK Frontend is not supported in this browser'
})
)
expect(global.console.log).toHaveBeenCalledWith(
}),
expect.objectContaining({
config: {
accordion: {
Expand Down Expand Up @@ -188,17 +183,15 @@ describe('initAll', () => {
document.body.classList.add('govuk-frontend-supported')
document.body.innerHTML = '<div data-module="govuk-accordion"></div>'

const accordionEl = document.querySelector(
"[data-module='govuk-accordion']"
)

jest.mocked(GOVUKFrontend.Accordion).mockImplementation(() => {
throw new Error('Error thrown from accordion')
})

const errorCallback = jest.fn((error, context) => {
console.log(error)
console.log(context)
})

// Silence warnings in test output, and allow us to 'expect' them
jest.spyOn(global.console, 'log').mockImplementation()
const errorCallback = jest.fn((_error, _context) => {})

initAll({
onError: errorCallback,
Expand All @@ -207,15 +200,18 @@ describe('initAll', () => {
}
})

expect(global.console.log).toHaveBeenCalledWith(
expect(global.console.log).not.toHaveBeenCalled()

expect(errorCallback).toHaveBeenCalledWith(
expect.objectContaining({
message: 'Error thrown from accordion'
})
)
expect(global.console.log).toHaveBeenCalledWith(
}),
expect.objectContaining({
component: GOVUKFrontend.Accordion,
config: { rememberExpanded: true }
config: {
rememberExpanded: true
},
element: accordionEl
})
)
})
Expand Down Expand Up @@ -273,13 +269,7 @@ describe('createAll', () => {
it('executes specified onError callback and returns empty array if not supported', () => {
document.body.classList.remove('govuk-frontend-supported')

const errorCallback = jest.fn((error, context) => {
console.log(error)
console.log(context)
})

// Silence warnings in test output, and allow us to 'expect' them
jest.spyOn(global.console, 'log').mockImplementation()
const errorCallback = jest.fn((_error, _context) => {})

expect(() => {
createAll(
Expand All @@ -289,14 +279,12 @@ describe('createAll', () => {
)
}).not.toThrow()

expect(errorCallback).toHaveBeenCalled()
expect(global.console.log).not.toHaveBeenCalled()

expect(global.console.log).toHaveBeenCalledWith(
expect(errorCallback).toHaveBeenCalledWith(
expect.objectContaining({
message: 'GOV.UK Frontend is not supported in this browser'
})
)
expect(global.console.log).toHaveBeenCalledWith(
}),
expect.objectContaining({
component: MockComponent,
config: { attribute: 'random' }
Expand Down

0 comments on commit 0a70233

Please sign in to comment.