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

fix: fix selectPicker when search is controlled and exit Dropdown wit… #2411

Merged
merged 7 commits into from Apr 7, 2022

Conversation

myNameIsDu
Copy link
Member

fix selectPicker when search is controlled and exit Dropdown without resetting external search

@vercel
Copy link

vercel bot commented Mar 22, 2022

@myNameIsDu is attempting to deploy a commit to the rsuite Team on Vercel.

A member of the Team first needs to authorize it.

@vercel
Copy link

vercel bot commented Mar 22, 2022

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployments, click below or on the icon next to each commit.

rsuite-nextjs – ./docs

🔍 Inspect: https://vercel.com/rsuite/rsuite-nextjs/B3pvCDUNtue8MQm1XW5UQnihDdrK
✅ Preview: https://rsuite-nextjs-git-fork-mynameisdu-fix-v5-selectpicker-rsuite.vercel.app

rsuite-v4 – ./docs

🔍 Inspect: https://vercel.com/rsuite/rsuite-v4/CPUtsZ5qGpDXvcMSbcoSRFJgKcF7
✅ Preview: Canceled

[Deployment for b3793f9 canceled]

@codesandbox-ci
Copy link

codesandbox-ci bot commented Mar 22, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit d7653ee:

Sandbox Source
rsuite-tp-ci Configuration

@codecov
Copy link

codecov bot commented Mar 22, 2022

Codecov Report

Merging #2411 (d7653ee) into main (ab4c721) will increase coverage by 0.04%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main    #2411      +/-   ##
==========================================
+ Coverage   85.54%   85.59%   +0.04%     
==========================================
  Files         274      275       +1     
  Lines        8648     8655       +7     
  Branches     2428     2433       +5     
==========================================
+ Hits         7398     7408      +10     
+ Misses        660      658       -2     
+ Partials      590      589       -1     
Flag Coverage Δ
ChromeCi 85.59% <100.00%> (+0.04%) ⬆️
Firefox 85.58% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/SelectPicker/SelectPicker.tsx 90.09% <100.00%> (+0.09%) ⬆️
src/Overlay/OverlayTrigger.tsx 62.69% <0.00%> (-0.80%) ⬇️
src/Dropdown/DropdownMenu.tsx 92.68% <0.00%> (-0.50%) ⬇️
src/InputPicker/InputPicker.tsx 86.51% <0.00%> (ø)
src/Form/useFormClassNames.ts 100.00% <0.00%> (ø)
src/Picker/DropdownMenuCheckItem.tsx 95.23% <0.00%> (+0.23%) ⬆️
src/Toggle/Toggle.tsx 92.59% <0.00%> (+0.28%) ⬆️
src/Checkbox/Checkbox.tsx 92.68% <0.00%> (+0.57%) ⬆️
src/Form/Form.tsx 87.27% <0.00%> (+0.66%) ⬆️
src/Menu/MenuItem.tsx 81.08% <0.00%> (+3.30%) ⬆️
... and 1 more

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 ab4c721...d7653ee. Read the comment docs.

@vercel vercel bot temporarily deployed to Preview – rsuite-v4 March 23, 2022 01:26 Inactive
@vercel vercel bot temporarily deployed to Preview – rsuite-v4 March 24, 2022 07:00 Inactive
@@ -288,6 +288,7 @@ const SelectPicker = React.forwardRef(
const handleExited = useCallback(() => {
setSearchKeyword('');
setActive(false);
onSearch?.('');
onClose?.();
}, [onClose, setSearchKeyword]);
Copy link
Member

Choose a reason for hiding this comment

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

React Hook useCallback has a missing dependency: 'onSearch'. Either include it or remove the dependency array

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah,I added it.

Comment on lines 27 to 30
afterEach(() => {
cleanup();
});

Copy link
Member

Choose a reason for hiding this comment

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

cleanup() is unnecessary here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah,I removed it

Comment on lines 407 to 455
it('SearchWord should be reset when controlled and triggered off', done => {
let searchRef = '';
let onClose = null;
const promise = new Promise(resolve => {
onClose = resolve;
});
const Wrapper = () => {
const [search, setSearch] = useState(searchRef);
const containerRef = useRef();
searchRef = search;
const handleSearch = value => {
setSearch(value);
};
const handleClose = () => {
onClose();
};
const container = () => containerRef.current;
return (
<div ref={containerRef}>
<button id="exit">exit</button>
<SelectPicker
container={container}
search={search}
defaultOpen
onClose={handleClose}
onSearch={handleSearch}
data={data}
/>
</div>
);
};
Wrapper.displayName = 'WrapperSelectPicker';
const { container } = render(<Wrapper />);

const exit = container?.querySelector('#exit');
const input = container.querySelector('.rs-picker-search-bar-input');

// change search
fireEvent.change(input, { target: { value: 'a' } });
assert.equal(searchRef, 'a');

// close select
fireEvent.mouseDown(exit, { bubbles: true });

promise.then(() => {
assert.equal(searchRef, '');
done();
});
});
Copy link
Member

Choose a reason for hiding this comment

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

Please refer to existing test scripts and see how to use sinon.spy() to validate a callback has been called with correct arguments.

Copy link
Member Author

Choose a reason for hiding this comment

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

I modified it, but I think this test code does not reflect its operation scenario, How do you think

Copy link
Member Author

Choose a reason for hiding this comment

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

I mean the way this is written should not be suitable for such a description, should is "shoule call onSearch when closed", or other

Copy link
Member

Choose a reason for hiding this comment

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

I mean the way this is written should not be suitable for such a description, should is "shoule call onSearch when closed", or other

The description was not quite suitable in the first place. "Should call onSearch with '' when closed" is exactly what should be tested here. The goal of unit tests is to make sure the APIs we provide (onSearch) work as expected (called with '' when closed), rather than to cover how users are using our APIs (e.g. updating a state when onSearch is called).

Comment on lines 419 to 421
await waitForElementToBeRemoved(document.querySelector('.rs-picker-search-bar-input'));

assert.isTrue(handleSearch.calledOnce);
Copy link
Member

Choose a reason for hiding this comment

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

Avoid mentioning implementation detail in test cases. Simply use waitFor from @testing-library/react for async assertions.


Arguments of onSearch call should also be asserted.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah

Comment on lines 405 to 414
const Wrapper = () => {
return (
<>
<button id="exit">exit</button>
<SelectPicker container={container} defaultOpen onSearch={handleSearch} data={data} />
</>
);
};
Wrapper.displayName = 'WrapperSelectPicker';
let { container } = render(<Wrapper />);
Copy link
Member

Choose a reason for hiding this comment

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

Unnecessary wrapping. Simply render the fragment.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah

};
Wrapper.displayName = 'WrapperSelectPicker';
let { container } = render(<Wrapper />);
const exit = container.querySelector('#exit');
Copy link
Member

Choose a reason for hiding this comment

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

Use getElementById when possible.

Copy link
Member Author

Choose a reason for hiding this comment

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

The container doesn't have the getElementById method

Copy link
Member

Choose a reason for hiding this comment

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

document should be available in test scripts.

Comment on lines 416 to 418
await waitFor(() => assert.isTrue(handleClose.calledOnce));

assert.isTrue(handleSearch.calledOnce);
Copy link
Member

Choose a reason for hiding this comment

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

Duplicated assertions. You could wrap multiple assertions within waitFor.

Copy link
Member Author

Choose a reason for hiding this comment

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

Great writing style

@vercel vercel bot temporarily deployed to Preview – rsuite-v4 April 5, 2022 08:20 Inactive
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.

None yet

3 participants