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

Add onError to initAll config #5276

Merged
merged 1 commit into from
Sep 11, 2024
Merged

Conversation

patrickpatrickpatrick
Copy link
Contributor

@patrickpatrickpatrick patrickpatrickpatrick commented Aug 29, 2024

What

  • config parameter of initAll now accepts onError callback
  • initAll tests updated for onError callback

Why

createAll now supports onError callback, so makes sense for initAll to be able to take onError as a parameter to be passed into createAll and also when initAll errors itself.

Fixes #5267

Copy link

github-actions bot commented Aug 29, 2024

📋 Stats

File sizes

File Size
dist/govuk-frontend-development.min.css 112.61 KiB
dist/govuk-frontend-development.min.js 42.2 KiB
packages/govuk-frontend/dist/govuk/all.bundle.js 89.77 KiB
packages/govuk-frontend/dist/govuk/all.bundle.mjs 84.31 KiB
packages/govuk-frontend/dist/govuk/all.mjs 1.01 KiB
packages/govuk-frontend/dist/govuk/govuk-frontend-component.mjs 359 B
packages/govuk-frontend/dist/govuk/govuk-frontend.min.css 112.6 KiB
packages/govuk-frontend/dist/govuk/govuk-frontend.min.js 42.18 KiB
packages/govuk-frontend/dist/govuk/i18n.mjs 5.55 KiB
packages/govuk-frontend/dist/govuk/init.mjs 6.79 KiB

Modules

File Size (bundled) Size (minified)
all.mjs 80.69 KiB 40.12 KiB
accordion.mjs 23.83 KiB 12.39 KiB
button.mjs 6.31 KiB 2.69 KiB
character-count.mjs 22.73 KiB 9.92 KiB
checkboxes.mjs 6.16 KiB 2.83 KiB
error-summary.mjs 8.22 KiB 3.46 KiB
exit-this-page.mjs 17.43 KiB 9.26 KiB
header.mjs 4.79 KiB 2.6 KiB
notification-banner.mjs 6.59 KiB 2.62 KiB
password-input.mjs 15.48 KiB 7.25 KiB
radios.mjs 5.16 KiB 2.38 KiB
skip-link.mjs 4.72 KiB 2.18 KiB
tabs.mjs 10.38 KiB 6.06 KiB

View stats and visualisations on the review app


Action run for 5983f3a

Copy link

github-actions bot commented Aug 29, 2024

JavaScript changes to npm package

diff --git a/packages/govuk-frontend/dist/govuk/govuk-frontend.min.js b/packages/govuk-frontend/dist/govuk/govuk-frontend.min.js
index dd7574fa1..2ae88f3c2 100644
--- a/packages/govuk-frontend/dist/govuk/govuk-frontend.min.js
+++ b/packages/govuk-frontend/dist/govuk/govuk-frontend.min.js
@@ -1083,7 +1083,9 @@ class Tabs extends GOVUKFrontendComponent {
 
 function initAll(e) {
     var t;
-    if (e = void 0 !== e ? e : {}, !isSupported()) return void console.log(new SupportError);
+    if (e = void 0 !== e ? e : {}, !isSupported()) return void(e.onError ? e.onError(new SupportError, {
+        config: e
+    }) : console.log(new SupportError));
     const n = [
             [Accordion, e.accordion],
             [Button, e.button],
@@ -1098,7 +1100,10 @@ function initAll(e) {
             [SkipLink],
             [Tabs]
         ],
-        i = null != (t = e.scope) ? t : document;
+        i = {
+            scope: null != (t = e.scope) ? t : document,
+            onError: e.onError
+        };
     n.forEach((([e, t]) => {
         createAll(e, t, i)
     }))

Action run for 5983f3a

Copy link

github-actions bot commented Aug 29, 2024

Other changes to npm package

diff --git a/packages/govuk-frontend/dist/govuk/all.bundle.js b/packages/govuk-frontend/dist/govuk/all.bundle.js
index 057ec0db2..d5d901b55 100644
--- a/packages/govuk-frontend/dist/govuk/all.bundle.js
+++ b/packages/govuk-frontend/dist/govuk/all.bundle.js
@@ -2356,19 +2356,28 @@
    * Use the `data-module` attributes to find, instantiate and init all of the
    * components provided as part of GOV.UK Frontend.
    *
-   * @param {Config & { scope?: Element }} [config] - Config for all components (with optional scope)
+   * @param {Config & { scope?: Element, onError?: OnErrorCallback<CompatibleClass> }} [config] - Config for all components (with optional scope)
    */
   function initAll(config) {
     var _config$scope;
     config = typeof config !== 'undefined' ? config : {};
     if (!isSupported()) {
-      console.log(new SupportError());
+      if (config.onError) {
+        config.onError(new SupportError(), {
+          config
+        });
+      } else {
+        console.log(new SupportError());
+      }
       return;
     }
     const components = [[Accordion, config.accordion], [Button, config.button], [CharacterCount, config.characterCount], [Checkboxes], [ErrorSummary, config.errorSummary], [ExitThisPage, config.exitThisPage], [Header], [NotificationBanner, config.notificationBanner], [PasswordInput, config.passwordInput], [Radios], [SkipLink], [Tabs]];
-    const $scope = (_config$scope = config.scope) != null ? _config$scope : document;
+    const options = {
+      scope: (_config$scope = config.scope) != null ? _config$scope : document,
+      onError: config.onError
+    };
     components.forEach(([Component, config]) => {
-      createAll(Component, config, $scope);
+      createAll(Component, config, options);
     });
   }
 
@@ -2469,7 +2478,7 @@
    * @template {CompatibleClass} T
    * @typedef {object} ErrorContext
    * @property {Element} [element] - Element used for component module initialisation
-   * @property {T} component - Class of component
+   * @property {T} [component] - Class of component
    * @property {T["defaults"]} config - Config supplied to component
    */
   /**
diff --git a/packages/govuk-frontend/dist/govuk/all.bundle.mjs b/packages/govuk-frontend/dist/govuk/all.bundle.mjs
index 15a7feada..8add055cb 100644
--- a/packages/govuk-frontend/dist/govuk/all.bundle.mjs
+++ b/packages/govuk-frontend/dist/govuk/all.bundle.mjs
@@ -2350,19 +2350,28 @@ Tabs.moduleName = 'govuk-tabs';
  * Use the `data-module` attributes to find, instantiate and init all of the
  * components provided as part of GOV.UK Frontend.
  *
- * @param {Config & { scope?: Element }} [config] - Config for all components (with optional scope)
+ * @param {Config & { scope?: Element, onError?: OnErrorCallback<CompatibleClass> }} [config] - Config for all components (with optional scope)
  */
 function initAll(config) {
   var _config$scope;
   config = typeof config !== 'undefined' ? config : {};
   if (!isSupported()) {
-    console.log(new SupportError());
+    if (config.onError) {
+      config.onError(new SupportError(), {
+        config
+      });
+    } else {
+      console.log(new SupportError());
+    }
     return;
   }
   const components = [[Accordion, config.accordion], [Button, config.button], [CharacterCount, config.characterCount], [Checkboxes], [ErrorSummary, config.errorSummary], [ExitThisPage, config.exitThisPage], [Header], [NotificationBanner, config.notificationBanner], [PasswordInput, config.passwordInput], [Radios], [SkipLink], [Tabs]];
-  const $scope = (_config$scope = config.scope) != null ? _config$scope : document;
+  const options = {
+    scope: (_config$scope = config.scope) != null ? _config$scope : document,
+    onError: config.onError
+  };
   components.forEach(([Component, config]) => {
-    createAll(Component, config, $scope);
+    createAll(Component, config, options);
   });
 }
 
@@ -2463,7 +2472,7 @@ function createAll(Component, config, createAllOptions) {
  * @template {CompatibleClass} T
  * @typedef {object} ErrorContext
  * @property {Element} [element] - Element used for component module initialisation
- * @property {T} component - Class of component
+ * @property {T} [component] - Class of component
  * @property {T["defaults"]} config - Config supplied to component
  */
 /**
diff --git a/packages/govuk-frontend/dist/govuk/init.mjs b/packages/govuk-frontend/dist/govuk/init.mjs
index 2de2329d5..57b9296b7 100644
--- a/packages/govuk-frontend/dist/govuk/init.mjs
+++ b/packages/govuk-frontend/dist/govuk/init.mjs
@@ -19,19 +19,28 @@ import { SupportError } from './errors/index.mjs';
  * Use the `data-module` attributes to find, instantiate and init all of the
  * components provided as part of GOV.UK Frontend.
  *
- * @param {Config & { scope?: Element }} [config] - Config for all components (with optional scope)
+ * @param {Config & { scope?: Element, onError?: OnErrorCallback<CompatibleClass> }} [config] - Config for all components (with optional scope)
  */
 function initAll(config) {
   var _config$scope;
   config = typeof config !== 'undefined' ? config : {};
   if (!isSupported()) {
-    console.log(new SupportError());
+    if (config.onError) {
+      config.onError(new SupportError(), {
+        config
+      });
+    } else {
+      console.log(new SupportError());
+    }
     return;
   }
   const components = [[Accordion, config.accordion], [Button, config.button], [CharacterCount, config.characterCount], [Checkboxes], [ErrorSummary, config.errorSummary], [ExitThisPage, config.exitThisPage], [Header], [NotificationBanner, config.notificationBanner], [PasswordInput, config.passwordInput], [Radios], [SkipLink], [Tabs]];
-  const $scope = (_config$scope = config.scope) != null ? _config$scope : document;
+  const options = {
+    scope: (_config$scope = config.scope) != null ? _config$scope : document,
+    onError: config.onError
+  };
   components.forEach(([Component, config]) => {
-    createAll(Component, config, $scope);
+    createAll(Component, config, options);
   });
 }
 
@@ -132,7 +141,7 @@ function createAll(Component, config, createAllOptions) {
  * @template {CompatibleClass} T
  * @typedef {object} ErrorContext
  * @property {Element} [element] - Element used for component module initialisation
- * @property {T} component - Class of component
+ * @property {T} [component] - Class of component
  * @property {T["defaults"]} config - Config supplied to component
  */
 /**

Action run for 5983f3a

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.

Seems to flow nicely from initAll to createAll 🙌🏻 There's a couple of little things to tidy up in the test file before we can merge (a couple for a separate PR), but otherwise it's neat.

@@ -19,14 +19,23 @@ import { SupportError } from './errors/index.mjs'
* Use the `data-module` attributes to find, instantiate and init all of the
* components provided as part of GOV.UK Frontend.
*
* @param {Config & { scope?: Element }} [config] - Config for all components (with optional scope)
* @param {Config & { scope?: Element } & { onError?: OnErrorCallback<CompatibleClass> }} [config] - Config for all components (with optional scope)
Copy link
Member

Choose a reason for hiding this comment

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

suggestion Could this be grouped in a single object with all the non-Config options?

Suggested change
* @param {Config & { scope?: Element } & { onError?: OnErrorCallback<CompatibleClass> }} [config] - Config for all components (with optional scope)
* @param {Config & { scope?: Element, onError?: OnErrorCallback<CompatibleClass> }} [config] - Config for all components (with optional scope)

packages/govuk-frontend/src/govuk/init.mjs Show resolved Hide resolved
Comment on lines +188 to +189
console.log(error)
console.log(context)
Copy link
Member

Choose a reason for hiding this comment

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

One for a separate PR, but this makes me think that one thing we didn't test is that createAll (and initAll) no longer log to the console if an onError is provided. Would be great if we had a way to test this alongside the fact that the callback is called.

If those two console.log calls are only here to support the two toHaveBeenCalledWith later on, it seems toHaveBeenCalledWith supports multiple arguments so those later calls might be doable as one:

expect(errorCallback).toHaveBeenCalledWith(
  expect.any(Error),
  expect.objectContaining({
    // Whatever we expect
  })
)

Which would then allow us to check that the function no longer logged with

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can consolidate the console.log check! I mean in theory it doesn't need to do any outputting at all. We can just check to see the error callback was called at all and then check to see if there's no error logged (the default behaviour).

Copy link
Member

Choose a reason for hiding this comment

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

Created #5303 to track the update of the tests.

})

// Silence warnings in test output, and allow us to 'expect' them
jest.spyOn(global.console, 'log').mockImplementation()
Copy link
Member

Choose a reason for hiding this comment

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

nitpick Missed that during the review of createAll, so one for a separate PR as well, but seems jest.spyOn does not clean up after itself and we may need to call jest.restoreAllMocks to ensure the mock is cleaned up for further tests.

Copy link
Member

Choose a reason for hiding this comment

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

Added a small story to track this as we should have a look more widely than the console.log spies for the createAll and initAll tests.

- `config` parameter of `initAll` now accepts `onError` callback
- `initAll` tests updated for `onError` callback
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-5276 September 10, 2024 09:58 Inactive
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.

Thanks for adressing the comments 😊 ⛵

@patrickpatrickpatrick patrickpatrickpatrick merged commit 5783a2b into public-js-api Sep 11, 2024
48 checks passed
@patrickpatrickpatrick patrickpatrickpatrick deleted the on-error-init-all branch September 11, 2024 10:03
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