ant-design-vue/tests/shared/focusTest.js

65 lines
1.5 KiB
JavaScript
Raw Normal View History

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