[ReactNative] Fix dimensions in jest tests

This commit is contained in:
Spencer Ahrens 2015-04-24 14:43:14 -07:00
parent 46db1826c5
commit a2dd7fc69f
4 changed files with 45 additions and 1 deletions

View File

@ -29,6 +29,7 @@ var NativeModules = {
UIManager: { UIManager: {
customBubblingEventTypes: {}, customBubblingEventTypes: {},
customDirectEventTypes: {}, customDirectEventTypes: {},
Dimensions: {},
}, },
AsyncLocalStorage: { AsyncLocalStorage: {
getItem: jest.genMockFunction(), getItem: jest.genMockFunction(),

View File

@ -20,7 +20,7 @@ var dimensions = NativeModules.UIManager.Dimensions;
// We calculate the window dimensions in JS so that we don't encounter loss of // We calculate the window dimensions in JS so that we don't encounter loss of
// precision in transferring the dimensions (which could be non-integers) over // precision in transferring the dimensions (which could be non-integers) over
// the bridge. // the bridge.
if (dimensions.windowPhysicalPixels) { if (dimensions && dimensions.windowPhysicalPixels) {
// parse/stringify => Clone hack // parse/stringify => Clone hack
dimensions = JSON.parse(JSON.stringify(dimensions)); dimensions = JSON.parse(JSON.stringify(dimensions));

View File

@ -0,0 +1,23 @@
// This mock only provides short-circuited methods of applyWithGuard and guard.
// A lot of modules rely on these two functions. This mock relieves their tests
// from depending on the real ErrorUtils module. If you need real error handling
// don't use this mock.
'use strict';
function execute(fun, context, args) {
return fun.apply(context, args);
};
function reportError(error) {
throw error;
}
var ErrorUtils = {
apply: jest.genMockFunction().mockImplementation(execute),
applyWithGuard: jest.genMockFunction().mockImplementation(execute),
inGuard: jest.genMockFunction().mockReturnValue(true),
reportError: jest.genMockFunction().mockImplementation(reportError),
setGlobalHandler: jest.genMockFunction(),
};
module.exports = ErrorUtils;

View File

@ -0,0 +1,20 @@
/**
* Copyright 2004-present Facebook. All Rights Reserved.
*/
'use strict';
var PixelRatio = {
startDetecting: function () {
// noop for our implementation
},
get: function() {
return 2;
},
getPixelSizeForLayoutSize: function (layoutSize) {
return Math.round(layoutSize * PixelRatio.get());
}
};
module.exports = PixelRatio;