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
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/SelectPicker/SelectPicker.tsx
Expand Up @@ -83,7 +83,7 @@ export interface SelectProps<T> {
onGroupTitleClick?: (event: React.SyntheticEvent) => void;

/** Called when searching */
onSearch?: (searchKeyword: string, event: React.SyntheticEvent) => void;
onSearch?: (searchKeyword: string, event?: React.SyntheticEvent) => void;

/** Called when clean */
onClean?: (event: React.SyntheticEvent) => void;
Expand Down Expand Up @@ -288,8 +288,9 @@ const SelectPicker = React.forwardRef(
const handleExited = useCallback(() => {
setSearchKeyword('');
setActive(false);
onSearch?.('');
onClose?.();
}, [onClose, setSearchKeyword]);
}, [onClose, setSearchKeyword, onSearch]);

const handleEntered = useCallback(() => {
setActive(true);
Expand Down
22 changes: 21 additions & 1 deletion src/SelectPicker/test/SelectPickerSpec.js
@@ -1,5 +1,5 @@
import React from 'react';
import { render } from '@testing-library/react';
import { render, fireEvent, waitForElementToBeRemoved } from '@testing-library/react';
import ReactTestUtils from 'react-dom/test-utils';
import { getDOMNode, getInstance } from '@test/testUtils';
import SelectPicker from '../SelectPicker';
Expand Down Expand Up @@ -400,4 +400,24 @@ describe('SelectPicker', () => {
expect(getByTestId('content')).to.have.text('Not selected');
});
});
it('SearchWord should be reset when controlled and triggered off', async () => {
const handleSearch = sinon.spy();
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

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.


// close select
fireEvent.mouseDown(exit, { bubbles: true });
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

});
});