setup.js 4.9 KB

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