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

Convert remaining components to functional components #464

Open
wants to merge 11 commits into
base: hooks
Choose a base branch
from

Conversation

m4theushw
Copy link
Contributor

What does it do?

  • Pulls all commits from develop and applies into hooks branch.
  • Converts all class components to functional components.
  • Fixes tests to work with hooks.

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • Updated documentation (if applicable)
  • Added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • My changes generate no new warnings

const keepDropdownActive = useRef(false)
const callbackRef = useRef()

const setStateWithCallback = (newState, callback) => {
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 created it to emulate the callback arg in the old setState. In the future, this component can be refactored to not need it.

@coveralls
Copy link

coveralls commented Feb 12, 2021

Pull Request Test Coverage Report for Build 1533

  • 164 of 167 (98.2%) changed or added relevant lines in 7 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.5%) to 94.541%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/tree/index.js 29 32 90.63%
Totals Coverage Status
Change from base Build 1507: 0.5%
Covered Lines: 669
Relevant Lines: 688

💛 - Coveralls

@mrchief
Copy link
Collaborator

mrchief commented Feb 12, 2021

This is awesome work @m4theushw !! Thanks for sending this. I'm gonna go over it this weekend and having the long weekend helps.

This is the first step towards having headless components and I'm glad we're heading in that direction.

Copy link
Collaborator

@mrchief mrchief left a comment

Choose a reason for hiding this comment

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

Findings so far...

__snapshots__/src/index.test.js.md Show resolved Hide resolved
__snapshots__/src/trigger/index.test.js.md Outdated Show resolved Hide resolved
@@ -29,7 +29,7 @@ test('some key presses opens dropdown on keyboardNavigation', t => {
;['ArrowUp', 'ArrowDown', 'Home', 'PageUp', 'End', 'PageDown', 'a', 'B'].forEach(key => {
const wrapper = mount(<DropdownTreeSelect data={tree} />)
triggerOnKeyboardKeyDown(wrapper, key)
t.true(wrapper.state().showDropdown)
t.true(wrapper.exists('.dropdown-content'))
Copy link
Collaborator

Choose a reason for hiding this comment

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

I vaguely remember having this kind of check which wasn't accurately detecting the dropdown state. state.showDropdown was more trusty as it gets set when the event handler gets called indicating the dropdown is being shown indeed. The only part trust in that case is the CSS properties actually render the div visible but we don't need to test the browser's CSS engine, so it was an acceptable compromise.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could check if the role is "listbox" or "tree". That would be a more recommended approach if React Testing Library was used.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Alright. So let's continue with this and tackle it later if we run into similar issues. Maybe moving to RTL could be another PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, dropping enzyme in favor of RTL would be a great improvement in testing quality.

src/index.keyboardNav.test.js Show resolved Hide resolved
wrapper.find('.search').simulate('click')
t.true(wrapper.exists('.dropdown-content'))
const event = new MouseEvent('click', { bubbles: true, cancelable: true })
global.document.dispatchEvent(event)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is interesting. So with this dispatch, do we still need the simulate('click')? Seems like they are both asserting the same thing?

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 kept the simulate('click') because the old test had it, but it could be removed. The purpose of the dispatch is to ensure that, if the user clicks outside the dropdown, it stays open.


const searchInput = (
<Input
ref={searchInputRef}
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 had some problems passing the ref as inputRef. It seems like AVA can't deal with refs passed through custom named props.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Trying to understand here - don't we do that already? What was the exact error/issue you ran into?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The error I got when using inputRef is:

 PointerLookupError {
    index: 10,
    message: 'Could not deserialize buffer: pointer 10 could not be resolved',
}

I found this relatated issue. I tried to upgrade AVA to the latest version but it didn't solve the problem.

@github-actions
Copy link

github-actions bot commented Dec 1, 2021

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 10 days if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the stale label Dec 1, 2021
@mrchief mrchief added wip Work In Progress and removed stale labels Dec 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wip Work In Progress
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants