-
Notifications
You must be signed in to change notification settings - Fork 334
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
#5276
Add onError
to initAll
#5276
Conversation
📋 StatsFile sizes
Modules
View stats and visualisations on the review app Action run for 5983f3a |
JavaScript changes to npm packagediff --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 |
Other changes to npm packagediff --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 |
There was a problem hiding this 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) |
There was a problem hiding this comment.
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?
* @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) |
console.log(error) | ||
console.log(context) |
There was a problem hiding this comment.
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()
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
5421f10
to
47cf52f
Compare
- `config` parameter of `initAll` now accepts `onError` callback - `initAll` tests updated for `onError` callback
47cf52f
to
5983f3a
Compare
There was a problem hiding this 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 😊 ⛵
What
config
parameter ofinitAll
now acceptsonError
callbackinitAll
tests updated foronError
callbackWhy
createAll
now supportsonError
callback, so makes sense forinitAll
to be able to takeonError
as a parameter to be passed intocreateAll
and also wheninitAll
errors itself.Fixes #5267