-
Notifications
You must be signed in to change notification settings - Fork 320
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
Conversation
3a7132b
to
7869b0e
Compare
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.
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.
7869b0e
to
69780ef
Compare
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 |
was hoping to cut down on the number of loops for the default example for each cell and row, when checking data. |
69780ef
to
f657fb9
Compare
f657fb9
to
e4fa317
Compare
e4fa317
to
48d365c
Compare
@igloosi I suppose since we only have three rows it'd look like
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. |
rewritten for more verbosity |
48d365c
to
a73f3b6
Compare
a73f3b6
to
87bd131
Compare
table
component
87bd131
to
3de46fa
Compare
3de46fa
to
0ce652a
Compare
0ce652a
to
d8822f2
Compare
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.
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) |
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.
If we used toEqual
here, could we get rid of the casting?
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.
hmmm, with toEqual:
Expected value to equal:
2
Received:
"2"
REVISED |
d8822f2
to
07dd601
Compare
07dd601
to
d078cd2
Compare
d078cd2
to
c051427
Compare
const { $ } = render('table', examples['table-with-caption-and-head']) | ||
const $caption = $('.govuk-c-table__caption') | ||
|
||
expect($caption).toHaveLength(1) |
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.
I don't think we need this assertion since the next one down will fail if the caption does not exist.
}) | ||
}) | ||
|
||
describe('variant', () => { |
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.
Does this need to be in a describe block? What is it describing?
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.
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() |
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.
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', () => { |
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.
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.
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.
We need to replace
const { $ } = render()
with
const $ = render()
c051427
to
0947e02
Compare
42b051b
to
aaaea1c
Compare
@NickColley and @alex-ju update based on comments |
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.
Needs linting fix, otherwise looks neat 👍
|
||
const examples = getExamples('table') | ||
describe('Table', () => { | ||
|
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.
Block must not be padded by blank lines
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.
fixed
expect($firstTableHeader.get(0).tagName).toEqual('th') | ||
expect($firstTableHeader.attr('scope')).toEqual('row') | ||
expect($firstTableHeader.hasClass('govuk-c-table__header')).toBeTruthy() | ||
|
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.
More than one blank line not allowed
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.
fixed
expect($lastTableHeader.get(0).tagName).toEqual('th') | ||
expect($lastTableHeader.attr('scope')).toEqual('row') | ||
expect($lastTableHeader.hasClass('govuk-c-table__header')).toBeTruthy() | ||
|
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.
Block must not be padded by blank lines
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.
fixed
We break this down into groups (as represented by describe blocks). 1. Default table 2. Variants 3. Individual attributes
aaaea1c
to
e859d99
Compare
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.
@NickColley, when you have a few minutes please have a look at the changes so we can unblock this one. thanks!
Refactoring based on my comment has been done.
We break this down into groups (as represented by describe blocks).
Trello https://trello.com/c/mdqq5RnP/645-automated-tests-for-table-component