index.js 957 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { mount as baseMount, ReactWrapper } from 'enzyme';
  2. import { ReactNode } from 'react';
  3. export const CONTAINER_ID = `__container`;
  4. /**
  5. *
  6. * @param {ReactNode} reactNode
  7. * @param {string} [containerSelector]
  8. * @returns {ReactWrapper}
  9. */
  10. export function mount(reactNode, containerSelector = `#${CONTAINER_ID}`) {
  11. return baseMount(reactNode, {
  12. attachTo: document.querySelector(containerSelector),
  13. });
  14. }
  15. export const genBeforeEach = cb => () => {
  16. document.body.innerHTML = '';
  17. // Avoid `attachTo: document.body` Warning
  18. const div = document.createElement('div');
  19. div.setAttribute('id', CONTAINER_ID);
  20. document.body.appendChild(div);
  21. if (typeof cb === 'function') {
  22. cb();
  23. }
  24. };
  25. export const genAfterEach = cb => () => {
  26. const div = document.getElementById(CONTAINER_ID);
  27. if (div) {
  28. document.body.removeChild(div);
  29. }
  30. if (typeof cb === 'function') {
  31. cb();
  32. }
  33. };