Add test case for Cascader

This commit is contained in:
afc163 2018-05-25 19:31:34 +08:00 committed by 偏右
parent 322ebf0039
commit 3db0610d52
3 changed files with 732 additions and 3 deletions

View File

@ -215,6 +215,632 @@ exports[`Cascader popup correctly with defaultValue 1`] = `
</div>
`;
exports[`Cascader should highlight keyword and filter when search in Cascader 1`] = `
<Popup
action={
Array [
"click",
]
}
align={
Object {
"offset": Array [
0,
4,
],
"overflow": Object {
"adjustX": 1,
"adjustY": 1,
},
"points": Array [
"tl",
"bl",
],
}
}
className=""
destroyPopupOnHide={false}
getClassNameFromAlign={[Function]}
getRootDomNode={[Function]}
mask={false}
onAlign={[Function]}
prefixCls="ant-cascader-menus"
style={Object {}}
transitionName="slide-up"
visible={true}
>
<div>
<Animate
animation={Object {}}
component=""
componentProps={Object {}}
exclusive={true}
onAppear={[Function]}
onEnd={[Function]}
onEnter={[Function]}
onLeave={[Function]}
showProp="xVisible"
transitionAppear={true}
transitionEnter={true}
transitionLeave={true}
transitionName="slide-up"
>
<AnimateChild
animation={Object {}}
key="popup"
transitionAppear={true}
transitionEnter={true}
transitionLeave={true}
transitionName="slide-up"
>
<Align
align={
Object {
"offset": Array [
0,
4,
],
"overflow": Object {
"adjustX": 1,
"adjustY": 1,
},
"points": Array [
"tl",
"bl",
],
}
}
childrenProps={
Object {
"visible": "xVisible",
}
}
disabled={false}
key="popup"
monitorBufferTime={50}
monitorWindowResize={true}
onAlign={[Function]}
target={[Function]}
xVisible={true}
>
<PopupInner
className="ant-cascader-menus ant-cascader-menus-placement-bottomLeft "
hiddenClassName="ant-cascader-menus-hidden"
prefixCls="ant-cascader-menus"
style={Object {}}
visible={true}
>
<div
className="ant-cascader-menus ant-cascader-menus-placement-bottomLeft "
style={Object {}}
>
<LazyRenderBox
className="ant-cascader-menus-content"
visible={true}
>
<Menus
activeValue={Array []}
allowClear={true}
builtinPlacements={
Object {
"bottomLeft": Object {
"offset": Array [
0,
4,
],
"overflow": Object {
"adjustX": 1,
"adjustY": 1,
},
"points": Array [
"tl",
"bl",
],
},
"bottomRight": Object {
"offset": Array [
0,
4,
],
"overflow": Object {
"adjustX": 1,
"adjustY": 1,
},
"points": Array [
"tr",
"br",
],
},
"topLeft": Object {
"offset": Array [
0,
-4,
],
"overflow": Object {
"adjustX": 1,
"adjustY": 1,
},
"points": Array [
"bl",
"tl",
],
},
"topRight": Object {
"offset": Array [
0,
-4,
],
"overflow": Object {
"adjustX": 1,
"adjustY": 1,
},
"points": Array [
"br",
"tr",
],
},
}
}
defaultFiledNames={
Object {
"children": "children",
"label": "label",
"value": "value",
}
}
disabled={false}
dropdownMenuColumnStyle={
Object {
"width": 0,
}
}
expandTrigger="click"
filedNames={
Object {
"children": "children",
"label": "label",
"value": "value",
}
}
inputPrefixCls="ant-input"
notFoundContent="Not Found"
onChange={[Function]}
onPopupVisibleChange={[Function]}
onSelect={[Function]}
options={
Array [
Object {
"__IS_FILTERED_OPTION": true,
"disabled": false,
"label": Array [
"Jiangsu",
Array [
" / ",
"Nanjing",
],
Array [
" / ",
"Zhong Hua Men",
],
],
"path": Array [
Object {
"children": Array [
Object {
"children": Array [
Object {
"label": "Zhong Hua Men",
"value": "zhonghuamen",
},
],
"label": "Nanjing",
"value": "nanjing",
},
],
"label": "Jiangsu",
"value": "jiangsu",
},
Object {
"children": Array [
Object {
"label": "Zhong Hua Men",
"value": "zhonghuamen",
},
],
"label": "Nanjing",
"value": "nanjing",
},
Object {
"label": "Zhong Hua Men",
"value": "zhonghuamen",
},
],
"value": Array [
"jiangsu",
"nanjing",
"zhonghuamen",
],
},
Object {
"__IS_FILTERED_OPTION": true,
"disabled": false,
"label": Array [
"Zhejiang",
Array [
" / ",
Array [
"Hang",
Array [
<span
className="ant-cascader-menu-item-keyword"
>
z
</span>,
"hou",
],
],
],
Array [
" / ",
"West Lake",
],
],
"path": Array [
Object {
"children": Array [
Object {
"children": Array [
Object {
"label": "West Lake",
"value": "xihu",
},
],
"label": "Hangzhou",
"value": "hangzhou",
},
],
"label": "Zhejiang",
"value": "zhejiang",
},
Object {
"children": Array [
Object {
"label": "West Lake",
"value": "xihu",
},
],
"label": "Hangzhou",
"value": "hangzhou",
},
Object {
"label": "West Lake",
"value": "xihu",
},
],
"value": Array [
"zhejiang",
"hangzhou",
"xihu",
],
},
]
}
placeholder="Please select"
popupClassName=""
popupPlacement="bottomLeft"
popupVisible={true}
prefixCls="ant-cascader"
showSearch={
Object {
"filter": [Function],
}
}
transitionName="slide-up"
value={Array []}
visible={true}
>
<div>
<ul
className="ant-cascader-menu"
key="0"
style={
Object {
"width": 0,
}
}
>
<li
className="ant-cascader-menu-item"
key="jiangsu,nanjing,zhonghuamen"
onClick={[Function]}
title=""
>
Jiangsu
/
Nanjing
/
Zhong Hua Men
</li>
<li
className="ant-cascader-menu-item"
key="zhejiang,hangzhou,xihu"
onClick={[Function]}
title=""
>
Zhejiang
/
Hang
<span
className="ant-cascader-menu-item-keyword"
key="seperator"
>
z
</span>
hou
/
West Lake
</li>
</ul>
</div>
</Menus>
</LazyRenderBox>
</div>
</PopupInner>
</Align>
</AnimateChild>
</Animate>
</div>
</Popup>
`;
exports[`Cascader should render not found content 1`] = `
<Popup
action={
Array [
"click",
]
}
align={
Object {
"offset": Array [
0,
4,
],
"overflow": Object {
"adjustX": 1,
"adjustY": 1,
},
"points": Array [
"tl",
"bl",
],
}
}
className=""
destroyPopupOnHide={false}
getClassNameFromAlign={[Function]}
getRootDomNode={[Function]}
mask={false}
onAlign={[Function]}
prefixCls="ant-cascader-menus"
style={Object {}}
transitionName="slide-up"
visible={true}
>
<div>
<Animate
animation={Object {}}
component=""
componentProps={Object {}}
exclusive={true}
onAppear={[Function]}
onEnd={[Function]}
onEnter={[Function]}
onLeave={[Function]}
showProp="xVisible"
transitionAppear={true}
transitionEnter={true}
transitionLeave={true}
transitionName="slide-up"
>
<AnimateChild
animation={Object {}}
key="popup"
transitionAppear={true}
transitionEnter={true}
transitionLeave={true}
transitionName="slide-up"
>
<Align
align={
Object {
"offset": Array [
0,
4,
],
"overflow": Object {
"adjustX": 1,
"adjustY": 1,
},
"points": Array [
"tl",
"bl",
],
}
}
childrenProps={
Object {
"visible": "xVisible",
}
}
disabled={false}
key="popup"
monitorBufferTime={50}
monitorWindowResize={true}
onAlign={[Function]}
target={[Function]}
xVisible={true}
>
<PopupInner
className="ant-cascader-menus ant-cascader-menus-placement-bottomLeft "
hiddenClassName="ant-cascader-menus-hidden"
prefixCls="ant-cascader-menus"
style={Object {}}
visible={true}
>
<div
className="ant-cascader-menus ant-cascader-menus-placement-bottomLeft "
style={Object {}}
>
<LazyRenderBox
className="ant-cascader-menus-content"
visible={true}
>
<Menus
activeValue={Array []}
allowClear={true}
builtinPlacements={
Object {
"bottomLeft": Object {
"offset": Array [
0,
4,
],
"overflow": Object {
"adjustX": 1,
"adjustY": 1,
},
"points": Array [
"tl",
"bl",
],
},
"bottomRight": Object {
"offset": Array [
0,
4,
],
"overflow": Object {
"adjustX": 1,
"adjustY": 1,
},
"points": Array [
"tr",
"br",
],
},
"topLeft": Object {
"offset": Array [
0,
-4,
],
"overflow": Object {
"adjustX": 1,
"adjustY": 1,
},
"points": Array [
"bl",
"tl",
],
},
"topRight": Object {
"offset": Array [
0,
-4,
],
"overflow": Object {
"adjustX": 1,
"adjustY": 1,
},
"points": Array [
"br",
"tr",
],
},
}
}
defaultFiledNames={
Object {
"children": "children",
"label": "label",
"value": "value",
}
}
disabled={false}
dropdownMenuColumnStyle={
Object {
"height": "auto",
"width": 0,
}
}
expandTrigger="click"
filedNames={
Object {
"children": "children",
"label": "label",
"value": "value",
}
}
inputPrefixCls="ant-input"
notFoundContent="Not Found"
onChange={[Function]}
onPopupVisibleChange={[Function]}
onSelect={[Function]}
options={
Array [
Object {
"disabled": true,
"label": "Not Found",
"value": "ANT_CASCADER_NOT_FOUND",
},
]
}
placeholder="Please select"
popupClassName=""
popupPlacement="bottomLeft"
popupVisible={true}
prefixCls="ant-cascader"
showSearch={
Object {
"filter": [Function],
}
}
transitionName="slide-up"
value={Array []}
visible={true}
>
<div>
<ul
className="ant-cascader-menu"
key="0"
style={
Object {
"height": "auto",
"width": 0,
}
}
>
<li
className="ant-cascader-menu-item ant-cascader-menu-item-disabled"
key="ANT_CASCADER_NOT_FOUND"
onClick={[Function]}
title="Not Found"
>
Not Found
</li>
</ul>
</div>
</Menus>
</LazyRenderBox>
</div>
</PopupInner>
</Align>
</AnimateChild>
</Animate>
</div>
</Popup>
`;
exports[`Cascader support controlled mode 1`] = `
<span
class="ant-cascader-picker"

View File

@ -28,6 +28,10 @@ const options = [{
}],
}];
function filter(inputValue, path) {
return path.some(option => (option.label).toLowerCase().indexOf(inputValue.toLowerCase()) > -1);
}
describe('Cascader', () => {
focusTest(Cascader);
@ -39,11 +43,13 @@ describe('Cascader', () => {
});
it('popup correctly when panel is open', () => {
const onPopupVisibleChange = jest.fn();
const wrapper = mount(
<Cascader options={options} />
<Cascader options={options} onPopupVisibleChange={onPopupVisibleChange} />
);
wrapper.find('input').simulate('click');
expect(render(wrapper.find('Trigger').instance().getComponent())).toMatchSnapshot();
expect(onPopupVisibleChange).toHaveBeenCalledWith(true);
});
it('support controlled mode', () => {
@ -64,8 +70,18 @@ describe('Cascader', () => {
expect(render(wrapper.find('Trigger').instance().getComponent())).toMatchSnapshot();
});
it('should support popupVisible', () => {
const wrapper = mount(
<Cascader options={options} defaultValue={['zhejiang', 'hangzhou']} />
);
expect(wrapper.find('Trigger').instance().getComponent().props.visible).toBe(false);
wrapper.setProps({ popupVisible: true });
expect(wrapper.find('Trigger').instance().getComponent().props.visible).toBe(true);
});
it('can be selected', () => {
const wrapper = mount(<Cascader options={options} />);
const onChange = jest.fn();
const wrapper = mount(<Cascader options={options} onChange={onChange} />);
wrapper.find('input').simulate('click');
let popupWrapper = mount(wrapper.find('Trigger').instance().getComponent());
popupWrapper.find('.ant-cascader-menu').at(0).find('.ant-cascader-menu-item').at(0)
@ -79,6 +95,7 @@ describe('Cascader', () => {
popupWrapper.find('.ant-cascader-menu').at(2).find('.ant-cascader-menu-item').at(0)
.simulate('click');
expect(render(wrapper.find('Trigger').instance().getComponent())).toMatchSnapshot();
expect(onChange).toHaveBeenCalledWith(['zhejiang', 'hangzhou', 'xihu'], expect.anything());
});
it('backspace should work with `Cascader[showSearch]`', () => {
@ -89,4 +106,88 @@ describe('Cascader', () => {
// Simulate onKeyDown will not trigger onChange by default, so the value is still '123'
expect(wrapper.state('inputValue')).toBe('123');
});
it('should highlight keyword and filter when search in Cascader', () => {
const wrapper = mount(<Cascader options={options} showSearch={{ filter }} />);
wrapper.find('input').simulate('click');
wrapper.find('input').simulate('change', { target: { value: 'z' } });
expect(wrapper.state('inputValue')).toBe('z');
const popupWrapper = mount(wrapper.find('Trigger').instance().getComponent());
expect(popupWrapper).toMatchSnapshot();
});
it('should render not found content', () => {
const wrapper = mount(<Cascader options={options} showSearch={{ filter }} />);
wrapper.find('input').simulate('click');
wrapper.find('input').simulate('change', { target: { value: '__notfoundkeyword__' } });
expect(wrapper.state('inputValue')).toBe('__notfoundkeyword__');
const popupWrapper = mount(wrapper.find('Trigger').instance().getComponent());
expect(popupWrapper).toMatchSnapshot();
});
it('should support to clear selection', () => {
const wrapper = mount(<Cascader options={options} defaultValue={['zhejiang', 'hangzhou']} />);
expect(wrapper.find('.ant-cascader-picker-label').text()).toBe('Zhejiang / Hangzhou');
wrapper.find('.ant-cascader-picker-clear').at(0).simulate('click');
expect(wrapper.find('.ant-cascader-picker-label').text()).toBe('');
});
it('should close popup when clear selection', () => {
const onPopupVisibleChange = jest.fn();
const wrapper = mount(
<Cascader
options={options}
popupVisible
defaultValue={['zhejiang', 'hangzhou']}
onPopupVisibleChange={onPopupVisibleChange}
/>
);
wrapper.find('.ant-cascader-picker-clear').at(0).simulate('click');
expect(onPopupVisibleChange).toHaveBeenCalledWith(false);
});
it('should clear search input when clear selection', () => {
const wrapper = mount(
<Cascader
options={options}
defaultValue={['zhejiang', 'hangzhou']}
showSearch
/>
);
wrapper.find('input').simulate('click');
wrapper.find('input').simulate('change', { target: { value: 'xxx' } });
expect(wrapper.state('inputValue')).toBe('xxx');
wrapper.find('.ant-cascader-picker-clear').at(0).simulate('click');
expect(wrapper.state('inputValue')).toBe('');
});
it('should not trigger visible change when click search input', () => {
const onPopupVisibleChange = jest.fn();
const wrapper = mount(
<Cascader
options={options}
showSearch
onPopupVisibleChange={onPopupVisibleChange}
/>
);
wrapper.find('input').simulate('focus');
expect(onPopupVisibleChange).toHaveBeenCalledTimes(0);
wrapper.find('input').simulate('click');
expect(onPopupVisibleChange).toHaveBeenCalledTimes(1);
wrapper.find('input').simulate('click');
expect(onPopupVisibleChange).toHaveBeenCalledTimes(1);
wrapper.find('input').simulate('blur');
wrapper.setState({ popupVisible: false });
wrapper.find('input').simulate('click');
expect(onPopupVisibleChange).toHaveBeenCalledTimes(2);
});
it('should change filtered item when options are changed', () => {
const wrapper = mount(<Cascader options={options} showSearch={{ filter }} />);
wrapper.find('input').simulate('click');
wrapper.find('input').simulate('change', { target: { value: 'a' } });
expect(wrapper.find('.ant-cascader-menu-item').length).toBe(2);
wrapper.setProps({ options: [options[0]] });
expect(wrapper.find('.ant-cascader-menu-item').length).toBe(1);
});
});

View File

@ -219,7 +219,9 @@ export default class Cascader extends React.Component<CascaderProps, CascaderSta
// Prevent `Trigger` behaviour.
if (inputFocused || popupVisible) {
e.stopPropagation();
e.nativeEvent.stopImmediatePropagation();
if (e.nativeEvent.stopImmediatePropagation) {
e.nativeEvent.stopImmediatePropagation();
}
}
}