2020-03-07 11:45:13 +00:00
|
|
|
import { mount } from '@vue/test-utils';
|
|
|
|
import Mentions from '..';
|
|
|
|
import focusTest from '../../../tests/shared/focusTest';
|
2020-08-01 08:37:06 +00:00
|
|
|
import { sleep } from '../../../tests/utils';
|
2020-08-10 06:16:24 +00:00
|
|
|
import KeyCode from '../../_util/KeyCode';
|
2020-03-07 11:45:13 +00:00
|
|
|
|
2020-08-10 06:16:24 +00:00
|
|
|
const { getMentions, Option } = Mentions;
|
2020-03-07 11:45:13 +00:00
|
|
|
|
|
|
|
function $$(className) {
|
|
|
|
return document.body.querySelectorAll(className);
|
|
|
|
}
|
|
|
|
|
|
|
|
function triggerInput(wrapper, text = '') {
|
2021-09-25 08:51:32 +00:00
|
|
|
const lastChar = text[text.length - 1];
|
2020-03-07 11:45:13 +00:00
|
|
|
wrapper.find('textarea').element.value = text;
|
|
|
|
wrapper.find('textarea').element.selectionStart = text.length;
|
|
|
|
wrapper.find('textarea').trigger('keydown');
|
|
|
|
wrapper.find('textarea').trigger('change');
|
2021-09-25 08:51:32 +00:00
|
|
|
wrapper.find('textarea').trigger('keyup', { key: lastChar });
|
2020-03-07 11:45:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
describe('Mentions', () => {
|
2020-08-01 08:37:06 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
document.body.innerHTML = '';
|
2020-03-07 11:45:13 +00:00
|
|
|
});
|
2021-09-25 08:51:32 +00:00
|
|
|
focusTest(Mentions);
|
2020-03-07 11:45:13 +00:00
|
|
|
it('getMentions', () => {
|
|
|
|
const mentions = getMentions('@light #bamboo cat', { prefix: ['@', '#'] });
|
|
|
|
expect(mentions).toEqual([
|
|
|
|
{
|
|
|
|
prefix: '@',
|
|
|
|
value: 'light',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
prefix: '#',
|
|
|
|
value: 'bamboo',
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2020-08-14 09:04:45 +00:00
|
|
|
it('focus', async () => {
|
2020-03-07 11:45:13 +00:00
|
|
|
const onFocus = jest.fn();
|
|
|
|
const onBlur = jest.fn();
|
|
|
|
|
2020-08-01 08:37:06 +00:00
|
|
|
const wrapper = mount(
|
|
|
|
{
|
|
|
|
render() {
|
|
|
|
return <Mentions onFocus={onFocus} onBlur={onBlur} />;
|
|
|
|
},
|
2020-03-07 11:45:13 +00:00
|
|
|
},
|
2020-08-01 08:37:06 +00:00
|
|
|
{ sync: false, attachTo: 'body' },
|
|
|
|
);
|
|
|
|
await sleep();
|
2020-03-07 11:45:13 +00:00
|
|
|
wrapper.find('textarea').trigger('focus');
|
2020-08-01 08:37:06 +00:00
|
|
|
await sleep();
|
2020-03-07 11:45:13 +00:00
|
|
|
expect(wrapper.find('.ant-mentions').classes('ant-mentions-focused')).toBeTruthy();
|
|
|
|
expect(onFocus).toHaveBeenCalled();
|
|
|
|
wrapper.find('textarea').trigger('blur');
|
2020-08-01 08:37:06 +00:00
|
|
|
await sleep(500);
|
2020-03-07 11:45:13 +00:00
|
|
|
expect(wrapper.classes()).not.toContain('ant-mentions-focused');
|
|
|
|
expect(onBlur).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2020-08-01 08:37:06 +00:00
|
|
|
it('loading', async () => {
|
2020-03-07 11:45:13 +00:00
|
|
|
const wrapper = mount(
|
|
|
|
{
|
|
|
|
render() {
|
|
|
|
return <Mentions loading />;
|
|
|
|
},
|
|
|
|
},
|
2020-08-01 08:37:06 +00:00
|
|
|
{ sync: false, attachTo: 'body' },
|
2020-03-07 11:45:13 +00:00
|
|
|
);
|
2021-09-25 08:51:32 +00:00
|
|
|
await sleep(100);
|
2020-03-07 11:45:13 +00:00
|
|
|
triggerInput(wrapper, '@');
|
2021-09-25 08:51:32 +00:00
|
|
|
await sleep(100);
|
2020-08-01 08:37:06 +00:00
|
|
|
expect($$('.ant-mentions-dropdown-menu-item').length).toBeTruthy();
|
|
|
|
expect($$('.ant-spin')).toBeTruthy();
|
2020-03-07 11:45:13 +00:00
|
|
|
});
|
|
|
|
|
2020-08-10 06:16:24 +00:00
|
|
|
it('notExist', async () => {
|
2020-10-25 15:06:03 +00:00
|
|
|
jest.useFakeTimers();
|
2020-08-10 06:16:24 +00:00
|
|
|
const wrapper = mount({
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<Mentions>
|
|
|
|
<Option value="bamboo">Bamboo</Option>
|
|
|
|
<Option value="light">Light</Option>
|
|
|
|
<Option value="cat">Cat</Option>
|
|
|
|
</Mentions>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
triggerInput(wrapper, '@notExist');
|
|
|
|
jest.runAllTimers();
|
|
|
|
|
|
|
|
wrapper.find('textarea').element.keyCode = KeyCode.ENTER;
|
|
|
|
wrapper.find('textarea').trigger('keydown');
|
|
|
|
jest.runAllTimers();
|
|
|
|
|
|
|
|
expect(wrapper.find('textarea').element.value).toBe('@notExist');
|
|
|
|
});
|
2020-03-07 11:45:13 +00:00
|
|
|
});
|