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
it('focus() and onFocus', () => {
2019-01-12 03:33:27 +00:00
const handleFocus = jest.fn();
const wrapper = mount(
{
render(h) {
return <Component ref="component" onFocus={handleFocus} />;
},
2018-05-18 13:09:52 +00:00
},
2019-01-12 03:33:27 +00:00
{ attachToDocument: true, sync: false },
);
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(
{
render(h) {
return <Component ref="component" onBlur={handleBlur} />;
},
2018-05-18 13:09:52 +00:00
},
2019-01-12 03:33:27 +00:00
{ attachToDocument: true, sync: false },
);
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
2019-01-12 03:33:27 +00:00
it('autoFocus', done => {
jest.useRealTimers();
const handleFocus = jest.fn();
mount(
{
render(h) {
return <Component autoFocus onFocus={handleFocus} />;
},
2018-05-18 13:09:52 +00:00
},
2019-01-12 03:33:27 +00:00
{ attachToDocument: true, sync: false },
);
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
}