Skip to content

Commit

Permalink
Relax requirements to receive configuration with createAll
Browse files Browse the repository at this point in the history
Remove the need for components to have a `defaults` static property,
as it's really an internal convention which is a lot to ask from outside components.
  • Loading branch information
romaricpascal committed Oct 3, 2024
1 parent bd306f7 commit b7c90a2
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 50 deletions.
52 changes: 3 additions & 49 deletions packages/govuk-frontend/src/govuk/init.jsdom.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -368,23 +368,17 @@ describe('createAll', () => {
expect(result[1].args).toStrictEqual([document.getElementById('b')])
})

describe('when a component accepts config', () => {
class MockComponentWithConfig extends MockComponent {
static defaults = {
__test: false
}
}

describe('when a configuration is passed', () => {
it('initialises a component, passing the component root and config', () => {
const componentRoot = document.createElement('div')
componentRoot.setAttribute('data-module', 'mock-component')
document.body.appendChild(componentRoot)

const result = createAll(MockComponentWithConfig, {
const result = createAll(MockComponent, {
__test: true
})

expect(result).toStrictEqual([expect.any(MockComponentWithConfig)])
expect(result).toStrictEqual([expect.any(MockComponent)])

expect(result[0].args).toStrictEqual([
componentRoot,
Expand All @@ -393,46 +387,6 @@ describe('createAll', () => {
}
])
})

it('initialises a component, passing the component root even when no config is passed', () => {
const componentRoot = document.createElement('div')
componentRoot.setAttribute('data-module', 'mock-component')
document.body.appendChild(componentRoot)

const result = createAll(MockComponentWithConfig)

expect(result).toStrictEqual([expect.any(MockComponentWithConfig)])

console.log(result[0].args)

expect(result[0].args).toStrictEqual([componentRoot])
})

it('passes the config to all component objects', () => {
document.body.innerHTML = `
<div data-module="mock-component" id="a"></div>
<div data-module="mock-component" id="b"></div>`

const config = {
__test: true
}

const result = createAll(MockComponentWithConfig, config)

expect(result).toStrictEqual([
expect.any(MockComponentWithConfig),
expect.any(MockComponentWithConfig)
])

expect(result[0].args).toStrictEqual([
document.getElementById('a'),
config
])
expect(result[1].args).toStrictEqual([
document.getElementById('b'),
config
])
})
})

describe('when a $scope is passed as third parameter', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/govuk-frontend/src/govuk/init.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ function createAll(Component, config, createAllOptions) {
try {
// Only pass config to components that accept it
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return 'defaults' in Component && typeof config !== 'undefined'
return typeof config !== 'undefined'
? new Component($element, config)
: new Component($element)
} catch (error) {
Expand Down

0 comments on commit b7c90a2

Please sign in to comment.