2019-01-12 03:33:27 +00:00
|
|
|
import { mount } from '@vue/test-utils';
|
2021-08-26 14:29:46 +00:00
|
|
|
import { sleep } from '../utils';
|
2018-05-20 14:30:48 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
export default function focusTest(Component) {
|
2018-05-18 13:09:52 +00:00
|
|
|
describe('focus and blur', () => {
|
2021-08-26 14:29:46 +00:00
|
|
|
let container;
|
|
|
|
beforeEach(() => {
|
|
|
|
container = document.createElement('div');
|
|
|
|
document.body.appendChild(container);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
document.body.removeChild(container);
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-05-18 13:09:52 +00:00
|
|
|
|
2020-07-28 07:23:56 +00:00
|
|
|
it('focus() and onFocus', async () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const handleFocus = jest.fn();
|
|
|
|
const wrapper = mount(
|
|
|
|
{
|
2020-07-28 07:23:56 +00:00
|
|
|
render() {
|
2019-01-12 03:33:27 +00:00
|
|
|
return <Component ref="component" onFocus={handleFocus} />;
|
|
|
|
},
|
2018-05-18 13:09:52 +00:00
|
|
|
},
|
2021-08-26 14:29:46 +00:00
|
|
|
{ attachTo: container, sync: false },
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2021-08-26 16:09:37 +00:00
|
|
|
await sleep();
|
2019-01-12 03:33:27 +00:00
|
|
|
wrapper.vm.$refs.component.focus();
|
2021-08-26 16:09:37 +00:00
|
|
|
await sleep();
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(handleFocus).toBeCalled();
|
|
|
|
});
|
2018-05-18 13:09:52 +00:00
|
|
|
|
2021-08-28 13:57:12 +00:00
|
|
|
it('blur() and onBlur', async () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const handleBlur = jest.fn();
|
2021-08-26 14:29:46 +00:00
|
|
|
const handleFocus = jest.fn();
|
2019-01-12 03:33:27 +00:00
|
|
|
const wrapper = mount(
|
|
|
|
{
|
2020-07-28 07:23:56 +00:00
|
|
|
render() {
|
2021-08-26 14:29:46 +00:00
|
|
|
return <Component ref="component" onFocus={handleFocus} onBlur={handleBlur} />;
|
2019-01-12 03:33:27 +00:00
|
|
|
},
|
2018-05-18 13:09:52 +00:00
|
|
|
},
|
2021-08-26 14:29:46 +00:00
|
|
|
{ attachTo: container, sync: false },
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2021-08-26 16:09:37 +00:00
|
|
|
wrapper.vm.$refs.component.focus();
|
2019-01-12 03:33:27 +00:00
|
|
|
wrapper.vm.$refs.component.blur();
|
2021-08-26 16:09:37 +00:00
|
|
|
await sleep(3000);
|
2021-08-26 14:29:46 +00:00
|
|
|
expect(handleBlur).toBeCalled();
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-05-18 13:09:52 +00:00
|
|
|
|
2021-08-26 14:29:46 +00:00
|
|
|
it('autofocus', async () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const handleFocus = jest.fn();
|
2021-08-26 16:09:37 +00:00
|
|
|
mount(
|
2019-01-12 03:33:27 +00:00
|
|
|
{
|
2020-07-28 07:23:56 +00:00
|
|
|
render() {
|
2021-08-26 16:09:37 +00:00
|
|
|
return <Component autofocus onFocus={handleFocus} />;
|
2019-01-12 03:33:27 +00:00
|
|
|
},
|
2018-05-18 13:09:52 +00:00
|
|
|
},
|
2021-08-26 14:29:46 +00:00
|
|
|
{ attachTo: container, sync: false },
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2021-08-26 16:09:37 +00:00
|
|
|
await sleep();
|
2021-08-26 14:29:46 +00:00
|
|
|
expect(handleFocus).toBeCalled();
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
|
|
|
});
|
2018-05-18 13:09:52 +00:00
|
|
|
}
|