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 tests for input macro #478

Merged
merged 2 commits into from
Feb 2, 2018
Merged

Add tests for input macro #478

merged 2 commits into from
Feb 2, 2018

Conversation

alex-ju
Copy link
Contributor

@alex-ju alex-ju commented Jan 29, 2018

This PR:

  • Adds tests to input component to ensure the markup is rendered correctly when providing arguments to the macro.
  • Updates CHANGELOG.MD.

Note to reviewers: if you feel like any example should be removed, let me know.

Trello Card

@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-478 January 29, 2018 15:55 Inactive
@alex-ju alex-ju force-pushed the add-tests-for-input-macro branch from d311bf5 to c13f2fb Compare January 29, 2018 15:55
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-478 January 29, 2018 15:56 Inactive
Copy link
Contributor

@36degrees 36degrees left a comment

Choose a reason for hiding this comment

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

Generally looking really good, though there's a couple of things that I think might want another look.

it('renders with label', () => {
const { $ } = render('input', examples.default)
const $label = $('.govuk-c-label')
expect($label.text()).toContain('National Insurance number')
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we have a test that the label is associated by id / for?

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, we do, but it's further up. What's the thinking being splitting these up into a separate context?

Copy link
Contributor Author

@alex-ju alex-ju Jan 29, 2018

Choose a reason for hiding this comment

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

From line 97 I tested the examples that we have defined in the yaml to somehow make sure we're not breaking them. Before that line are 'by default' tests that cover the arguments, logic and relationships.

it('renders with id', () => {
const { $ } = render('input', {
id: 'my-input',
name: 'my-input-name'
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't test for name, and given that in the example above we omit both id and name, do we want to do the same here and just provide an id? Conversely, if we're providing name and id in the other examples because they are 'required', should we include them in the classes example also?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed I started to add id and name because they are 'required', but I'm not sure it's the right approach and right now is inconsistent across examples. I would rather add the minimum number of attributes since we cannot really enforce the 'required' state.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed unused attributes for now, even if defined as 'required'.

@alex-ju alex-ju force-pushed the add-tests-for-input-macro branch from c13f2fb to b4607fd Compare January 29, 2018 17:47
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-478 January 29, 2018 17:47 Inactive
@alex-ju alex-ju force-pushed the add-tests-for-input-macro branch from b4607fd to ac6707c Compare January 29, 2018 17:51
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-478 January 29, 2018 17:51 Inactive
expect($label.attr('for')).toEqual('my-input')
})

it('renders with error message', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it make sense to split this test into two parts?

  1. it applies the error variant class if an error message is passed
  2. it renders an error message if passed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

split


it('renders with label', () => {
const { $ } = render('input', {
id: 'my-input',
Copy link
Contributor

Choose a reason for hiding this comment

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

This is redundant

Copy link
Contributor Author

@alex-ju alex-ju Jan 30, 2018

Choose a reason for hiding this comment

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

removed the other one that was testing the example and the label too

it('renders with attributes', () => {
const { $ } = render('input', {
attributes: {
'aria-describedby': 'my-label'
Copy link
Contributor

Choose a reason for hiding this comment

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

It might be worth using a different 'non-sensical' data attribute, only to avoid confusion for people scanning through who might mis-read this as inputs actually having or requiring some aria markup?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

replaced aria-describedby with 'non-sensical' data attribute


const $label = $('.govuk-c-label')
expect($label).toBeTruthy()
expect($label.attr('for')).toEqual('my-input')
Copy link
Contributor

Choose a reason for hiding this comment

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

Again, suggest this is split into two parts - one that tests that the label is rendered correctly (probably worth checking the text itself rather than just ensuring the element exists?) and a second test that ensures the label is correctly associated with the input?

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 expect the label to be tested within the label macro. We don't add any logic about the label in the input macro, except for setting label's for attribute equal to the input's id.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK. I still think it's worth having an explicit test for that for/id relationship.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

split

it('renders with hint text', () => {
const { $ } = render('input', examples['with-hint-text'])
const $hintText = $('.govuk-c-label__hint')
expect($hintText.text().trim()).toEqual('It’s on your National Insurance card, benefit letter, payslip or P60. For example, ‘QQ 12 34 56 C’.')
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be good to wrap this within 80 chars.

@alex-ju alex-ju force-pushed the add-tests-for-input-macro branch from ac6707c to 1284fd9 Compare January 30, 2018 10:22
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-478 January 30, 2018 10:22 Inactive
@alex-ju alex-ju force-pushed the add-tests-for-input-macro branch from 1284fd9 to 4d5edfb Compare January 30, 2018 10:27
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-478 January 30, 2018 10:27 Inactive
@alex-ju alex-ju force-pushed the add-tests-for-input-macro branch from 4d5edfb to 84a9c5c Compare January 30, 2018 10:32
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-478 January 30, 2018 10:32 Inactive
Copy link
Member

@hannalaakso hannalaakso left a comment

Choose a reason for hiding this comment

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

Looks good to me. Thanks for talking me through the rationale of how we're grouping tests @alex-ju (I'll write these up for our docs).

I don't know if you want to have another look at this PR @36degrees before it gets merged?

@alex-ju alex-ju force-pushed the add-tests-for-input-macro branch from 84a9c5c to 04370bf Compare February 2, 2018 11:01
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-478 February 2, 2018 11:01 Inactive
})

const $errorMessage = $('.govuk-c-error-message')
expect($errorMessage).toBeTruthy()
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we replace these truthy tests with snapshots?

@alex-ju alex-ju force-pushed the add-tests-for-input-macro branch from 04370bf to f5773be Compare February 2, 2018 11:54
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-478 February 2, 2018 11:54 Inactive
@alex-ju
Copy link
Contributor Author

alex-ju commented Feb 2, 2018

Updated with snapshots as discussed.

}
})

expect(htmlWithClassName($, '.govuk-c-label')).toMatchSnapshot()
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we wrap these snapshots tests with a description 'dependant components' or similar?

@alex-ju alex-ju merged commit c787e9a into master Feb 2, 2018
@alex-ju alex-ju deleted the add-tests-for-input-macro branch February 2, 2018 16:27
@alex-ju alex-ju mentioned this pull request Feb 20, 2018
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.

5 participants