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

Update input template tests to use jsdom #5017

Merged
merged 2 commits into from
May 31, 2024
Merged

Conversation

36degrees
Copy link
Contributor

@36degrees 36degrees commented May 28, 2024

Note

This PR is stacked on top of #5011 which must be merged first.

Also refactor the tests to:

  • improve the test descriptions and grouping
  • move duplicated code (like rendering the same example of a component) into beforeAll blocks
  • use toHaveAccessibleDescription and toHaveAccessibleName where appropriate
  • not use snapshots

Part of #5010.

Copy link

github-actions bot commented May 28, 2024

📋 Stats

File sizes

File Size
dist/govuk-frontend-development.min.css 113.37 KiB
dist/govuk-frontend-development.min.js 42.34 KiB
packages/govuk-frontend/dist/govuk/all.bundle.js 88.16 KiB
packages/govuk-frontend/dist/govuk/all.bundle.mjs 82.83 KiB
packages/govuk-frontend/dist/govuk/all.mjs 981 B
packages/govuk-frontend/dist/govuk/govuk-frontend-component.mjs 359 B
packages/govuk-frontend/dist/govuk/govuk-frontend.min.css 113.36 KiB
packages/govuk-frontend/dist/govuk/govuk-frontend.min.js 42.33 KiB
packages/govuk-frontend/dist/govuk/i18n.mjs 5.55 KiB
packages/govuk-frontend/dist/govuk/init.mjs 4.86 KiB

Modules

File Size (bundled) Size (minified)
all.mjs 78.45 KiB 40.31 KiB
accordion.mjs 22.71 KiB 12.85 KiB
button.mjs 5.98 KiB 2.69 KiB
character-count.mjs 22.4 KiB 9.92 KiB
checkboxes.mjs 5.83 KiB 2.83 KiB
error-summary.mjs 7.89 KiB 3.46 KiB
exit-this-page.mjs 17.1 KiB 9.26 KiB
header.mjs 4.46 KiB 2.6 KiB
notification-banner.mjs 6.26 KiB 2.62 KiB
password-input.mjs 15.15 KiB 7.25 KiB
radios.mjs 4.83 KiB 2.38 KiB
skip-link.mjs 4.39 KiB 2.18 KiB
tabs.mjs 10.13 KiB 6.11 KiB

View stats and visualisations on the review app


Action run for f751c3b

Copy link

github-actions bot commented May 28, 2024

Rendered HTML changes to npm package

diff --git a/packages/govuk-frontend/dist/govuk/components/input/template-disabled.html b/packages/govuk-frontend/dist/govuk/components/input/template-disabled.html
new file mode 100644
index 000000000..486045b79
--- /dev/null
+++ b/packages/govuk-frontend/dist/govuk/components/input/template-disabled.html
@@ -0,0 +1,6 @@
+<div class="govuk-form-group">
+  <label class="govuk-label" for="disabled-input">
+    Disabled input
+  </label>
+  <input class="govuk-input" id="disabled-input" name="" type="text" disabled>
+</div>
diff --git a/packages/govuk-frontend/dist/govuk/components/input/template-with-error-and-hint.html b/packages/govuk-frontend/dist/govuk/components/input/template-with-error-and-hint.html
new file mode 100644
index 000000000..6b14ce405
--- /dev/null
+++ b/packages/govuk-frontend/dist/govuk/components/input/template-with-error-and-hint.html
@@ -0,0 +1,12 @@
+<div class="govuk-form-group govuk-form-group--error">
+  <label class="govuk-label" for="with-error-hint">
+    National insurance number
+  </label>
+  <div id="with-error-hint-hint" class="govuk-hint">
+    It’s on your National Insurance card, benefit letter, payslip or P60. For example, ‘QQ 12 34 56 C’.
+  </div>
+  <p id="with-error-hint-error" class="govuk-error-message">
+    <span class="govuk-visually-hidden">Error:</span> Enter a National Insurance number in the correct format
+  </p>
+  <input class="govuk-input govuk-input--error" id="with-error-hint" name="with-error-hint" type="text" aria-describedby="with-error-hint-hint with-error-hint-error">
+</div>
diff --git a/packages/govuk-frontend/dist/govuk/components/input/template-with-error-message.html b/packages/govuk-frontend/dist/govuk/components/input/template-with-error-message.html
index e025b5b9c..35b100b90 100644
--- a/packages/govuk-frontend/dist/govuk/components/input/template-with-error-message.html
+++ b/packages/govuk-frontend/dist/govuk/components/input/template-with-error-message.html
@@ -2,11 +2,8 @@
   <label class="govuk-label" for="input-with-error-message">
     National Insurance number
   </label>
-  <div id="input-with-error-message-hint" class="govuk-hint">
-    It’s on your National Insurance card, benefit letter, payslip or P60. For example, ‘QQ 12 34 56 C’.
-  </div>
   <p id="input-with-error-message-error" class="govuk-error-message">
-    <span class="govuk-visually-hidden">Error:</span> Error message goes here
+    <span class="govuk-visually-hidden">Error:</span> Enter a National Insurance number in the correct format
   </p>
-  <input class="govuk-input govuk-input--error" id="input-with-error-message" name="test-name-3" type="text" aria-describedby="input-with-error-message-hint input-with-error-message-error">
+  <input class="govuk-input govuk-input--error" id="input-with-error-message" name="test-name-3" type="text" aria-describedby="input-with-error-message-error">
 </div>

Action run for f751c3b

Copy link

github-actions bot commented May 28, 2024

Other changes to npm package

diff --git a/packages/govuk-frontend/dist/govuk/components/input/fixtures.json b/packages/govuk-frontend/dist/govuk/components/input/fixtures.json
index 5b5223d03..0563db9ac 100644
--- a/packages/govuk-frontend/dist/govuk/components/input/fixtures.json
+++ b/packages/govuk-frontend/dist/govuk/components/input/fixtures.json
@@ -38,19 +38,36 @@
                 "label": {
                     "text": "National Insurance number"
                 },
-                "hint": {
-                    "text": "It’s on your National Insurance card, benefit letter, payslip or P60. For example, ‘QQ 12 34 56 C’."
-                },
                 "id": "input-with-error-message",
                 "name": "test-name-3",
                 "errorMessage": {
-                    "text": "Error message goes here"
+                    "text": "Enter a National Insurance number in the correct format"
                 }
             },
             "hidden": false,
             "description": "",
             "previewLayoutModifiers": [],
-            "html": "<div class=\"govuk-form-group govuk-form-group--error\">\n  <label class=\"govuk-label\" for=\"input-with-error-message\">\n    National Insurance number\n  </label>\n  <div id=\"input-with-error-message-hint\" class=\"govuk-hint\">\n    It’s on your National Insurance card, benefit letter, payslip or P60. For example, ‘QQ 12 34 56 C’.\n  </div>\n  <p id=\"input-with-error-message-error\" class=\"govuk-error-message\">\n    <span class=\"govuk-visually-hidden\">Error:</span> Error message goes here\n  </p>\n  <input class=\"govuk-input govuk-input--error\" id=\"input-with-error-message\" name=\"test-name-3\" type=\"text\" aria-describedby=\"input-with-error-message-hint input-with-error-message-error\">\n</div>"
+            "html": "<div class=\"govuk-form-group govuk-form-group--error\">\n  <label class=\"govuk-label\" for=\"input-with-error-message\">\n    National Insurance number\n  </label>\n  <p id=\"input-with-error-message-error\" class=\"govuk-error-message\">\n    <span class=\"govuk-visually-hidden\">Error:</span> Enter a National Insurance number in the correct format\n  </p>\n  <input class=\"govuk-input govuk-input--error\" id=\"input-with-error-message\" name=\"test-name-3\" type=\"text\" aria-describedby=\"input-with-error-message-error\">\n</div>"
+        },
+        {
+            "name": "with error and hint",
+            "options": {
+                "id": "with-error-hint",
+                "name": "with-error-hint",
+                "label": {
+                    "text": "National insurance number"
+                },
+                "errorMessage": {
+                    "text": "Enter a National Insurance number in the correct format"
+                },
+                "hint": {
+                    "text": "It’s on your National Insurance card, benefit letter, payslip or P60. For example, ‘QQ 12 34 56 C’."
+                }
+            },
+            "hidden": false,
+            "description": "",
+            "previewLayoutModifiers": [],
+            "html": "<div class=\"govuk-form-group govuk-form-group--error\">\n  <label class=\"govuk-label\" for=\"with-error-hint\">\n    National insurance number\n  </label>\n  <div id=\"with-error-hint-hint\" class=\"govuk-hint\">\n    It’s on your National Insurance card, benefit letter, payslip or P60. For example, ‘QQ 12 34 56 C’.\n  </div>\n  <p id=\"with-error-hint-error\" class=\"govuk-error-message\">\n    <span class=\"govuk-visually-hidden\">Error:</span> Enter a National Insurance number in the correct format\n  </p>\n  <input class=\"govuk-input govuk-input--error\" id=\"with-error-hint\" name=\"with-error-hint\" type=\"text\" aria-describedby=\"with-error-hint-hint with-error-hint-error\">\n</div>"
         },
         {
             "name": "with width-2 class",
@@ -290,6 +307,20 @@
             "previewLayoutModifiers": [],
             "html": "<div class=\"govuk-form-group\">\n  <label class=\"govuk-label\" for=\"input-with-autocapitalize-off\">\n    Autocapitalize is turned off\n  </label>\n  <input class=\"govuk-input\" id=\"input-with-autocapitalize-off\" name=\"autocapitalize\" type=\"text\" autocapitalize=\"none\">\n</div>"
         },
+        {
+            "name": "disabled",
+            "options": {
+                "label": {
+                    "text": "Disabled input"
+                },
+                "id": "disabled-input",
+                "disabled": true
+            },
+            "hidden": false,
+            "description": "",
+            "previewLayoutModifiers": [],
+            "html": "<div class=\"govuk-form-group\">\n  <label class=\"govuk-label\" for=\"disabled-input\">\n    Disabled input\n  </label>\n  <input class=\"govuk-input\" id=\"disabled-input\" name=\"\" type=\"text\" disabled>\n</div>"
+        },
         {
             "name": "with prefix",
             "options": {
@@ -551,26 +582,6 @@
             "previewLayoutModifiers": [],
             "html": "<div class=\"govuk-form-group govuk-form-group--error\">\n  <label class=\"govuk-label\" for=\"with-error-describedby\">\n    With error describedBy\n  </label>\n  <p id=\"with-error-describedby-error\" class=\"govuk-error-message\">\n    <span class=\"govuk-visually-hidden\">Error:</span> Error message\n  </p>\n  <input class=\"govuk-input govuk-input--error\" id=\"with-error-describedby\" name=\"with-error-describedby\" type=\"text\" aria-describedby=\"test-target-element with-error-describedby-error\">\n</div>"
         },
-        {
-            "name": "with error and hint",
-            "options": {
-                "id": "with-error-hint",
-                "name": "with-error-hint",
-                "label": {
-                    "text": "With error and hint"
-                },
-                "errorMessage": {
-                    "text": "Error message"
-                },
-                "hint": {
-                    "text": "Hint"
-                }
-            },
-            "hidden": true,
-            "description": "",
-            "previewLayoutModifiers": [],
-            "html": "<div class=\"govuk-form-group govuk-form-group--error\">\n  <label class=\"govuk-label\" for=\"with-error-hint\">\n    With error and hint\n  </label>\n  <div id=\"with-error-hint-hint\" class=\"govuk-hint\">\n    Hint\n  </div>\n  <p id=\"with-error-hint-error\" class=\"govuk-error-message\">\n    <span class=\"govuk-visually-hidden\">Error:</span> Error message\n  </p>\n  <input class=\"govuk-input govuk-input--error\" id=\"with-error-hint\" name=\"with-error-hint\" type=\"text\" aria-describedby=\"with-error-hint-hint with-error-hint-error\">\n</div>"
-        },
         {
             "name": "with error, hint and describedBy",
             "options": {

Action run for f751c3b

@36degrees 36degrees force-pushed the jsdom-test-matchers branch from ea3d9fc to 182b60e Compare May 28, 2024 09:56
Comment on lines -183 to -184
hint:
text: It’s on your National Insurance card, benefit letter, payslip or P60. For example, ‘QQ 12 34 56 C’.
Copy link
Member

Choose a reason for hiding this comment

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

question Thinking that hint might have been there to check where the error would be rendered relative to the hint. By removing it, and not having a test for it, we're no longer checking that it goes 'label, hint, error, field', which I guess is important from a design perspective.

Copy link
Member

Choose a reason for hiding this comment

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

I'd be keen to add that hint back and add a test that checks the order of elements inside the govuk-form-group, what do you say?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There's a separate example which includes both an error message and a hint further down:

https://github.com/alphagov/govuk-frontend/pull/5017/files#diff-64dabf8e89e534dc5f047bb2159938a010e94c221cc75b9b7196d04dcb8a5ceeR451-R461

With a test for the ordering of aria-describedby here:

https://github.com/alphagov/govuk-frontend/pull/5017/files#diff-54a0f41b55758264a220e2eb6e3fba6d181769d8541a05acbe05a1c0963b46bcR241-R261

I think it would make sense to make that example visible though, I'll update it to match this example before I removed the hint.

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'd be keen to add that hint back and add a test that checks the order of elements inside the govuk-form-group, what do you say?

Do you mean the order they appear in the description, or in the DOM?

Copy link
Member

Choose a reason for hiding this comment

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

I meant the order they appear in the DOM, sorry for the confusion. Thinking it's worth having that checked as it's been a design decision at some point (and a check the snapshot was doing for us), but won't die on that hill (don't think we have anything looking at the position in the DOM of the prefix and suffix, for example, which are equally tied to design decisions).

I think it would make sense to make that example visible though, I'll update it to match this example before I removed the hint.

That's a good shout 😊

Copy link
Member

Choose a reason for hiding this comment

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

Had a look at Percy and didn't see anything beyond the default example that would test the order (visually at least).

The next commit will refactor the tests to make use of jsdom which requires us to update the filename so that we get the jsdom test environment.

Doing this in two commits so that git shows the changes to the file rather than treating it as an addition and a deletion.
@36degrees 36degrees force-pushed the jsdom-input-template-tests branch from 794a2cd to e3a41af Compare May 28, 2024 10:00
@36degrees 36degrees force-pushed the jsdom-input-template-tests branch from e3a41af to 3be210a Compare May 28, 2024 16:36
Base automatically changed from jsdom-test-matchers to main May 28, 2024 17:12
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.

Neat, seems all test are there 🙌🏻 I'd say we can add tests for the DOM order at a later stage, I can add an issue for that.

Thinking we could update the tests like it('includes the hint') and similar tests to become tests checking the DOM order with something like expect($component.matches('.govuk-form-group .govuk-hint ~ .govuk-input')).toBe(true), a bit like what happens for checking the wrapper is here when prefix and suffix are used.

Nothing blocking for merging, but would be good if you could fix the quotes in the test for the 0 value (unless I'm mistaken) and document the reason for the addition of the paragraph hosting the description (a thumbs up on my question if my supposition was right should be enough of a trace for the future 😊 ), cheers!.

const $component = $('.govuk-input')
expect($component.attr('pattern')).toBe('[0-9]*')
it('the input is named by the label', () => {
expect($component).toHaveAccessibleName($label.textContent.trim())
Copy link
Member

Choose a reason for hiding this comment

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

😍

Comment on lines -183 to -184
hint:
text: It’s on your National Insurance card, benefit letter, payslip or P60. For example, ‘QQ 12 34 56 C’.
Copy link
Member

Choose a reason for hiding this comment

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

I meant the order they appear in the DOM, sorry for the confusion. Thinking it's worth having that checked as it's been a design decision at some point (and a check the snapshot was doing for us), but won't die on that hill (don't think we have anything looking at the position in the DOM of the prefix and suffix, for example, which are equally tied to design decisions).

I think it would make sense to make that example visible though, I'll update it to match this example before I removed the hint.

That's a good shout 😊

Comment on lines -183 to -184
hint:
text: It’s on your National Insurance card, benefit letter, payslip or P60. For example, ‘QQ 12 34 56 C’.
Copy link
Member

Choose a reason for hiding this comment

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

Had a look at Percy and didn't see anything beyond the default example that would test the order (visually at least).

Comment on lines +159 to +162
const $additionalDescription = document.createElement('p')
$additionalDescription.id = 'test-target-element'
$additionalDescription.textContent = 'Additional description'
document.body.appendChild($additionalDescription)
Copy link
Member

Choose a reason for hiding this comment

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

question Is this necessary for toHaveAccessibleDescription to function properly (finding the element with the relevant ID and computing its accessible name for use as a description, which it cannot do if the element is not in the DOM)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, if we want to be able to use toHaveAccessibleDescription and be confident that the external element referenced by the describedBy option is included.

The alternative would be to assert against the value of the aria-describedby attribute itself (like the old tests did) but I felt it was better to have just one way of doing it?

Copy link
Member

Choose a reason for hiding this comment

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

I agree, it's much nicer to use the 'toHaveAccessibleDescription' rather than look at the `aria-describedby. Just wanted to check I understood why that paragraph was there 😊

Also refactor the tests to:

- move duplicated code (like rendering the same example of a component) into `beforeAll` blocks
- use `toHaveAccessibleDescription` and `toHaveAccessibleName` where appropriate
- not use snapshots
@36degrees 36degrees force-pushed the jsdom-input-template-tests branch from 3be210a to f751c3b Compare May 31, 2024 09:01
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-5017 May 31, 2024 09:01 Inactive
@36degrees
Copy link
Contributor Author

Force-pushed a change to remove the quotes around zero to resolve #5017 (comment).

@36degrees 36degrees requested a review from romaricpascal May 31, 2024 09:03
@romaricpascal
Copy link
Member

@36degrees Looks good to go ⛵

@36degrees 36degrees merged commit 1c90480 into main May 31, 2024
48 checks passed
@36degrees 36degrees deleted the jsdom-input-template-tests branch May 31, 2024 09:59
domoscargin pushed a commit that referenced this pull request Jun 19, 2024
Update input template tests to use jsdom
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