setup.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import Adapter from 'enzyme-adapter-react-16';
  2. import Enzyme, { shallow, render, mount } from 'enzyme';
  3. import React from 'react';
  4. import sinon from 'sinon';
  5. import enzymeToJson from 'enzyme-to-json';
  6. import jest from 'jest';
  7. import jsdom from 'jsdom';
  8. import crypto from 'crypto';
  9. import { advanceBy, advanceTo, clear } from 'jest-date-mock';
  10. import { mockRandom } from 'jest-mock-random';
  11. import { IntersectionObserver } from '@shopify/jest-dom-mocks';
  12. // window.requestAnimationFrame = function (callback) {
  13. // setTimeout(callback, 0);
  14. // };
  15. // window.addEventListener = () => {};
  16. // React 16 Enzyme adapter
  17. Enzyme.configure({
  18. adapter: new Adapter(),
  19. });
  20. class ResizeObserver {
  21. observe() {
  22. // do nothing
  23. }
  24. unobserve() {
  25. // do nothing
  26. }
  27. disconnect() {
  28. // do nothing
  29. }
  30. }
  31. global.ResizeObserver = ResizeObserver;
  32. // Define globals to cut down on imports in test file
  33. global.React = React;
  34. global.shallow = shallow;
  35. global.render = render;
  36. global.mount = mount;
  37. global.sinon = sinon;
  38. Object.defineProperty(global.self, 'crypto', {
  39. value: {
  40. getRandomValues: arr => crypto.randomBytes(arr.length),
  41. },
  42. });
  43. // 固定每次直接无入参调用new Date()的结果
  44. advanceTo(new Date('2019-08-08 12:00:00'));
  45. // 固定每次调用Math.random的结果
  46. mockRandom([0.1, 0.2, 0.3, 0.6]);
  47. // global.IntersectionObserver = IntersectionObserver;
  48. global.MutationObserver = class {
  49. // eslint-disable-next-line no-useless-constructor
  50. constructor(callback) {}
  51. disconnect() {}
  52. observe(element, initObject) {}
  53. };
  54. global.IntersectionObserver = class IntersectionObserver {
  55. disconnect() {
  56. return null;
  57. }
  58. observe() {
  59. return null;
  60. }
  61. takeRecords() {
  62. return null;
  63. }
  64. unobserve() {
  65. return null;
  66. }
  67. };
  68. global.matchMedia = global.matchMedia || function () {
  69. return {
  70. matches: false,
  71. addListener() {},
  72. removeListener() {},
  73. addEventListener() {},
  74. removeEventListener() {}
  75. };
  76. };
  77. global.HTMLElement.prototype.getBoundingClientRect = function () {
  78. return {
  79. width: parseFloat(this.style.width) || 0,
  80. height: parseFloat(this.style.height) || 0,
  81. top: parseFloat(this.style.marginTop) || 0,
  82. left: parseFloat(this.style.marginLeft) || 0
  83. };
  84. };
  85. Object.defineProperties(global.HTMLElement.prototype, {
  86. offsetWidth: {
  87. get() {
  88. return parseFloat(this.style.width) || 0;
  89. }
  90. },
  91. offsetHeight: {
  92. get() {
  93. return parseFloat(this.style.height) || 0;
  94. }
  95. },
  96. offsetTop: {
  97. get() {
  98. return parseFloat(this.style.marginTop) || 0;
  99. }
  100. },
  101. offsetLeft: {
  102. get() {
  103. return parseFloat(this.style.marginLeft) || 0;
  104. }
  105. }
  106. });
  107. // const { JSDOM } = jsdom;
  108. // const dom = new JSDOM(`<!doctype html>
  109. // <html>
  110. // <body></body>
  111. // </html>`);
  112. // const { window } = dom;
  113. // function copyProps(src, target) {
  114. // Object.defineProperties(target, {
  115. // ...Object.getOwnPropertyDescriptors(src),
  116. // ...Object.getOwnPropertyDescriptors(target),
  117. // });
  118. // }
  119. // global.window = window;
  120. // global.document = window.document;
  121. // // global.navigator = {
  122. // // userAgent: 'node.js',
  123. // // };
  124. // global.requestAnimationFrame = function (callback) {
  125. // return setTimeout(callback, 0);
  126. // };
  127. // global.cancelAnimationFrame = function (id) {
  128. // clearTimeout(id);
  129. // };
  130. // copyProps(window, global);