setup.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. 'use strict';
  10. const MockNativeMethods = jest.requireActual('./MockNativeMethods');
  11. const mockComponent = jest.requireActual('./mockComponent');
  12. jest.requireActual('../Libraries/polyfills/Object.es7.js');
  13. jest.requireActual('../Libraries/polyfills/error-guard');
  14. global.__DEV__ = true;
  15. global.Promise = jest.requireActual('promise');
  16. global.regeneratorRuntime = jest.requireActual('regenerator-runtime/runtime');
  17. global.requestAnimationFrame = function(callback) {
  18. return setTimeout(callback, 0);
  19. };
  20. global.cancelAnimationFrame = function(id) {
  21. clearTimeout(id);
  22. };
  23. // there's a __mock__ for it.
  24. jest.setMock(
  25. '../Libraries/vendor/core/ErrorUtils',
  26. require('../Libraries/vendor/core/ErrorUtils'),
  27. );
  28. jest
  29. .mock('../Libraries/Core/InitializeCore', () => {})
  30. .mock('../Libraries/Core/NativeExceptionsManager', () => ({
  31. __esModule: true,
  32. default: {
  33. reportException: jest.fn(),
  34. },
  35. }))
  36. .mock('../Libraries/ReactNative/UIManager', () => ({
  37. AndroidViewPager: {
  38. Commands: {
  39. setPage: jest.fn(),
  40. setPageWithoutAnimation: jest.fn(),
  41. },
  42. },
  43. blur: jest.fn(),
  44. createView: jest.fn(),
  45. customBubblingEventTypes: {},
  46. customDirectEventTypes: {},
  47. dispatchViewManagerCommand: jest.fn(),
  48. focus: jest.fn(),
  49. getViewManagerConfig: jest.fn(name => {
  50. if (name === 'AndroidDrawerLayout') {
  51. return {
  52. Constants: {
  53. DrawerPosition: {
  54. Left: 10,
  55. },
  56. },
  57. };
  58. }
  59. }),
  60. measure: jest.fn(),
  61. manageChildren: jest.fn(),
  62. removeSubviewsFromContainerWithID: jest.fn(),
  63. replaceExistingNonRootView: jest.fn(),
  64. setChildren: jest.fn(),
  65. updateView: jest.fn(),
  66. AndroidDrawerLayout: {
  67. Constants: {
  68. DrawerPosition: {
  69. Left: 10,
  70. },
  71. },
  72. },
  73. AndroidTextInput: {
  74. Commands: {},
  75. },
  76. ScrollView: {
  77. Constants: {},
  78. },
  79. View: {
  80. Constants: {},
  81. },
  82. }))
  83. .mock('../Libraries/Image/Image', () =>
  84. mockComponent('../Libraries/Image/Image'),
  85. )
  86. .mock('../Libraries/Text/Text', () =>
  87. mockComponent('../Libraries/Text/Text', MockNativeMethods),
  88. )
  89. .mock('../Libraries/Components/TextInput/TextInput', () =>
  90. mockComponent('../Libraries/Components/TextInput/TextInput', {
  91. ...MockNativeMethods,
  92. isFocused: jest.fn(),
  93. clear: jest.fn(),
  94. getNativeRef: jest.fn(),
  95. }),
  96. )
  97. .mock('../Libraries/Modal/Modal', () =>
  98. mockComponent('../Libraries/Modal/Modal'),
  99. )
  100. .mock('../Libraries/Components/View/View', () =>
  101. mockComponent('../Libraries/Components/View/View', MockNativeMethods),
  102. )
  103. .mock('../Libraries/Components/AccessibilityInfo/AccessibilityInfo', () => ({
  104. addEventListener: jest.fn(),
  105. announceForAccessibility: jest.fn(),
  106. fetch: jest.fn(),
  107. isBoldTextEnabled: jest.fn(),
  108. isGrayscaleEnabled: jest.fn(),
  109. isInvertColorsEnabled: jest.fn(),
  110. isReduceMotionEnabled: jest.fn(),
  111. isReduceTransparencyEnabled: jest.fn(),
  112. isScreenReaderEnabled: jest.fn(() => Promise.resolve(false)),
  113. removeEventListener: jest.fn(),
  114. setAccessibilityFocus: jest.fn(),
  115. }))
  116. .mock('../Libraries/Components/RefreshControl/RefreshControl', () =>
  117. jest.requireActual(
  118. '../Libraries/Components/RefreshControl/__mocks__/RefreshControlMock',
  119. ),
  120. )
  121. .mock('../Libraries/Components/ScrollView/ScrollView', () => {
  122. const baseComponent = mockComponent(
  123. '../Libraries/Components/ScrollView/ScrollView',
  124. {
  125. ...MockNativeMethods,
  126. getScrollResponder: jest.fn(),
  127. getScrollableNode: jest.fn(),
  128. getInnerViewNode: jest.fn(),
  129. getInnerViewRef: jest.fn(),
  130. getNativeScrollRef: jest.fn(),
  131. scrollTo: jest.fn(),
  132. scrollToEnd: jest.fn(),
  133. flashScrollIndicators: jest.fn(),
  134. scrollResponderZoomTo: jest.fn(),
  135. scrollResponderScrollNativeHandleToKeyboard: jest.fn(),
  136. },
  137. );
  138. const mockScrollView = jest.requireActual('./mockScrollView');
  139. return mockScrollView(baseComponent);
  140. })
  141. .mock('../Libraries/Components/ActivityIndicator/ActivityIndicator', () =>
  142. mockComponent(
  143. '../Libraries/Components/ActivityIndicator/ActivityIndicator',
  144. ),
  145. )
  146. .mock('../Libraries/AppState/AppState', () => ({
  147. addEventListener: jest.fn(),
  148. removeEventListener: jest.fn(),
  149. }))
  150. .mock('../Libraries/Linking/Linking', () => ({
  151. openURL: jest.fn(),
  152. canOpenURL: jest.fn(() => Promise.resolve(true)),
  153. openSettings: jest.fn(),
  154. addEventListener: jest.fn(),
  155. getInitialURL: jest.fn(() => Promise.resolve()),
  156. removeEventListener: jest.fn(),
  157. sendIntent: jest.fn(),
  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. return viewName => {
  315. const Component = class extends React.Component {
  316. render() {
  317. return React.createElement(viewName, this.props, this.props.children);
  318. }
  319. // The methods that exist on host components
  320. blur = jest.fn();
  321. focus = jest.fn();
  322. measure = jest.fn();
  323. measureInWindow = jest.fn();
  324. measureLayout = jest.fn();
  325. setNativeProps = jest.fn();
  326. };
  327. if (viewName === 'RCTView') {
  328. Component.displayName = 'View';
  329. } else {
  330. Component.displayName = viewName;
  331. }
  332. return Component;
  333. };
  334. })
  335. .mock(
  336. '../Libraries/Utilities/verifyComponentAttributeEquivalence',
  337. () => function() {},
  338. )
  339. .mock('../Libraries/Components/View/ViewNativeComponent', () => {
  340. const React = require('react');
  341. const Component = class extends React.Component {
  342. render() {
  343. return React.createElement('View', this.props, this.props.children);
  344. }
  345. };
  346. Component.displayName = 'View';
  347. return {
  348. __esModule: true,
  349. default: Component,
  350. };
  351. });