You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We currently render the template to HTML and then load it into a Cheerio instance which we can use to check the state of the HTML by using Cheerio methods like hasClass.
it('renders with classes',()=>{const$=render('button',examples.classes)const$component=$('.govuk-button')expect($component.hasClass('app-button--custom-modifier')).toBeTruthy()})
After:
it('renders with classes',()=>{document.body.innerHTML=render('button',examples.grey)const$component=document.querySelector('.govuk-button')expect($component).toHaveClass('app-button--custom-modifier')})
It'd also allow us to use other more useful matchers like toHaveAccessibleDescription, toHaveName, toHaveRole which are simpler and better convey intention than checking the association as we currently do:
Before:
it('associates the input as "described by" the hint',()=>{const$=render('input',examples['with hint text'])const$input=$('.govuk-input')consthintId=$('.govuk-hint').attr('id')constdescribedBy=newRegExp(`${WORD_BOUNDARY}${hintId}${WORD_BOUNDARY}`)expect($input.attr('aria-describedby')).toMatch(describedBy)})
After:
it('associates the input as "described by" the hint',()=>{document.body.innerHTML=render('input',examples['with hint text'])const$input=document.querySelector('.govuk-input')expect($input).toHaveAccessibleDescription("It’s on your National Insurance card, benefit letter, payslip or P60. For example, ‘QQ 12 34 56 C’.")})
Finally, given we aren't using jQuery anywhere else in the codebase, this also means that we only need to remember a single syntax, rather than switching between 'vanilla JavaScript' in our JavaScript and the jQuery-esque Cheerio syntax.
Who needs to work on this
Developers
Who needs to review this
Developers
Done when
The content you are editing has changed. Please copy your edits and refresh the page.
What
Update the existing template tests to use jsdom and the matchers provided by
@testing-library/jsdom
rather than loading the HTML into a Cheerio instance.Remove the dependency on Cheerio.
Why
We currently render the template to HTML and then load it into a Cheerio instance which we can use to check the state of the HTML by using Cheerio methods like
hasClass
.However, if we instead used jsdom (through jest-environment-jsdom) we can make use of the matchers provided by
@testing-library/jsdom
).Before:
After:
It'd also allow us to use other more useful matchers like
toHaveAccessibleDescription
,toHaveName
,toHaveRole
which are simpler and better convey intention than checking the association as we currently do:Before:
After:
Finally, given we aren't using jQuery anywhere else in the codebase, this also means that we only need to remember a single syntax, rather than switching between 'vanilla JavaScript' in our JavaScript and the jQuery-esque Cheerio syntax.
Who needs to work on this
Developers
Who needs to review this
Developers
Done when
Update the template tests
Tidy up
The text was updated successfully, but these errors were encountered: