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 table component #472

Merged
merged 3 commits into from
Feb 5, 2018
Merged

Add tests for table component #472

merged 3 commits into from
Feb 5, 2018

Conversation

kr8n3r
Copy link

@kr8n3r kr8n3r commented Jan 29, 2018

We break this down into groups (as represented by describe blocks).

Trello https://trello.com/c/mdqq5RnP/645-automated-tests-for-table-component

@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-472 January 29, 2018 11:26 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-472 January 29, 2018 11:28 Inactive
Copy link
Contributor

@NickColley NickColley left a comment

Choose a reason for hiding this comment

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

I think I prefer to be verbose and have lots of assertions rather than looping logic since it's hard to know what your intent was.

Not sure this is a blocker though.

@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-472 January 29, 2018 12:37 Inactive
@36degrees
Copy link
Contributor

36degrees commented Jan 29, 2018

I think I would agree with Nick here. I find it really hard to follow the tests and to understand what the intent is. I think there's as much chance of introducing bugs in the logic of the tests as there is in having bugs in the code we're trying to test.

There's some good guidance on avoiding logic in tests here:

https://github.com/mawrkus/js-unit-testing-guide#avoid-logic-in-your-tests

@kr8n3r
Copy link
Author

kr8n3r commented Jan 29, 2018

was hoping to cut down on the number of loops for the default example for each cell and row, when checking data.
how do you test if the data to render table is correct?

@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-472 January 29, 2018 14:05 Inactive
@kr8n3r kr8n3r changed the title WIP: Add Jest test for table component WIP: Add tests for table component Jan 29, 2018
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-472 January 29, 2018 14:09 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-472 January 29, 2018 14:23 Inactive
@NickColley
Copy link
Contributor

NickColley commented Jan 29, 2018

@igloosi I suppose since we only have three rows it'd look like

loop 3
  assertion loop.item
endloop
assertion 'Item 1'
assertion 'Item 2'
assertion 'Item 3'

So the compromise is verbose tests, which for me personally is OK since looping behaviours are harder to grok and easier to hide bad assertions?

Hardcoding assertions feels dodgy since you'd want to have DRY code but in this case of testing it makes the intent of what you were asserting explicit.

@kr8n3r
Copy link
Author

kr8n3r commented Jan 29, 2018

rewritten for more verbosity

@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-472 January 29, 2018 16:05 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-472 January 29, 2018 16:08 Inactive
@kr8n3r kr8n3r changed the title WIP: Add tests for table component Add tests for table component Jan 29, 2018
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-472 January 30, 2018 08:59 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-472 January 30, 2018 09:02 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-472 January 30, 2018 16:37 Inactive
@kr8n3r kr8n3r changed the title Add tests for table component Add tests for table component Jan 30, 2018
Copy link
Contributor

@alex-ju alex-ju left a comment

Choose a reason for hiding this comment

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

Overall looks good to me. 👍

We have a lot of children 'search' that can be potentially reduced by using our BEM selectors (e.g. govuk-c-table__cell) to get a specific child element, otherwise being class-agnostic gives some flexibility, but I'm not sure we need it.

const $componentBody = $component.children('tbody')
const $componentBodyRow = $componentBody.children('tr')

expect(parseInt($componentBodyRow.eq(0).children().eq(1).attr('colspan'), 10)).toBe(args.rows[0][1].colspan)
Copy link
Contributor

Choose a reason for hiding this comment

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

If we used toEqual here, could we get rid of the casting?

Copy link
Author

Choose a reason for hiding this comment

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

hmmm, with toEqual:

Expected value to equal:
2
Received:
"2"

@kr8n3r
Copy link
Author

kr8n3r commented Jan 31, 2018

REVISED

@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-472 January 31, 2018 11:35 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-472 January 31, 2018 11:46 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-472 February 1, 2018 11:34 Inactive
const { $ } = render('table', examples['table-with-caption-and-head'])
const $caption = $('.govuk-c-table__caption')

expect($caption).toHaveLength(1)
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we need this assertion since the next one down will fail if the caption does not exist.

})
})

describe('variant', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this need to be in a describe block? What is it describing?

Copy link
Author

Choose a reason for hiding this comment

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

removed

const $tableRow = $component.find('.govuk-c-table .govuk-c-table__body .govuk-c-table__row')

expect($tableRow.eq(0).children().eq(0).get(0).tagName).toEqual('th')
expect($tableRow.eq(0).children().eq(0).hasClass('govuk-c-table__header')).toBeTruthy()
Copy link
Contributor

Choose a reason for hiding this comment

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

We could swap this around and make this a bit easier to read by doing

Instead of

expect($tableRow.eq(0).children().eq(0).get(0).tagName).toEqual('th')
 expect($tableRow.eq(0).children().eq(0).hasClass('govuk-c-table__header')).toBeTruthy()

try

const $tableBody = $component.find('.govuk-c-table .govuk-c-table__body')

const $firstTableRow = $tableBody.find('.govuk-c-table__row:first-child')
const $firstTableHeader = $firstTableRow.find('govuk-c-table__header:first-child')
expect($firstTableHeader.get(0).tagName).toEqual('th')

const $lastTableRow = $tableBody.find('.govuk-c-table__row:last-child')
const $lastTableHeader = $lastTableRow.find('govuk-c-table__header:last-child')
expect($lastTableHeader.get(0).tagName).toEqual('th')

})
})

describe('variant', () => {
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 get these two tests outside 'variant', even if they're using examples from yaml. also, and I know I'm being picky with this one, but can we order them somehow in a similar way we have them in the table or arguments (classes, caption..) would make it easier to follow and make sure we cover all the arguments.

Copy link
Contributor

@NickColley NickColley left a comment

Choose a reason for hiding this comment

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

We need to replace

const { $ } = render()

with

const $ = render()

@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-472 February 2, 2018 15:30 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-472 February 4, 2018 20:16 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-472 February 4, 2018 20:17 Inactive
@kr8n3r
Copy link
Author

kr8n3r commented Feb 4, 2018

@NickColley and @alex-ju update based on comments

Copy link
Contributor

@alex-ju alex-ju left a comment

Choose a reason for hiding this comment

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

Needs linting fix, otherwise looks neat 👍


const examples = getExamples('table')
describe('Table', () => {

Copy link
Contributor

Choose a reason for hiding this comment

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

Block must not be padded by blank lines

Copy link
Author

Choose a reason for hiding this comment

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

fixed

expect($firstTableHeader.get(0).tagName).toEqual('th')
expect($firstTableHeader.attr('scope')).toEqual('row')
expect($firstTableHeader.hasClass('govuk-c-table__header')).toBeTruthy()

Copy link
Contributor

Choose a reason for hiding this comment

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

More than one blank line not allowed

Copy link
Author

Choose a reason for hiding this comment

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

fixed

expect($lastTableHeader.get(0).tagName).toEqual('th')
expect($lastTableHeader.attr('scope')).toEqual('row')
expect($lastTableHeader.hasClass('govuk-c-table__header')).toBeTruthy()

Copy link
Contributor

Choose a reason for hiding this comment

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

Block must not be padded by blank lines

Copy link
Author

Choose a reason for hiding this comment

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

fixed

Jani Kraner added 3 commits February 5, 2018 10:25
We break this down into groups (as represented by describe blocks).

1. Default table
2. Variants
3. Individual attributes
Copy link
Contributor

@alex-ju alex-ju left a comment

Choose a reason for hiding this comment

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

@NickColley, when you have a few minutes please have a look at the changes so we can unblock this one. thanks!

@NickColley NickColley dismissed their stale review February 5, 2018 12:47

Refactoring based on my comment has been done.

@kr8n3r kr8n3r merged commit 3622ee1 into master Feb 5, 2018
@kr8n3r kr8n3r deleted the table-component-tests branch February 5, 2018 12:53
@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