storyMock.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { mount, render, shallow } from 'enzyme';
  2. // some component story need special process
  3. // function componentTestAdapter(describeName, module) {
  4. // switch (describeName) {
  5. // case 'Banner':
  6. // let firstChild = document.createElement('div');
  7. // document.body.appendChild(firstChild);
  8. // break;
  9. // default:
  10. // break;
  11. // }
  12. // }
  13. export const storiesOf = function storiesOf(describeName, module) {
  14. let api = {};
  15. // describe('stories-' + describeName);
  16. api.add = function (name, funcDemo) {
  17. it(name, () => {
  18. const div = document.createElement('div');
  19. div.setAttribute('id', 'container');
  20. document.body.appendChild(div);
  21. // story snapshot test need stable random value
  22. window.crypto.getRandomValues = arr => arr.length;
  23. // can't attactTo document.body, some story will render to null, so weird.
  24. let wrapper = mount(funcDemo(), { attachTo: document.getElementById('container') });
  25. // Ingore Component Story
  26. const ingoreComponent = ['OverflowList', 'Table', 'Form', 'Select'];
  27. // OverflowList use IntersectionObserver API, can't easy mock in jest environment, just skip it.
  28. // TODO - Find out why test story of Table/Select/Form will cause a stack overflow
  29. if (ingoreComponent.includes(describeName)) {
  30. wrapper = null;
  31. }
  32. // componentTestAdapter(describeName, module);
  33. // const wrapper = mount(funcDemo(), { attachTo: document.body });
  34. expect(wrapper).toMatchSnapshot();
  35. document.body.removeChild(div);
  36. document.body.innerHTML = '';
  37. });
  38. return api;
  39. };
  40. api.addWithInfo = function (name, func) {
  41. func();
  42. return api;
  43. };
  44. api.addDecorator = function () {};
  45. return api;
  46. };