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
Changes from 1 commit
ee681c6
b686373
755930e
61cd20f
99a2b58
b3793f9
d7653ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import React, { useRef, useState } from 'react'; | ||
import { render, cleanup, fireEvent } from '@testing-library/react'; | ||
import ReactTestUtils from 'react-dom/test-utils'; | ||
import { getDOMNode, getInstance } from '@test/testUtils'; | ||
import SelectPicker from '../SelectPicker'; | ||
|
@@ -24,6 +24,10 @@ const data = [ | |
} | ||
]; | ||
|
||
afterEach(() => { | ||
cleanup(); | ||
}); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah,I removed it |
||
describe('SelectPicker', () => { | ||
it('Should clean selected default value', () => { | ||
const instance = getDOMNode(<SelectPicker defaultOpen data={data} defaultValue={'Eugenia'} />); | ||
|
@@ -400,4 +404,53 @@ describe('SelectPicker', () => { | |
expect(getByTestId('content')).to.have.text('Not selected'); | ||
}); | ||
}); | ||
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(); | ||
}); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please refer to existing test scripts and see how to use There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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 ( |
||
}); |
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.
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.
Yeah,I added it.