setup.js 3.6 KB

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