setup.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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/domId');
  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. getCurrentHub: jest.spyOn(SentryBrowser, 'getCurrentHub'),
  102. withScope: jest.spyOn(SentryBrowser, 'withScope'),
  103. };
  104. });
  105. jest.mock('popper.js', () => {
  106. const PopperJS = jest.requireActual('popper.js');
  107. return class {
  108. static placements = PopperJS.placements;
  109. constructor() {
  110. return {
  111. destroy: () => {},
  112. scheduleUpdate: () => {},
  113. };
  114. }
  115. };
  116. });
  117. // We generally use actual jQuery, and jest mocks takes precedence over node_modules
  118. jest.unmock('jquery');
  119. /**
  120. * Test Globals
  121. */
  122. // This is so we can use async/await in tests instead of wrapping with `setTimeout`
  123. window.tick = () => new Promise(resolve => setTimeout(resolve));
  124. window.$ = window.jQuery = jQuery;
  125. window.scrollTo = jest.fn();
  126. // this is very commonly used, so expose it globally
  127. window.MockApiClient = require.requireMock('app/api').Client;
  128. window.TestStubs = {
  129. // react-router's 'router' context
  130. router: (params = {}) => ({
  131. push: jest.fn(),
  132. replace: jest.fn(),
  133. go: jest.fn(),
  134. goBack: jest.fn(),
  135. goForward: jest.fn(),
  136. listen: jest.fn(),
  137. setRouteLeaveHook: jest.fn(),
  138. isActive: jest.fn(),
  139. createHref: jest.fn(),
  140. location: {query: {}},
  141. ...params,
  142. }),
  143. location: (params = {}) => ({
  144. query: {},
  145. pathame: '/mock-pathname/',
  146. ...params,
  147. }),
  148. routes: () => [
  149. {path: '/'},
  150. {path: '/:orgId/'},
  151. {name: 'this should be skipped'},
  152. {path: '/organizations/:orgId/'},
  153. {path: 'api-keys/', name: 'API Key'},
  154. ],
  155. routerProps: (params = {}) => ({
  156. location: TestStubs.location(),
  157. params: {},
  158. routes: [],
  159. stepBack: () => {},
  160. ...params,
  161. }),
  162. routerContext: ([context, childContextTypes] = []) => ({
  163. context: {
  164. [channel]: {
  165. subscribe: broadcast.subscribe,
  166. unsubscribe: broadcast.unsubscribe,
  167. },
  168. location: TestStubs.location(),
  169. router: TestStubs.router(),
  170. organization: fixtures.Organization(),
  171. project: fixtures.Project(),
  172. ...context,
  173. },
  174. childContextTypes: {
  175. [channel]: PropTypes.object,
  176. router: PropTypes.object,
  177. location: PropTypes.object,
  178. organization: PropTypes.object,
  179. project: PropTypes.object,
  180. ...childContextTypes,
  181. },
  182. }),
  183. AllAuthenticators: () => {
  184. return Object.values(fixtures.Authenticators()).map(x => x());
  185. },
  186. ...fixtures,
  187. };