-
Notifications
You must be signed in to change notification settings - Fork 1
fix(Dropdown): Remove timeouts, Fix ref bug #42
Conversation
Fixing some tests for escape/close and also adding one for enter, and then will open back up. |
if (autoclose) { | ||
// Use timeout to delay examination of activeElement until after blur/focus | ||
// events have been processed. | ||
setTimeout(() => { |
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.
This setTimeout was causing a bug with blur when inputs were clicked. Managing whether blur is being triggered via the state now allows inputs clicks to trigger blur. Blur is now handled via relatedTarget
.
Codecov Report
@@ Coverage Diff @@
## master #42 +/- ##
=======================================
Coverage 68.36% 68.36%
=======================================
Files 22 22
Lines 433 433
Branches 91 92 +1
=======================================
Hits 296 296
Misses 107 107
Partials 30 30
Continue to review full report at Codecov.
|
@cehsu Could you elaborate on why enzyme was added? It'd be preferable to not have two testing frameworks in play simultaneously |
@kylealwyn the initial test framework is based on direct DOM manipulation, but as noted here, I wasn't able to trigger the toggle that way in the console. Therefore enzyme which is more closely tied to the component lifecycle, was tested out, and it does the job. |
@cehsu We were using blur successfully in the tests before, so I'm not entirely convinced that's the case: // Some issues with fireEvent.focus: https://github.com/kentcdodds/react-testing-library/issues/276#issuecomment-473392827
renderUtils.wrapper.focus();
renderUtils.getByTestId('dropdown-menu').blur(); |
@kylealwyn thanks again, I think removing the setTimeout may have affected the ability to use blur directly. I can push up a version using the other library for reference. |
@cehsu I'd prefer we stick with react-testing-library so if you point me to the exact issue you were encountering, I'd like to take a look |
Thanks @kylealwyn. The original (now failing) test has been restored for reference. |
I'm restored the enzyme tests for now since they work, if that's alright. We can revisit the libraries used again. |
@cehsu I'd prefer that we skip the failing test for now with manual inspection rather than adding an entirely new test framework |
@cehsu Could we tackle the tests or are they still cryptically failing? |
src/Dropdown/Dropdown.js
Outdated
}); | ||
} | ||
}, [isOpen]); | ||
|
||
useKeyPress('Escape', () => { | ||
if (isOpen) { | ||
triggerRef.current.focus(); |
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 manually implement returnFocus
here, so that inputs can be directly focused (without a second click), which was prevented by using FocusLock's returnFocus
.
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.
Works and looks great. Thanks for seeing this through @cehsu. If tests are giving too much trouble, we can loop back.
@kylealwyn, good idea, the blur test re-enabled, I wasn't able to get the keypress trigger event tests to work however. I tried something along these lines:
|
These changes remove the timeout in handleMenuBlur, which was causing issues when attempting to focus on inputs while the dropdown was open, and would cause the menu not to close on blur.
Also this fixes a bug, when attempting to open the dropdown via the enter key (when menuRef.current was null).