Moving the jest configuration from jest-react-native to react native.

Reviewed By: cpojer

Differential Revision: D3923609

fbshipit-source-id: 62804df81b064871b499ae8c091e13dd1e0f229b
This commit is contained in:
Cristian Carlesso
2016-10-17 08:40:11 -07:00
committed by Facebook Github Bot
parent 3e332d9cc8
commit 6a462fb085
12 changed files with 230 additions and 127 deletions

24
jest/mockComponent.js Normal file
View File

@@ -0,0 +1,24 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
module.exports = moduleName => {
const RealComponent = require.requireActual(moduleName);
const React = require('react');
const Component = class extends RealComponent {
render() {
return React.createElement(
RealComponent.displayName || RealComponent.name,
this.props,
this.props.children,
);
}
};
return Component;
};

42
jest/preprocessor.js Normal file
View File

@@ -0,0 +1,42 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';
const babel = require('babel-core');
const babelRegisterOnly = require('../packager/babelRegisterOnly');
const createCacheKeyFunction = require('fbjs-scripts/jest/createCacheKeyFunction');
const path = require('path');
const transformer = require('../packager/transformer.js');
const nodeFiles = RegExp([
'/local-cli/',
'/packager/(?!react-packager/src/Resolver/polyfills/)',
].join('|'));
const nodeOptions = babelRegisterOnly.config([nodeFiles]);
module.exports = {
process(src, file) {
// Don't transform node_modules, except react-tools which includes the
// untransformed copy of React
if (file.match(/node_modules\/(?!react-tools\/)/)) {
return src;
} else if (nodeFiles.test(file)) { // node specific transforms only
return babel.transform(
src, Object.assign({filename: file}, nodeOptions)).code;
}
return transformer.transform(src, file, {inlineRequires: true}).code;
},
getCacheKey: createCacheKeyFunction([
__filename,
path.join(__dirname, '../packager/transformer.js'),
require.resolve('babel-core/package.json'),
]),
};

160
jest/setup.js Normal file
View File

@@ -0,0 +1,160 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';
const mockComponent = require.requireActual('./mockComponent');
require.requireActual('../packager/react-packager/src/Resolver/polyfills/babelHelpers.js');
require.requireActual('../packager/react-packager/src/Resolver/polyfills/Object.es7.js');
require.requireActual('../packager/react-packager/src/Resolver/polyfills/error-guard');
global.__DEV__ = true;
global.Promise = require.requireActual('promise');
global.regeneratorRuntime = require.requireActual('regenerator-runtime/runtime');
jest
.mock('npmlog');
// there's a __mock__ for it.
jest.setMock('ErrorUtils', require('ErrorUtils'));
jest
.mock('ReactNativeDefaultInjection')
.mock('Image', () => mockComponent('Image'))
.mock('Text', () => mockComponent('Text'))
.mock('TextInput', () => mockComponent('TextInput'))
.mock('Modal', () => mockComponent('Modal'))
.mock('View', () => mockComponent('View'))
.mock('ScrollView', () => mockComponent('ScrollView'))
.mock(
'ActivityIndicator',
() => mockComponent('ActivityIndicator'),
)
.mock('ListView', () => {
const RealListView = require.requireActual('ListView');
const ListView = mockComponent('ListView');
ListView.prototype.render = RealListView.prototype.render;
return ListView;
})
.mock('ListViewDataSource', () => {
const DataSource = require.requireActual('ListViewDataSource');
DataSource.prototype.toJSON = function() {
function ListViewDataSource(dataBlob) {
this.items = 0;
// Ensure this doesn't throw.
try {
Object.keys(dataBlob).forEach(key => {
this.items += dataBlob[key] && dataBlob[key].length;
});
} catch (e) {
this.items = 'unknown';
}
}
return new ListViewDataSource(this._dataBlob);
};
return DataSource;
})
.mock('ensureComponentIsNative', () => () => true);
const mockEmptyObject = {};
const mockNativeModules = {
AlertManager: {
alertWithArgs: jest.fn(),
},
AsyncLocalStorage: {
clear: jest.fn(),
getItem: jest.fn(),
removeItem: jest.fn(),
setItem: jest.fn(),
},
BuildInfo: {
appVersion: '0',
buildVersion: '0',
},
Clipboard: {
setString: jest.fn(),
},
DataManager: {
queryData: jest.fn(),
},
FacebookSDK: {
login: jest.fn(),
logout: jest.fn(),
queryGraphPath: jest.fn((path, method, params, callback) => callback()),
},
FbRelayNativeAdapter: {
updateCLC: jest.fn(),
},
GraphPhotoUpload: {
upload: jest.fn(),
},
I18n: {
translationsDictionary: JSON.stringify({
'Good bye, {name}!|Bye message': '\u{00A1}Adi\u{00F3}s {name}!',
}),
},
ImageLoader: {
getSize: jest.fn(
(uri, success) => process.nextTick(() => success(320, 240))
),
prefetchImage: jest.fn(),
},
ImageViewManager: {
getSize: jest.fn(
(uri, success) => process.nextTick(() => success(320, 240))
),
prefetchImage: jest.fn(),
},
ModalFullscreenViewManager: {},
SourceCode: {
scriptURL: null,
},
Timing: {
createTimer: jest.fn(),
deleteTimer: jest.fn(),
},
UIManager: {
customBubblingEventTypes: {},
customDirectEventTypes: {},
Dimensions: {
window: {
fontScale: 2,
height: 1334,
scale: 2,
width: 750,
},
},
RCTModalFullscreenView: {
Constants: {},
},
RCTScrollView: {
Constants: {},
},
RCTView: {
Constants: {},
},
},
};
Object.keys(mockNativeModules).forEach(module => {
try {
jest.doMock(module, () => mockNativeModules[module]); // needed by FacebookSDK-test
} catch (e) {
jest.doMock(module, () => mockNativeModules[module], {virtual: true});
}
});
jest
.doMock('NativeModules', () => mockNativeModules)
.doMock('ReactNativePropRegistry', () => ({
register: id => id,
getByID: () => mockEmptyObject,
}));