setup.js 5.0 KB

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