-
Notifications
You must be signed in to change notification settings - Fork 336
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
Throw ElementError
for missing elements during Character count instantiation
#4261
Conversation
📋 StatsFile sizes
Modules
View stats and visualisations on the review app Action run for 808f7ec |
packages/govuk-frontend/src/govuk/components/character-count/character-count.mjs
Outdated
Show resolved
Hide resolved
Looks like we'll have to port the tests currently run in JSDom to Puppeteer. Even after adding an element for the textarea description, appending the markup to the |
@romaricpascal Any quicker fixes to avoid |
Digging a bit, we're using |
Now we throw if the textarea description is missing, that element needs to be in the DOM as well. Our use of `document.getElementById` also meant that the elements need to be added to the `document`. This meant that we also need some cleanup between tests as Jest JSDOM doesn't handle that between tests of a same file. Finally, we also needed to use `textContent` rather than `innerText` as[JSDOM does not support the later](jsdom/jsdom#1245), and we were only [using it for IE8](#2980), which we no longer need to support.
…components For consistency.
We are not quoting the identifier and having those around the types leads to unsightly quoting for the Character Count
I think it's properly ready for review 😊 @domoscargin The removal of the quotes has a tiny bit of overlap with the tests you may be writing for the Tabs. It should be fairly easy to update with a search and replace for |
packages/govuk-frontend/src/govuk/components/character-count/character-count.jsdom.test.mjs
Outdated
Show resolved
Hide resolved
packages/govuk-frontend/src/govuk/components/character-count/character-count.jsdom.test.mjs
Outdated
Show resolved
Hide resolved
return this | ||
throw new ElementError($textarea, { | ||
componentName: 'Character count', | ||
identifier: '.govuk-js-character-count', |
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.
With (potentially) multiple textarea components on the page, how much do we worry about scoping selectors against their module?
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.
Linking with #4263 (review)
For these "module child" selectors it could be brilliant to show a scope that the browser can inspect
identifier: '.govuk-js-character-count', | |
identifier: '.govuk-js-character-count', | |
scope: $module, |
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.
🤔 Yeah, that's probably the component most likely to be on a page multiple times. We recommend to start with a single question per page so that limits the risks. It doesn't completely avoids the possibility of multiple of those on the page (especially if we consider the radios/checkboxes potentially revealing fields). That said, there shouldn't be too many of them on a given page that it's tricky for developers to find back the one that's wonky (either by looking carefully at the page or breaking on error in the devtools). I would say it's not something we should not worry too hard about at the start. We can always dig deeper if we receive feedback.
If we have a simple solution, though, not against adding extra information, but it might be tricky to add that consistently. The element with data-module
does not really have anything identifiable usually. Here, the $textarea
could provide an id
(but only if it is on the page so nothing if it doesn't exist).
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.
Whoops, there wasn't your second message when I started writing mine, sorry. How would users access the $module
you propose passing as $scope
(it's fine if that part still needs looking into)? Do browsers log extra fields from the errors? the cause
maybe, in browsers that support it (that'd be neat)?
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 to investigate. Would be amazing if you could hover over scope
and it highlighted the element
Can leave them off for now if we don't need it
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.
That'd speed debugging indeed, but let's see if people come back needing more pointers to where things happen as I think that'd need a good bit of investigation 😊 Had a quick glance at cause
, Firefox logs it (only if the element itself, if an object, you get Object {...}
, not expandable), not Chrome, nor Safari (at the time of this message). We could always override toString
to log before the message gets computed for display in the console, but seems quite hacky anyway 😆
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.
Sorry, not the clearest blurb. I'll (try to) clarify:
- I'd prefer leaving it off until we have the need for it as it'd require more investigation to make it work
- Got curious and gave a quick check at where Error's
cause
are logged (no luck there) and tried a quick hack by overridingtoString
to log before the error message gets computed (can log the element, but a bit hacky).
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.
No don't worry, the confused face was wondering why cause
wasn't logged
One for another day (and don't want to be Chrome specific) but some nice ideas in:
https://developer.chrome.com/docs/devtools/console/log/
).rejects.toEqual({ | ||
name: 'ElementError', | ||
message: | ||
'Character count: $module is not an instance of "HTMLElement"' | ||
'Character count: .govuk-js-character-count is not of type HTMLTextareaElement or HTMLInputElement' |
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.
It works 🙌
packages/govuk-frontend/src/govuk/components/character-count/character-count.jsdom.test.mjs
Outdated
Show resolved
Hide resolved
packages/govuk-frontend/src/govuk/components/character-count/character-count.jsdom.test.mjs
Outdated
Show resolved
Hide resolved
207794c
to
c875b47
Compare
c875b47
to
f601900
Compare
f601900
to
808f7ec
Compare
Throw `ElementError` for missing elements during Character count instantiation
Adds two errors thrown when:
HTMLTextareaElement
or anHTMLInputElement
Closes #4125