focusTest.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import React from 'react';
  2. import { mount } from 'enzyme';
  3. export default function focusTest(Component) {
  4. describe('focus and blur', () => {
  5. beforeAll(() => {
  6. jest.useFakeTimers();
  7. });
  8. let container;
  9. beforeEach(() => {
  10. container = document.createElement('div');
  11. document.body.appendChild(container);
  12. });
  13. afterAll(() => {
  14. jest.useRealTimers();
  15. });
  16. afterEach(() => {
  17. document.body.removeChild(container);
  18. });
  19. it('focus() and onFocus', () => {
  20. const handleFocus = jest.fn();
  21. const wrapper = mount(<Component onFocus={handleFocus} />, { attachTo: container });
  22. wrapper.instance().focus();
  23. jest.runAllTimers();
  24. expect(handleFocus).toHaveBeenCalled();
  25. });
  26. it('blur() and onBlur', () => {
  27. const handleBlur = jest.fn();
  28. const wrapper = mount(<Component onBlur={handleBlur} />, { attachTo: container });
  29. wrapper.instance().focus();
  30. jest.runAllTimers();
  31. wrapper.instance().blur();
  32. jest.runAllTimers();
  33. expect(handleBlur).toHaveBeenCalled();
  34. });
  35. it('autoFocus', () => {
  36. const handleFocus = jest.fn();
  37. mount(<Component autoFocus onFocus={handleFocus} />, { attachTo: container });
  38. jest.runAllTimers();
  39. expect(handleFocus).toHaveBeenCalled();
  40. });
  41. });
  42. }