Skip to content

test and refactor current push/pull button behaviour - #7042

Merged
niik merged 1 commit into
developmentfrom
component-tests-and-refactor-push-pull-internals
Mar 26, 2019
Merged

niik merged 1 commit into
developmentfrom
component-tests-and-refactor-push-pull-internals

Conversation

@shiftkey

@shiftkey shiftkey commented Mar 8, 2019 •

Copy link
Copy Markdown
Member

Overview

Closes #6985

Description

  • add component testing to yarn test step
  • setup test suite for PushPullButton to cover majority of output cases
  • rewrite PushPullButton.render to be more maintainable
  • rebase changes on top of [rebase] enable --force-with-lease after rebasing #6981 once merged
  • add more tests to cover force push scenario
  • extract component tests out of this branch
  • write up some notes about the experience of doing this component testing

Notes

Diff details aside, the size of the component in the production bundle increased 18% (from 3048 characters to 3615 characters, so still less than 4KB) for the component, which is mostly because we no longer have a central React.createElement inside render().

You can see the human-readable before-vs-after (with whitespace and indenting) output in this gist: https://gist.github.com/shiftkey/a3b4ddb7c7e3537e28127279cd5969c7

Release notes

Notes: no-notes

Comment thread app/src/ui/lib/props-type.ts Resolved
@shiftkey
shiftkey force-pushed the component-tests-and-refactor-push-pull-internals branch from 08dfc61 to 96444b2 Compare March 8, 2019 19:45
@codecov

This comment has been minimized.

1 similar comment
@codecov

This comment has been minimized.

@shiftkey
shiftkey force-pushed the component-tests-and-refactor-push-pull-internals branch from 0a722f3 to 68ae4ae Compare March 11, 2019 15:11
@shiftkey
shiftkey force-pushed the component-tests-and-refactor-push-pull-internals branch 6 times, most recently from 1ca467f to 577cfef Compare March 19, 2019 20:15
@shiftkey

shiftkey commented Mar 25, 2019 •

Copy link
Copy Markdown
Member Author

I've archived the tests I used in here over in this branch as a reference to complement the notes below about my component testing adventures.

What did I do?

This PR was a chance to explore testing React components while I refactor a component, because this scenario felt like a good fit for it, rather than other scenarios that I've been contemplating.

Why this area?

This component felt like a good fit for testing for a few reasons:

  • it's a component that has been around basically forever, accruing little bits of technical debt over time
  • it's purpose have changed over time, in particular to support new states and actions
  • while it currently has a lot of props to show, the rendered DOM is relatively stable

What worked?

It was helpful to use these tests as a regression suite while I was refactoring. What I did was build up a number of cases that I knew the component needed to support, and run these tests to store a snapshot of the rendered DOM:

it(`when detached HEAD and rebase in progress`, () => {
const props = {
...defaultProps,
remoteName: 'origin',
tipState: TipState.Detached,
rebaseInProgress: true,
}
const { container } = render(<PushPullButton {...props} />)
expect(container).toMatchSnapshot()
})

This was a bit exploratory, and required looking over the internals to understand what it was looking for, because of the large number of props this component supports. I think unifying this logic outside of the component, and returning a union type prop of the possible states, would be the next iteration on the API here.

Once I had this set of tests, I could contemplate changes to the code. When I was originally refactoring this component I went through this sort of repetitive process:

  1. identify a scenario inside render() that could be extracted out to it's own function
  2. make a change
  3. run the tests to verify the tests aren't broken
  4. commit the change
  5. go back to step 1

Chipping away at the code eventually got me to what is currently in this PR - having one place where the conditions are checked and each scenario becomes a standalone function.

What didn't work?

I wouldn't recommend this approach for other situations:

  • tests while writing new components - as things change, even during development, I can see a lot of time wasted on updating snapshots in tests
  • tests wrapping large/complex components - because of how Jest snapshots represent the underlying DOM, I can see situations where changing a core component like Button would cause cascading test failures in areas that contributors might not understand the impact (or the resolution)
  • tests with interactions to see how the component re-render - I've been able to do this in other situations, but the value of these tests isn't clear to me

I also had a shot at enabling code coverage for these "component tests" but it clashed with the unit test coverage and made CodeCov submit disappointed comments into this draft PR about my coverage failing. If we want to go down this path, we'd need to better organize the unit tests to appease Codecov.

Would I do it again?

Yes. The other case I've been exploring is how we can verify React components that currently use APIs which will be removed in the next major version of React, to ensure we can effectively migrate to the new APIs without breaking functionality.

@shiftkey
shiftkey marked this pull request as ready for review March 25, 2019 11:54
@shiftkey
shiftkey force-pushed the component-tests-and-refactor-push-pull-internals branch from 577cfef to ba0f86c Compare March 25, 2019 11:55
@shiftkey shiftkey added ready-for-review Pull Requests that are ready to be reviewed by the maintainers tech-debt Issues and pull requests related to addressing technical debt or improving the codebase labels Mar 25, 2019
@shiftkey
shiftkey force-pushed the component-tests-and-refactor-push-pull-internals branch from ba0f86c to 1e099c5 Compare March 25, 2019 19:50
@niik niik self-assigned this Mar 26, 2019
@niik
niik merged commit f0c119f into development Mar 26, 2019
@niik
niik deleted the component-tests-and-refactor-push-pull-internals branch March 26, 2019 08:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-review Pull Requests that are ready to be reviewed by the maintainers tech-debt Issues and pull requests related to addressing technical debt or improving the codebase

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants