setup.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* global __dirname */
  2. import jQuery from 'jquery';
  3. import Adapter from 'enzyme-adapter-react-16';
  4. import Enzyme from 'enzyme'; // eslint-disable-line no-restricted-imports
  5. import MockDate from 'mockdate';
  6. import PropTypes from 'prop-types';
  7. import fromEntries from 'object.fromentries';
  8. import ConfigStore from 'app/stores/configStore';
  9. import {loadFixtures} from './sentry-test/loadFixtures';
  10. export * from './sentry-test/select';
  11. // We need this polyfill for testing only because
  12. // typescript handles it for main application
  13. fromEntries.shim();
  14. /**
  15. * Enzyme configuration
  16. */
  17. Enzyme.configure({adapter: new Adapter()});
  18. /**
  19. * Mock (current) date to alway be below
  20. */
  21. const constantDate = new Date(1508208080000); //National Pasta Day
  22. MockDate.set(constantDate);
  23. /**
  24. * Load all files in `tests/js/fixtures/*` as a module.
  25. * These will then be added to the `TestStubs` global below
  26. */
  27. const fixturesPath = `${__dirname}/sentry-test/fixtures`;
  28. const fixtures = loadFixtures(fixturesPath);
  29. /**
  30. * Global testing configuration
  31. */
  32. ConfigStore.loadInitialData({
  33. messages: [],
  34. user: fixtures.User(),
  35. });
  36. /**
  37. * Mocks
  38. */
  39. jest.mock('lodash/debounce', () => jest.fn(fn => fn));
  40. jest.mock('app/utils/recreateRoute');
  41. jest.mock('app/translations');
  42. jest.mock('app/api');
  43. jest.mock('app/utils/domId');
  44. jest.mock('app/utils/withOrganization');
  45. jest.mock('scroll-to-element', () => jest.fn());
  46. jest.mock('react-router', () => {
  47. const ReactRouter = require.requireActual('react-router');
  48. return {
  49. IndexRedirect: ReactRouter.IndexRedirect,
  50. IndexRoute: ReactRouter.IndexRoute,
  51. Link: ReactRouter.Link,
  52. Redirect: ReactRouter.Redirect,
  53. Route: ReactRouter.Route,
  54. withRouter: ReactRouter.withRouter,
  55. browserHistory: {
  56. goBack: jest.fn(),
  57. push: jest.fn(),
  58. replace: jest.fn(),
  59. listen: jest.fn(() => {}),
  60. },
  61. };
  62. });
  63. jest.mock('react-lazyload', () => {
  64. const LazyLoadMock = ({children}) => children;
  65. return LazyLoadMock;
  66. });
  67. jest.mock('react-virtualized', () => {
  68. const ActualReactVirtualized = require.requireActual('react-virtualized');
  69. return {
  70. ...ActualReactVirtualized,
  71. AutoSizer: ({children}) => {
  72. return children({width: 100, height: 100});
  73. },
  74. };
  75. });
  76. jest.mock('echarts-for-react/lib/core', () => {
  77. // We need to do this because `jest.mock` gets hoisted by babel and `React` is not
  78. // guaranteed to be in scope
  79. const ReactActual = require('react');
  80. // We need a class component here because `BaseChart` passes `ref` which will
  81. // error if we return a stateless/functional component
  82. return class extends ReactActual.Component {
  83. render() {
  84. return null;
  85. }
  86. };
  87. });
  88. jest.mock('@sentry/browser', () => {
  89. const SentryBrowser = require.requireActual('@sentry/browser');
  90. return {
  91. init: jest.fn(),
  92. configureScope: jest.fn(),
  93. captureBreadcrumb: jest.fn(),
  94. addBreadcrumb: jest.fn(),
  95. captureMessage: jest.fn(),
  96. captureException: jest.fn(),
  97. showReportDialog: jest.fn(),
  98. startSpan: jest.fn(),
  99. finishSpan: jest.fn(),
  100. lastEventId: jest.fn(),
  101. getCurrentHub: jest.spyOn(SentryBrowser, 'getCurrentHub'),
  102. withScope: jest.spyOn(SentryBrowser, 'withScope'),
  103. Severity: SentryBrowser.Severity,
  104. };
  105. });
  106. jest.mock('popper.js', () => {
  107. const PopperJS = jest.requireActual('popper.js');
  108. return class {
  109. static placements = PopperJS.placements;
  110. constructor() {
  111. return {
  112. destroy: () => {},
  113. scheduleUpdate: () => {},
  114. };
  115. }
  116. };
  117. });
  118. // We generally use actual jQuery, and jest mocks takes precedence over node_modules
  119. jest.unmock('jquery');
  120. /**
  121. * Test Globals
  122. */
  123. // This is so we can use async/await in tests instead of wrapping with `setTimeout`
  124. window.tick = () => new Promise(resolve => setTimeout(resolve));
  125. window.$ = window.jQuery = jQuery;
  126. window.scrollTo = jest.fn();
  127. // this is very commonly used, so expose it globally
  128. window.MockApiClient = require.requireMock('app/api').Client;
  129. window.TestStubs = {
  130. // react-router's 'router' context
  131. router: (params = {}) => ({
  132. push: jest.fn(),
  133. replace: jest.fn(),
  134. go: jest.fn(),
  135. goBack: jest.fn(),
  136. goForward: jest.fn(),
  137. listen: jest.fn(),
  138. setRouteLeaveHook: jest.fn(),
  139. isActive: jest.fn(),
  140. createHref: jest.fn(),
  141. location: {query: {}},
  142. ...params,
  143. }),
  144. location: (params = {}) => ({
  145. query: {},
  146. pathame: '/mock-pathname/',
  147. ...params,
  148. }),
  149. routes: () => [
  150. {path: '/'},
  151. {path: '/:orgId/'},
  152. {name: 'this should be skipped'},
  153. {path: '/organizations/:orgId/'},
  154. {path: 'api-keys/', name: 'API Key'},
  155. ],
  156. routerProps: (params = {}) => ({
  157. location: TestStubs.location(),
  158. params: {},
  159. routes: [],
  160. stepBack: () => {},
  161. ...params,
  162. }),
  163. routerContext: ([context, childContextTypes] = []) => ({
  164. context: {
  165. location: TestStubs.location(),
  166. router: TestStubs.router(),
  167. organization: fixtures.Organization(),
  168. project: fixtures.Project(),
  169. ...context,
  170. },
  171. childContextTypes: {
  172. router: PropTypes.object,
  173. location: PropTypes.object,
  174. organization: PropTypes.object,
  175. project: PropTypes.object,
  176. ...childContextTypes,
  177. },
  178. }),
  179. AllAuthenticators: () => {
  180. return Object.values(fixtures.Authenticators()).map(x => x());
  181. },
  182. ...fixtures,
  183. };