ant-design/components/mention/__tests__/index.test.js

117 lines
3.6 KiB
JavaScript
Raw Normal View History

2018-05-22 16:39:17 +08:00
import React from 'react';
import { mount } from 'enzyme';
import Mention from '..';
const { toContentState } = Mention;
describe('Mention', () => {
beforeAll(() => {
jest.useFakeTimers();
});
afterAll(() => {
jest.useRealTimers();
});
it('should has focus function', () => {
const handleFocus = jest.fn();
const wrapper = mount(
<Mention
defaultValue={toContentState('@afc163')}
onFocus={handleFocus}
suggestions={['afc163', 'benjycui', 'yiminghe', 'RaoHai', '中文', 'にほんご']}
2018-12-07 20:02:01 +08:00
/>,
2018-05-22 16:39:17 +08:00
);
wrapper.instance().focus();
jest.runAllTimers();
expect(handleFocus).toBeCalled();
});
it('basic suggestion', () => {
const handleSearch = jest.fn();
const wrapper = mount(
2018-12-07 20:02:01 +08:00
<Mention suggestions={['afc163', 'raohai']} onSearchChange={handleSearch} />,
2018-05-22 16:39:17 +08:00
);
wrapper.find('DraftEditorContents').simulate('focus');
const ed = wrapper.find('.public-DraftEditor-content');
ed.simulate('beforeInput', { data: '@a' });
jest.runAllTimers();
expect(handleSearch).toBeCalledWith('a', '@');
});
it('change suggestions', () => {
2018-05-22 23:09:26 +08:00
const container = mount(<div id="container" />);
2018-05-22 16:39:17 +08:00
const wrapper = mount(
2018-12-07 20:02:01 +08:00
<Mention
suggestions={['afc163', 'raohai']}
getSuggestionContainer={() => container.getDOMNode()}
/>,
2018-05-22 16:39:17 +08:00
);
wrapper.find('DraftEditorContents').simulate('focus');
const ed = wrapper.find('.public-DraftEditor-content');
ed.simulate('beforeInput', { data: '@' });
2018-05-22 21:08:00 +08:00
jest.runAllTimers();
2018-05-22 23:09:26 +08:00
expect(container.getDOMNode().querySelectorAll('.ant-mention-dropdown-item').length).toBe(2);
2018-12-07 20:02:01 +08:00
expect(container.getDOMNode().querySelectorAll('.ant-mention-dropdown-item')[0].innerHTML).toBe(
'afc163',
);
2018-11-21 22:38:27 +08:00
wrapper.setProps({ suggestions: ['yesmeck', 'yiminghe', 'lucy'] });
2018-05-22 21:08:00 +08:00
jest.runAllTimers();
2018-05-22 23:09:26 +08:00
expect(container.getDOMNode().querySelectorAll('.ant-mention-dropdown-item').length).toBe(3);
2018-12-07 20:02:01 +08:00
expect(container.getDOMNode().querySelectorAll('.ant-mention-dropdown-item')[0].innerHTML).toBe(
'yesmeck',
);
2018-05-22 16:39:17 +08:00
});
2018-10-10 16:15:44 +08:00
it('select item', () => {
const onChange = jest.fn();
const onSelect = jest.fn();
const wrapper = mount(
2018-12-07 20:02:01 +08:00
<Mention suggestions={['afc163', 'raohai']} onChange={onChange} onSelect={onSelect} />,
2018-10-10 16:15:44 +08:00
);
wrapper.find('DraftEditorContents').simulate('focus');
const ed = wrapper.find('.public-DraftEditor-content');
ed.simulate('beforeInput', { data: '@' });
jest.runAllTimers();
expect(onChange).toBeCalled();
expect(onSelect).not.toBeCalled();
// enzyme cannot find .ant-mention-dropdown-item in react 15
// I don't know why
if (process.env.REACT === '15') {
return;
}
2018-12-07 20:02:01 +08:00
wrapper
.find('.ant-mention-dropdown-item')
.at(0)
.simulate('mouseDown');
wrapper
.find('.ant-mention-dropdown-item')
.at(0)
.simulate('mouseUp');
wrapper
.find('.ant-mention-dropdown-item')
.at(0)
.simulate('click');
2018-10-10 16:15:44 +08:00
jest.runAllTimers();
expect(onSelect).toBeCalled();
expect(wrapper.find('.public-DraftStyleDefault-block').text()).toBe('@afc163 ');
});
2018-12-18 19:50:20 +08:00
it('defaultSuggestion filter', () => {
if (process.env.REACT === '15') {
return;
}
const wrapper = mount(<Mention defaultSuggestions={['light', 'bamboo']} />);
wrapper.find('DraftEditorContents').simulate('focus');
const ed = wrapper.find('.public-DraftEditor-content');
ed.simulate('beforeInput', { data: '@b' });
jest.runAllTimers();
const items = wrapper.find('div.ant-mention-dropdown-item');
expect(items.length).toBe(1);
expect(items.at(0).props().children).toBe('bamboo');
});
2018-05-22 16:39:17 +08:00
});