Skip to content
This repository has been archived by the owner on Oct 6, 2020. It is now read-only.

fix(Dropdown): Remove timeouts, Fix ref bug #42

Merged
merged 20 commits into from
May 15, 2019
Merged

Conversation

cehsu
Copy link
Contributor

@cehsu cehsu commented May 2, 2019

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).

@cehsu cehsu requested a review from a team May 2, 2019 19:25
@cehsu cehsu closed this May 2, 2019
@cehsu cehsu removed the request for review from a team May 2, 2019 19:26
@cehsu cehsu changed the title Remove timeouts, fix ref bug fix(Dropdown): Remove timeouts, Fix ref bug May 2, 2019
@cehsu
Copy link
Contributor Author

cehsu commented May 2, 2019

Fixing some tests for escape/close and also adding one for enter, and then will open back up.

@cehsu cehsu reopened this May 6, 2019
if (autoclose) {
// Use timeout to delay examination of activeElement until after blur/focus
// events have been processed.
setTimeout(() => {
Copy link
Contributor Author

@cehsu cehsu May 6, 2019

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.

src/Dropdown/Dropdown.js Outdated Show resolved Hide resolved
@codecov
Copy link

codecov bot commented May 6, 2019

Codecov Report

Merging #42 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@           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
Impacted Files Coverage Δ
src/Dropdown/Dropdown.js 92.85% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9e7b7d3...4ca9453. Read the comment docs.

src/Dropdown/Dropdown.mdx Outdated Show resolved Hide resolved
@cehsu cehsu requested a review from a team May 6, 2019 19:02
@kylealwyn
Copy link
Contributor

@cehsu Could you elaborate on why enzyme was added? It'd be preferable to not have two testing frameworks in play simultaneously

@cehsu
Copy link
Contributor Author

cehsu commented May 7, 2019

@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.

@kylealwyn
Copy link
Contributor

kylealwyn commented May 7, 2019

@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();

@cehsu
Copy link
Contributor Author

cehsu commented May 7, 2019

@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.

@kylealwyn
Copy link
Contributor

@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

@cehsu
Copy link
Contributor Author

cehsu commented May 8, 2019

Thanks @kylealwyn. The original (now failing) test has been restored for reference.

@cehsu cehsu requested a review from choochootrain May 8, 2019 20:51
@cehsu cehsu requested review from erikshestopal and kylealwyn May 13, 2019 19:36
@cehsu
Copy link
Contributor Author

cehsu commented May 13, 2019

I'm restored the enzyme tests for now since they work, if that's alright. We can revisit the libraries used again.

@kylealwyn
Copy link
Contributor

@cehsu I'd prefer that we skip the failing test for now with manual inspection rather than adding an entirely new test framework

src/Dropdown/Dropdown.js Show resolved Hide resolved
src/Dropdown/Dropdown.js Outdated Show resolved Hide resolved
src/Dropdown/Dropdown.mdx Outdated Show resolved Hide resolved
src/Dropdown/Dropdown.js Outdated Show resolved Hide resolved
src/Dropdown/Dropdown.mdx Outdated Show resolved Hide resolved
@cehsu cehsu requested review from kylealwyn, erikshestopal and a team May 15, 2019 20:27
@kylealwyn
Copy link
Contributor

@cehsu Could we tackle the tests or are they still cryptically failing?

});
}
}, [isOpen]);

useKeyPress('Escape', () => {
if (isOpen) {
triggerRef.current.focus();
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 manually implement returnFocus here, so that inputs can be directly focused (without a second click), which was prevented by using FocusLock's returnFocus.

Copy link
Contributor

@kylealwyn kylealwyn left a 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.

@cehsu
Copy link
Contributor Author

cehsu commented May 15, 2019

@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:

+  const stopPropagation = jest.mock();
+    
+  const triggerEvents = {
+    'click': { type: 'click', stopPropagation },
+    'enter': { type: 'keyPress', which: 13, stopPropagation },
+    'space': { type: 'keyPress', which: 32, stopPropagation },
+  };
+
 ...
-  const openDropdown = async () => {
+  const openDropdown = async ( event = triggerEvents.click ) => {
     const trigger = renderUtils.getByText('Trigger');
-    fireEvent.click(trigger, { stopPropagation: () => null });
+    fireEvent[event.type](trigger, { ...event, type: event.type.toLowerCase() });
     return assertDropdownOpen();
   };

@cehsu cehsu merged commit 85590cf into master May 15, 2019
@cehsu cehsu deleted the fix/dropdown-blur branch May 15, 2019 23:48
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants