Skip to content

Fixing and adding tests for ComboBox onLoadMore called correctly - #2466

Merged
ktabors merged 4 commits into
mainfrom
combobox_onloadmore
Oct 26, 2021
Merged

Fixing and adding tests for ComboBox onLoadMore called correctly#2466
ktabors merged 4 commits into
mainfrom
combobox_onloadmore

Conversation

@ktabors

@ktabors ktabors commented Oct 18, 2021

Copy link
Copy Markdown
Collaborator

Closes #2365

✅ Pull Request Checklist:

  • Included link to corresponding React Spectrum GitHub Issue.
  • Added/updated unit tests and storybook for this change (for new code or code which already has tests).
  • Filled out test instructions.
  • Updated documentation (if it already exists for this component).
  • Looked at the Accessibility Practices for this feature - Aria Practices

📝 Test Instructions:

Tests pass
Added onLoadMore action call logging to server side filtering with useAsyncList story.

🧢 Your Project:

RSP


expect(onOpenChange).toHaveBeenCalledTimes(1);
expect(onOpenChange).toHaveBeenCalledWith(true, 'manual');
expect(onLoadMore).toHaveBeenCalledTimes(6);

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@LFDanLu Do you know why this is being called 6 times on initial load in tests? There are three items in the menu. It works in storybook correctly.

I did confirm that without the fix it is called more times in the following test which is the reopen test.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Pretty odd, I know there are a couple of places loadMore can get called in Virtualizer (onVisibleRectChange, the useLayoutEffect, etc) but can't say for sure without digging into the ScrollView logic.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for the info! It sounds like this is okay to proceed forward with.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

you could always see where it's being called from by getting stack traces

let onLoadMore = jest.fn(() => {
  try {
    console.trace();
  } catch (e) {}
});

Which show's @LFDanLu's hunch is correct, those are the two places it's being called from, and in ScrollView it's line 144.

what's interesting is that updateSize is called numerous times. if you place a log on line 143 just before the if console.log(stateRef.current.width, w, stateRef.current.height, h);

0 1000 0 1000
1000 1000 1000 1000
1000 1000 1000 1000
1000 1000 1000 1000
1000 1000 1000 1000
1000 1000 1000 104
1000 1000 104 8
1000 1000 8 8

so it's iterating on the height changing, I'm not familiar enough to know where these sizes are coming from, however, if you change line 255 of the ComboBox test to return 8 for the clientHeight, you can pull down the number of onLoadMore calls to 2

@LFDanLu you're a bit more familiar, do we need to adjust the test for this?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

IMO it would be worthwhile digging into it a little bit. If it becomes too much of a deep dive into VirtualizerState/ScrollView stuff, we can split it out into a separate issue

@ktabors
ktabors marked this pull request as ready for review October 19, 2021 16:59
@adobe-bot

Copy link
Copy Markdown

Build successful! 🎉

@dannify

dannify commented Oct 21, 2021

Copy link
Copy Markdown
Member

Did this get introduced with this change? https://github.com/adobe/react-spectrum/pull/1698/files

@ktabors

ktabors commented Oct 22, 2021

Copy link
Copy Markdown
Collaborator Author

Did this get introduced with this change? https://github.com/adobe/react-spectrum/pull/1698/files

It looks like it did. @LFDanLu?

@LFDanLu

LFDanLu commented Oct 22, 2021

Copy link
Copy Markdown
Member

Yep that is correct.

@adobe-bot

Copy link
Copy Markdown

Build successful! 🎉

@adobe-bot

Copy link
Copy Markdown

Build successful! 🎉

@LFDanLu LFDanLu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Think this should be fine

useLayoutEffect(() => {
if (!isLoading && onLoadMore && !state.isAnimating) {
if (state.contentSize.height <= state.virtualizer.visibleRect.height) {
if (state.contentSize.height > 0 && state.contentSize.height <= state.virtualizer.visibleRect.height) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I was initially apprehensive about getting into a state where state.contentSize.height is 0 even after the first load but I think this should be ok for now. I don't imagine there would be a case where the Virtualizer doesn't have a height at all after the initial load (placeholders would exist or minimum top/bottom padding for the scroll view)

@LFDanLu

LFDanLu commented Oct 25, 2021

Copy link
Copy Markdown
Member

@ktabors AH, one more thing perhaps. I was doing some extra testing and we'll still call load more upon opening the ComboBox if the number of items doesn't exceed 2x the scroll view height. We could perhaps change

let onVisibleRectChange = useCallback((rect: Rect) => {
so that it accepts a isScrollEvent param as well and have
onVisibleRectChange(new Rect(state.scrollLeft, state.scrollTop, state.width, state.height));
call onVisibleRectChange with isScrollEvent = true and
onVisibleRectChange(new Rect(state.scrollLeft, state.scrollTop, w, h));
with isScrollEvent = false.

Open to other opinions since this will mean we will only cause extra load mores to happen if the user actually scrolls the view whereas we currently will keep trying to loadMore on each open of the combobox until we are 2x the scroll view height.

@snowystinger snowystinger left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

@ktabors
ktabors merged commit 74e31fa into main Oct 26, 2021
@ktabors
ktabors deleted the combobox_onloadmore branch October 26, 2021 16:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ComboBox onLoadMore event fires on open

5 participants