setup.js 5.5 KB

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