setup.js 3.5 KB

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