UNPKG

10.1 kBJavaScriptView Raw
1/**
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @format
8 */
9
10'use strict';
11
12const MockNativeMethods = jest.requireActual('./MockNativeMethods');
13const mockComponent = jest.requireActual('./mockComponent');
14
15jest.requireActual('../Libraries/polyfills/Object.es7.js');
16jest.requireActual('../Libraries/polyfills/error-guard');
17
18global.__DEV__ = true;
19
20global.Promise = jest.requireActual('promise');
21global.regeneratorRuntime = jest.requireActual('regenerator-runtime/runtime');
22
23global.requestAnimationFrame = function(callback) {
24 return setTimeout(callback, 0);
25};
26global.cancelAnimationFrame = function(id) {
27 clearTimeout(id);
28};
29
30// there's a __mock__ for it.
31jest.setMock(
32 '../Libraries/vendor/core/ErrorUtils',
33 require('../Libraries/vendor/core/ErrorUtils'),
34);
35
36jest
37 .mock('../Libraries/Core/InitializeCore', () => {})
38 .mock('../Libraries/ReactNative/UIManager', () => ({
39 AndroidViewPager: {
40 Commands: {
41 setPage: jest.fn(),
42 setPageWithoutAnimation: jest.fn(),
43 },
44 },
45 blur: jest.fn(),
46 createView: jest.fn(),
47 customBubblingEventTypes: {},
48 customDirectEventTypes: {},
49 dispatchViewManagerCommand: jest.fn(),
50 focus: jest.fn(),
51 getViewManagerConfig: jest.fn(name => {
52 if (name === 'AndroidDrawerLayout') {
53 return {
54 Constants: {
55 DrawerPosition: {
56 Left: 10,
57 },
58 },
59 };
60 }
61 }),
62 measure: jest.fn(),
63 manageChildren: jest.fn(),
64 removeSubviewsFromContainerWithID: jest.fn(),
65 replaceExistingNonRootView: jest.fn(),
66 setChildren: jest.fn(),
67 updateView: jest.fn(),
68 AndroidDrawerLayout: {
69 Constants: {
70 DrawerPosition: {
71 Left: 10,
72 },
73 },
74 },
75 AndroidTextInput: {
76 Commands: {},
77 },
78 ScrollView: {
79 Constants: {},
80 },
81 View: {
82 Constants: {},
83 },
84 }))
85 .mock('../Libraries/Image/Image', () =>
86 mockComponent('../Libraries/Image/Image'),
87 )
88 .mock('../Libraries/Text/Text', () =>
89 mockComponent('../Libraries/Text/Text', MockNativeMethods),
90 )
91 .mock('../Libraries/Components/TextInput/TextInput', () =>
92 mockComponent('../Libraries/Components/TextInput/TextInput', {
93 ...MockNativeMethods,
94 isFocused: jest.fn(),
95 clear: jest.fn(),
96 getNativeRef: jest.fn(),
97 }),
98 )
99 .mock('../Libraries/Modal/Modal', () =>
100 mockComponent('../Libraries/Modal/Modal'),
101 )
102 .mock('../Libraries/Components/View/View', () =>
103 mockComponent('../Libraries/Components/View/View', MockNativeMethods),
104 )
105 .mock('../Libraries/Components/AccessibilityInfo/AccessibilityInfo', () => ({
106 addEventListener: jest.fn(),
107 announceForAccessibility: jest.fn(),
108 fetch: jest.fn(),
109 isBoldTextEnabled: jest.fn(),
110 isGrayscaleEnabled: jest.fn(),
111 isInvertColorsEnabled: jest.fn(),
112 isReduceMotionEnabled: jest.fn(),
113 isReduceTransparencyEnabled: jest.fn(),
114 isScreenReaderEnabled: jest.fn(),
115 removeEventListener: jest.fn(),
116 setAccessibilityFocus: jest.fn(),
117 }))
118 .mock('../Libraries/Components/RefreshControl/RefreshControl', () =>
119 jest.requireActual(
120 '../Libraries/Components/RefreshControl/__mocks__/RefreshControlMock',
121 ),
122 )
123 .mock('../Libraries/Components/ScrollView/ScrollView', () =>
124 jest.requireActual(
125 '../Libraries/Components/ScrollView/__mocks__/ScrollViewMock',
126 ),
127 )
128 .mock('../Libraries/Components/ActivityIndicator/ActivityIndicator', () =>
129 mockComponent(
130 '../Libraries/Components/ActivityIndicator/ActivityIndicator',
131 ),
132 )
133 .mock('../Libraries/AppState/AppState', () => ({
134 addEventListener: jest.fn(),
135 removeEventListener: jest.fn(),
136 }))
137 .mock('../Libraries/Linking/Linking', () => ({
138 openURL: jest.fn(),
139 canOpenURL: jest.fn(() => Promise.resolve(true)),
140 openSettings: jest.fn(),
141 addEventListener: jest.fn(),
142 getInitialURL: jest.fn(() => Promise.resolve()),
143 removeEventListener: jest.fn(),
144 sendIntent: jest.fn(),
145 }))
146 .mock('../Libraries/Renderer/shims/ReactNative', () => {
147 const ReactNative = jest.requireActual(
148 '../Libraries/Renderer/shims/ReactNative',
149 );
150 const NativeMethodsMixin =
151 ReactNative.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
152 .NativeMethodsMixin;
153
154 Object.assign(NativeMethodsMixin, MockNativeMethods);
155 Object.assign(ReactNative.NativeComponent.prototype, MockNativeMethods);
156
157 return ReactNative;
158 })
159 // Mock modules defined by the native layer (ex: Objective-C, Java)
160 .mock('../Libraries/BatchedBridge/NativeModules', () => ({
161 AlertManager: {
162 alertWithArgs: jest.fn(),
163 },
164 AsyncLocalStorage: {
165 multiGet: jest.fn((keys, callback) =>
166 process.nextTick(() => callback(null, [])),
167 ),
168 multiSet: jest.fn((entries, callback) =>
169 process.nextTick(() => callback(null)),
170 ),
171 multiRemove: jest.fn((keys, callback) =>
172 process.nextTick(() => callback(null)),
173 ),
174 multiMerge: jest.fn((entries, callback) =>
175 process.nextTick(() => callback(null)),
176 ),
177 clear: jest.fn(callback => process.nextTick(() => callback(null))),
178 getAllKeys: jest.fn(callback =>
179 process.nextTick(() => callback(null, [])),
180 ),
181 },
182 Clipboard: {
183 getString: jest.fn(() => ''),
184 setString: jest.fn(),
185 },
186 DeviceInfo: {
187 getConstants() {
188 return {
189 Dimensions: {
190 window: {
191 fontScale: 2,
192 height: 1334,
193 scale: 2,
194 width: 750,
195 },
196 screen: {
197 fontScale: 2,
198 height: 1334,
199 scale: 2,
200 width: 750,
201 },
202 },
203 };
204 },
205 },
206 ImageLoader: {
207 getSize: jest.fn(url => Promise.resolve({width: 320, height: 240})),
208 prefetchImage: jest.fn(),
209 },
210 ImageViewManager: {
211 getSize: jest.fn((uri, success) =>
212 process.nextTick(() => success(320, 240)),
213 ),
214 prefetchImage: jest.fn(),
215 },
216 KeyboardObserver: {
217 addListener: jest.fn(),
218 removeListeners: jest.fn(),
219 },
220 Networking: {
221 sendRequest: jest.fn(),
222 abortRequest: jest.fn(),
223 addListener: jest.fn(),
224 removeListeners: jest.fn(),
225 },
226 PlatformConstants: {
227 getConstants() {
228 return {};
229 },
230 },
231 PushNotificationManager: {
232 presentLocalNotification: jest.fn(),
233 scheduleLocalNotification: jest.fn(),
234 cancelAllLocalNotifications: jest.fn(),
235 removeAllDeliveredNotifications: jest.fn(),
236 getDeliveredNotifications: jest.fn(callback =>
237 process.nextTick(() => []),
238 ),
239 removeDeliveredNotifications: jest.fn(),
240 setApplicationIconBadgeNumber: jest.fn(),
241 getApplicationIconBadgeNumber: jest.fn(callback =>
242 process.nextTick(() => callback(0)),
243 ),
244 cancelLocalNotifications: jest.fn(),
245 getScheduledLocalNotifications: jest.fn(callback =>
246 process.nextTick(() => callback()),
247 ),
248 requestPermissions: jest.fn(() =>
249 Promise.resolve({alert: true, badge: true, sound: true}),
250 ),
251 abandonPermissions: jest.fn(),
252 checkPermissions: jest.fn(callback =>
253 process.nextTick(() =>
254 callback({alert: true, badge: true, sound: true}),
255 ),
256 ),
257 getInitialNotification: jest.fn(() => Promise.resolve(null)),
258 addListener: jest.fn(),
259 removeListeners: jest.fn(),
260 },
261 SourceCode: {
262 getConstants() {
263 return {
264 scriptURL: null,
265 };
266 },
267 },
268 StatusBarManager: {
269 setColor: jest.fn(),
270 setStyle: jest.fn(),
271 setHidden: jest.fn(),
272 setNetworkActivityIndicatorVisible: jest.fn(),
273 setBackgroundColor: jest.fn(),
274 setTranslucent: jest.fn(),
275 getConstants: () => ({
276 HEIGHT: 42,
277 }),
278 },
279 Timing: {
280 createTimer: jest.fn(),
281 deleteTimer: jest.fn(),
282 },
283 UIManager: {},
284 BlobModule: {
285 getConstants: () => ({BLOB_URI_SCHEME: 'content', BLOB_URI_HOST: null}),
286 addNetworkingHandler: jest.fn(),
287 enableBlobSupport: jest.fn(),
288 disableBlobSupport: jest.fn(),
289 createFromParts: jest.fn(),
290 sendBlob: jest.fn(),
291 release: jest.fn(),
292 },
293 WebSocketModule: {
294 connect: jest.fn(),
295 send: jest.fn(),
296 sendBinary: jest.fn(),
297 ping: jest.fn(),
298 close: jest.fn(),
299 addListener: jest.fn(),
300 removeListeners: jest.fn(),
301 },
302 I18nManager: {
303 allowRTL: jest.fn(),
304 forceRTL: jest.fn(),
305 swapLeftAndRightInRTL: jest.fn(),
306 getConstants: () => ({
307 isRTL: false,
308 doLeftAndRightSwapInRTL: true,
309 }),
310 },
311 }))
312 .mock('../Libraries/ReactNative/requireNativeComponent', () => {
313 const React = require('react');
314
315 return viewName => {
316 const Component = class extends React.Component {
317 render() {
318 return React.createElement(viewName, this.props, this.props.children);
319 }
320
321 // The methods that exist on host components
322 blur = jest.fn();
323 focus = jest.fn();
324 measure = jest.fn();
325 measureInWindow = jest.fn();
326 measureLayout = jest.fn();
327 setNativeProps = jest.fn();
328 };
329
330 if (viewName === 'RCTView') {
331 Component.displayName = 'View';
332 } else {
333 Component.displayName = viewName;
334 }
335
336 return Component;
337 };
338 })
339 .mock(
340 '../Libraries/Utilities/verifyComponentAttributeEquivalence',
341 () => function() {},
342 )
343 .mock('../Libraries/Components/View/ViewNativeComponent', () => {
344 const React = require('react');
345 const Component = class extends React.Component {
346 render() {
347 return React.createElement('View', this.props, this.props.children);
348 }
349 };
350
351 Component.displayName = 'View';
352
353 return {
354 __esModule: true,
355 default: Component,
356 };
357 });