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