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

checkSupport made static and protected #5336

Open
wants to merge 1 commit into
base: public-js-api
Choose a base branch
from

Conversation

patrickpatrickpatrick
Copy link
Contributor

@patrickpatrickpatrick patrickpatrickpatrick commented Sep 19, 2024

What

Specify that checkSupport of GOVUKFrontendComponent is static and protected instead of private.

Why

Change in approach to how we allow people to run their own function to check if a component is supported. This change means now components that extend GOVUKFrontendComponent can overload checkSupport (as can be seen in the tests).

Fixes #5225 (comment)

@patrickpatrickpatrick patrickpatrickpatrick changed the base branch from main to public-js-api September 19, 2024 15:27
Copy link

github-actions bot commented Sep 19, 2024

📋 Stats

File sizes

File Size
dist/govuk-frontend-development.min.css 118.52 KiB
dist/govuk-frontend-development.min.js 44.6 KiB
packages/govuk-frontend/dist/govuk/all.bundle.js 94.18 KiB
packages/govuk-frontend/dist/govuk/all.bundle.mjs 88.43 KiB
packages/govuk-frontend/dist/govuk/all.mjs 1.1 KiB
packages/govuk-frontend/dist/govuk/govuk-frontend-component.mjs 1.29 KiB
packages/govuk-frontend/dist/govuk/govuk-frontend.min.css 118.5 KiB
packages/govuk-frontend/dist/govuk/govuk-frontend.min.js 44.59 KiB
packages/govuk-frontend/dist/govuk/i18n.mjs 5.55 KiB
packages/govuk-frontend/dist/govuk/init.mjs 6.9 KiB

Modules

File Size (bundled) Size (minified)
all.mjs 84.57 KiB 42.37 KiB
accordion.mjs 25 KiB 12.94 KiB
button.mjs 7.48 KiB 3.24 KiB
character-count.mjs 23.9 KiB 10.47 KiB
checkboxes.mjs 7.34 KiB 3.38 KiB
error-summary.mjs 9.39 KiB 4 KiB
exit-this-page.mjs 18.61 KiB 9.81 KiB
header.mjs 5.97 KiB 3.15 KiB
notification-banner.mjs 7.76 KiB 3.17 KiB
password-input.mjs 16.64 KiB 7.8 KiB
radios.mjs 6.33 KiB 2.92 KiB
service-navigation.mjs 5.95 KiB 3.24 KiB
skip-link.mjs 5.89 KiB 2.73 KiB
tabs.mjs 11.55 KiB 6.61 KiB

View stats and visualisations on the review app


Action run for 290af25

`checkSupport` of `GOVUKFrontend` can be overloaded by classes that
extend it. Added tests for `createAll` when `checkSupport` has been
overloaded.
Copy link
Member

@romaricpascal romaricpascal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Little misunderstanding on the staticness of the checkSupport method, sorry 😔 I've clarified in the comments 😊

Comment on lines +52 to 56
* @protected
* @static
* @throws {SupportError} when the components are not supported
*/
checkSupport() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue This only documents the method as static, but the method itself is not static from a JavaScript point of view (it's interesting that TypeScript does not pick up on that, actually, that'll be something to remember).

Suggested change
* @protected
* @static
* @throws {SupportError} when the components are not supported
*/
checkSupport() {
* @protected
* @throws {SupportError} when the components are not supported
*/
static checkSupport() {

Having the method static will help avoid overloading the console with errors when a component is repeated multiple times on the page (like the Copy button on the Design System page), letting createAll do a single initial check before looping.

const result = createAll(MockComponentWithCheckSupport)
expect(checkSupportMock).toHaveBeenCalled()
expect(result).toStrictEqual([])
expect(global.console.log).toHaveBeenCalledWith(expect.any(Error))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion Is there a way we can check this is the error thrown by the support mock (for example, comparing the text of the error)?

Comment on lines +230 to 234
class MockComponent extends GOVUKFrontendComponent {
constructor(...args) {
super(...args)
this.args = args
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good spot to update this here 🙌🏻

Comment on lines +304 to +308
class MockComponentWithCheckSupport extends MockComponent {
checkSupport() {
checkSupportMock()
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion Possibly a long shot, but does jest.spyOn let us avoid creating one class per test with the method being static?

const checkSupportMock = jest.spyOn(MockComponent, 'checkSupport')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants