2019-01-12 11:33:27 +08:00
|
|
|
import { mount } from '@vue/test-utils';
|
|
|
|
import { asyncExpect } from '../utils';
|
2018-05-20 22:30:48 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
export default function focusTest(Component) {
|
2018-05-18 21:09:52 +08:00
|
|
|
describe('focus and blur', () => {
|
|
|
|
beforeAll(() => {
|
2019-01-12 11:33:27 +08:00
|
|
|
jest.useFakeTimers();
|
|
|
|
});
|
2018-05-18 21:09:52 +08:00
|
|
|
|
|
|
|
afterAll(() => {
|
2019-01-12 11:33:27 +08:00
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
2018-05-18 21:09:52 +08:00
|
|
|
|
2020-07-28 15:23:56 +08:00
|
|
|
it('focus() and onFocus', async () => {
|
2019-01-12 11:33:27 +08:00
|
|
|
const handleFocus = jest.fn();
|
|
|
|
const wrapper = mount(
|
|
|
|
{
|
2020-07-28 15:23:56 +08:00
|
|
|
render() {
|
2019-01-12 11:33:27 +08:00
|
|
|
return <Component ref="component" onFocus={handleFocus} />;
|
|
|
|
},
|
2018-05-18 21:09:52 +08:00
|
|
|
},
|
2020-07-28 15:23:56 +08:00
|
|
|
{ attachTo: 'body', sync: false },
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
|
|
|
wrapper.vm.$refs.component.focus();
|
|
|
|
jest.runAllTimers();
|
|
|
|
expect(handleFocus).toBeCalled();
|
|
|
|
});
|
2018-05-18 21:09:52 +08:00
|
|
|
|
2018-06-09 15:48:34 +08:00
|
|
|
it('blur() and onBlur', async () => {
|
2019-01-12 11:33:27 +08:00
|
|
|
const handleBlur = jest.fn();
|
|
|
|
const wrapper = mount(
|
|
|
|
{
|
2020-07-28 15:23:56 +08:00
|
|
|
render() {
|
2019-01-12 11:33:27 +08:00
|
|
|
return <Component ref="component" onBlur={handleBlur} />;
|
|
|
|
},
|
2018-05-18 21:09:52 +08:00
|
|
|
},
|
2020-07-28 15:23:56 +08:00
|
|
|
{ attachTo: 'body', sync: false },
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
|
|
|
wrapper.vm.$refs.component.focus();
|
|
|
|
wrapper.vm.$refs.component.blur();
|
|
|
|
jest.runAllTimers();
|
2018-06-09 15:48:34 +08:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 11:33:27 +08:00
|
|
|
expect(handleBlur).toBeCalled();
|
|
|
|
});
|
|
|
|
});
|
2018-05-18 21:09:52 +08:00
|
|
|
|
2020-07-17 17:36:58 +08:00
|
|
|
it('autofocus', done => {
|
2019-01-12 11:33:27 +08:00
|
|
|
jest.useRealTimers();
|
|
|
|
const handleFocus = jest.fn();
|
|
|
|
mount(
|
|
|
|
{
|
2020-07-28 15:23:56 +08:00
|
|
|
render() {
|
|
|
|
return <Component autofocus={true} onFocus={handleFocus} />;
|
2019-01-12 11:33:27 +08:00
|
|
|
},
|
2018-05-18 21:09:52 +08:00
|
|
|
},
|
2020-07-28 15:23:56 +08:00
|
|
|
{ attachTo: 'body', sync: false },
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-05-18 21:09:52 +08:00
|
|
|
setTimeout(() => {
|
2019-01-12 11:33:27 +08:00
|
|
|
expect(handleFocus).toBeCalled();
|
|
|
|
done();
|
|
|
|
}, 1000);
|
|
|
|
});
|
|
|
|
});
|
2018-05-18 21:09:52 +08:00
|
|
|
}
|