setup.js 3.2 KB

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