Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relax requirements to receive configuration with createAll #5374

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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