setup.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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/babelHelpers.js');
  13. jest.requireActual('../Libraries/polyfills/Object.es7.js');
  14. jest.requireActual('../Libraries/polyfills/error-guard');
  15. global.__DEV__ = true;
  16. global.Promise = jest.requireActual('promise');
  17. global.regeneratorRuntime = jest.requireActual('regenerator-runtime/runtime');
  18. global.requestAnimationFrame = function(callback) {
  19. return setTimeout(callback, 0);
  20. };
  21. global.cancelAnimationFrame = function(id) {
  22. clearTimeout(id);
  23. };
  24. jest.mock('setupDevtools').mock('npmlog');
  25. // there's a __mock__ for it.
  26. jest.setMock('ErrorUtils', require('ErrorUtils'));
  27. jest
  28. .mock('InitializeCore', () => {})
  29. .mock('Image', () => mockComponent('Image'))
  30. .mock('Text', () => mockComponent('Text', MockNativeMethods))
  31. .mock('TextInput', () => mockComponent('TextInput'))
  32. .mock('Modal', () => mockComponent('Modal'))
  33. .mock('View', () => mockComponent('View', MockNativeMethods))
  34. .mock('RefreshControl', () => jest.requireMock('RefreshControlMock'))
  35. .mock('ScrollView', () => jest.requireMock('ScrollViewMock'))
  36. .mock('ActivityIndicator', () => mockComponent('ActivityIndicator'))
  37. .mock('ListView', () => jest.requireMock('ListViewMock'))
  38. .mock('ListViewDataSource', () => {
  39. const DataSource = jest.requireActual('ListViewDataSource');
  40. DataSource.prototype.toJSON = function() {
  41. function ListViewDataSource(dataBlob) {
  42. this.items = 0;
  43. // Ensure this doesn't throw.
  44. try {
  45. Object.keys(dataBlob).forEach(key => {
  46. this.items +=
  47. dataBlob[key] &&
  48. (dataBlob[key].length || dataBlob[key].size || 0);
  49. });
  50. } catch (e) {
  51. this.items = 'unknown';
  52. }
  53. }
  54. return new ListViewDataSource(this._dataBlob);
  55. };
  56. return DataSource;
  57. })
  58. .mock('AnimatedImplementation', () => {
  59. const AnimatedImplementation = jest.requireActual('AnimatedImplementation');
  60. const oldCreate = AnimatedImplementation.createAnimatedComponent;
  61. AnimatedImplementation.createAnimatedComponent = function(
  62. Component,
  63. defaultProps,
  64. ) {
  65. const Wrapped = oldCreate(Component, defaultProps);
  66. Wrapped.__skipSetNativeProps_FOR_TESTS_ONLY = true;
  67. return Wrapped;
  68. };
  69. return AnimatedImplementation;
  70. })
  71. .mock('ReactNative', () => {
  72. const ReactNative = jest.requireActual('ReactNative');
  73. const NativeMethodsMixin =
  74. ReactNative.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
  75. .NativeMethodsMixin;
  76. Object.assign(NativeMethodsMixin, MockNativeMethods);
  77. Object.assign(ReactNative.NativeComponent.prototype, MockNativeMethods);
  78. return ReactNative;
  79. })
  80. .mock('ensureComponentIsNative', () => () => true);
  81. const mockNativeModules = {
  82. AlertManager: {
  83. alertWithArgs: jest.fn(),
  84. },
  85. AppState: {
  86. addEventListener: jest.fn(),
  87. removeEventListener: jest.fn(),
  88. },
  89. AsyncLocalStorage: {
  90. multiGet: jest.fn((keys, callback) =>
  91. process.nextTick(() => callback(null, [])),
  92. ),
  93. multiSet: jest.fn((entries, callback) =>
  94. process.nextTick(() => callback(null)),
  95. ),
  96. multiRemove: jest.fn((keys, callback) =>
  97. process.nextTick(() => callback(null)),
  98. ),
  99. multiMerge: jest.fn((entries, callback) =>
  100. process.nextTick(() => callback(null)),
  101. ),
  102. clear: jest.fn(callback => process.nextTick(() => callback(null))),
  103. getAllKeys: jest.fn(callback => process.nextTick(() => callback(null, []))),
  104. },
  105. BuildInfo: {
  106. appVersion: '0',
  107. buildVersion: '0',
  108. getConstants() {
  109. return {
  110. appVersion: '0',
  111. buildVersion: '0',
  112. };
  113. },
  114. },
  115. Clipboard: {
  116. setString: jest.fn(),
  117. },
  118. DataManager: {
  119. queryData: jest.fn(),
  120. },
  121. DeviceInfo: {
  122. Dimensions: {
  123. window: {
  124. fontScale: 2,
  125. height: 1334,
  126. scale: 2,
  127. width: 750,
  128. },
  129. screen: {
  130. fontScale: 2,
  131. height: 1334,
  132. scale: 2,
  133. width: 750,
  134. },
  135. },
  136. },
  137. FacebookSDK: {
  138. login: jest.fn(),
  139. logout: jest.fn(),
  140. queryGraphPath: jest.fn((path, method, params, callback) => callback()),
  141. },
  142. GraphPhotoUpload: {
  143. upload: jest.fn(),
  144. },
  145. I18n: {
  146. translationsDictionary: JSON.stringify({
  147. 'Good bye, {name}!|Bye message': '\u{00A1}Adi\u{00F3}s {name}!',
  148. }),
  149. },
  150. ImageLoader: {
  151. getSize: jest.fn(url => Promise.resolve({width: 320, height: 240})),
  152. prefetchImage: jest.fn(),
  153. },
  154. ImageViewManager: {
  155. getSize: jest.fn((uri, success) =>
  156. process.nextTick(() => success(320, 240)),
  157. ),
  158. prefetchImage: jest.fn(),
  159. },
  160. KeyboardObserver: {
  161. addListener: jest.fn(),
  162. removeListeners: jest.fn(),
  163. },
  164. Linking: {
  165. openURL: jest.fn(),
  166. canOpenURL: jest.fn(() => Promise.resolve(true)),
  167. addEventListener: jest.fn(),
  168. getInitialURL: jest.fn(() => Promise.resolve()),
  169. removeEventListener: jest.fn(),
  170. sendIntent: jest.fn(),
  171. },
  172. LocationObserver: {
  173. getCurrentPosition: jest.fn(),
  174. startObserving: jest.fn(),
  175. stopObserving: jest.fn(),
  176. },
  177. ModalFullscreenViewManager: {},
  178. NetInfo: {
  179. fetch: jest.fn(() => Promise.resolve()),
  180. getConnectionInfo: jest.fn(() => Promise.resolve()),
  181. addEventListener: jest.fn(),
  182. removeEventListener: jest.fn(),
  183. isConnected: {
  184. fetch: jest.fn(() => Promise.resolve()),
  185. addEventListener: jest.fn(),
  186. removeEventListener: jest.fn(),
  187. },
  188. isConnectionExpensive: jest.fn(() => Promise.resolve()),
  189. },
  190. Networking: {
  191. sendRequest: jest.fn(),
  192. abortRequest: jest.fn(),
  193. addListener: jest.fn(),
  194. removeListeners: jest.fn(),
  195. },
  196. PushNotificationManager: {
  197. presentLocalNotification: jest.fn(),
  198. scheduleLocalNotification: jest.fn(),
  199. cancelAllLocalNotifications: jest.fn(),
  200. removeAllDeliveredNotifications: jest.fn(),
  201. getDeliveredNotifications: jest.fn(callback => process.nextTick(() => [])),
  202. removeDeliveredNotifications: jest.fn(),
  203. setApplicationIconBadgeNumber: jest.fn(),
  204. getApplicationIconBadgeNumber: jest.fn(callback =>
  205. process.nextTick(() => callback(0)),
  206. ),
  207. cancelLocalNotifications: jest.fn(),
  208. getScheduledLocalNotifications: jest.fn(callback =>
  209. process.nextTick(() => callback()),
  210. ),
  211. requestPermissions: jest.fn(() =>
  212. Promise.resolve({alert: true, badge: true, sound: true}),
  213. ),
  214. abandonPermissions: jest.fn(),
  215. checkPermissions: jest.fn(callback =>
  216. process.nextTick(() => callback({alert: true, badge: true, sound: true})),
  217. ),
  218. getInitialNotification: jest.fn(() => Promise.resolve(null)),
  219. addListener: jest.fn(),
  220. removeListeners: jest.fn(),
  221. },
  222. SourceCode: {
  223. scriptURL: null,
  224. },
  225. StatusBarManager: {
  226. HEIGHT: 42,
  227. setColor: jest.fn(),
  228. setStyle: jest.fn(),
  229. setHidden: jest.fn(),
  230. setNetworkActivityIndicatorVisible: jest.fn(),
  231. setBackgroundColor: jest.fn(),
  232. setTranslucent: jest.fn(),
  233. },
  234. Timing: {
  235. createTimer: jest.fn(),
  236. deleteTimer: jest.fn(),
  237. },
  238. UIManager: {
  239. AndroidViewPager: {
  240. Commands: {
  241. setPage: jest.fn(),
  242. setPageWithoutAnimation: jest.fn(),
  243. },
  244. },
  245. blur: jest.fn(),
  246. createView: jest.fn(),
  247. dispatchViewManagerCommand: jest.fn(),
  248. focus: jest.fn(),
  249. getViewManagerConfig: jest.fn(),
  250. setChildren: jest.fn(),
  251. manageChildren: jest.fn(),
  252. updateView: jest.fn(),
  253. removeSubviewsFromContainerWithID: jest.fn(),
  254. replaceExistingNonRootView: jest.fn(),
  255. customBubblingEventTypes: {},
  256. customDirectEventTypes: {},
  257. AndroidTextInput: {
  258. Commands: {},
  259. },
  260. ModalFullscreenView: {
  261. Constants: {},
  262. },
  263. ScrollView: {
  264. Constants: {},
  265. },
  266. View: {
  267. Constants: {},
  268. },
  269. },
  270. BlobModule: {
  271. BLOB_URI_SCHEME: 'content',
  272. BLOB_URI_HOST: null,
  273. addNetworkingHandler: jest.fn(),
  274. enableBlobSupport: jest.fn(),
  275. disableBlobSupport: jest.fn(),
  276. createFromParts: jest.fn(),
  277. sendBlob: jest.fn(),
  278. release: jest.fn(),
  279. },
  280. WebSocketModule: {
  281. connect: jest.fn(),
  282. send: jest.fn(),
  283. sendBinary: jest.fn(),
  284. ping: jest.fn(),
  285. close: jest.fn(),
  286. addListener: jest.fn(),
  287. removeListeners: jest.fn(),
  288. },
  289. };
  290. Object.keys(mockNativeModules).forEach(module => {
  291. try {
  292. jest.doMock(module, () => mockNativeModules[module]); // needed by FacebookSDK-test
  293. } catch (e) {
  294. jest.doMock(module, () => mockNativeModules[module], {virtual: true});
  295. }
  296. });
  297. jest.doMock('NativeModules', () => mockNativeModules);
  298. jest.doMock('requireNativeComponent', () => {
  299. const React = require('react');
  300. return viewName =>
  301. class extends React.Component {
  302. render() {
  303. return React.createElement(viewName, this.props, this.props.children);
  304. }
  305. };
  306. });