From d8e8ea5cbbfae10f1eb0ccfaeb7e8b0668020083 Mon Sep 17 00:00:00 2001 From: Sebastian Markbage Date: Thu, 24 Mar 2016 18:58:47 -0700 Subject: [PATCH] Add test and mocks Mock UIManager Comment out dontMock that actually needs mocking --- .../InitializeJavaScriptAppEngine.js | 14 +++++ .../__mocks__/InteractionManager.js | 16 +++++ .../__mocks__/JSTimersExecution.js | 14 +++++ .../native/ReactNative/__mocks__/Platform.js | 16 +++++ .../ReactNative/__mocks__/RCTEventEmitter.js | 14 +++++ .../native/ReactNative/__mocks__/RCTLog.js | 14 +++++ .../ReactNative/__mocks__/TextInputState.js | 17 +++++ .../native/ReactNative/__mocks__/UIManager.js | 21 +++++++ .../ReactNative/__mocks__/deepDiffer.js | 63 +++++++++++++++++++ .../deepFreezeAndThrowOnMutationInDev.js | 16 +++++ .../ReactNative/__mocks__/flattenStyle.js | 16 +++++ .../native/ReactNative/__mocks__/merge.js | 18 ++++++ .../ReactNativeAttributePayload-test.js | 4 +- .../__tests__/ReactNativeMount-test.js | 34 ++++++++++ 14 files changed, 275 insertions(+), 2 deletions(-) create mode 100644 src/renderers/native/ReactNative/__mocks__/InitializeJavaScriptAppEngine.js create mode 100644 src/renderers/native/ReactNative/__mocks__/InteractionManager.js create mode 100644 src/renderers/native/ReactNative/__mocks__/JSTimersExecution.js create mode 100644 src/renderers/native/ReactNative/__mocks__/Platform.js create mode 100644 src/renderers/native/ReactNative/__mocks__/RCTEventEmitter.js create mode 100644 src/renderers/native/ReactNative/__mocks__/RCTLog.js create mode 100644 src/renderers/native/ReactNative/__mocks__/TextInputState.js create mode 100644 src/renderers/native/ReactNative/__mocks__/UIManager.js create mode 100644 src/renderers/native/ReactNative/__mocks__/deepDiffer.js create mode 100644 src/renderers/native/ReactNative/__mocks__/deepFreezeAndThrowOnMutationInDev.js create mode 100644 src/renderers/native/ReactNative/__mocks__/flattenStyle.js create mode 100644 src/renderers/native/ReactNative/__mocks__/merge.js create mode 100644 src/renderers/native/ReactNative/__tests__/ReactNativeMount-test.js diff --git a/src/renderers/native/ReactNative/__mocks__/InitializeJavaScriptAppEngine.js b/src/renderers/native/ReactNative/__mocks__/InitializeJavaScriptAppEngine.js new file mode 100644 index 0000000000..3f4b4fdd0f --- /dev/null +++ b/src/renderers/native/ReactNative/__mocks__/InitializeJavaScriptAppEngine.js @@ -0,0 +1,14 @@ +/** + * Copyright 2013-2015, 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'; + +// Noop + +// TODO: Move all initialization callers back into react-native diff --git a/src/renderers/native/ReactNative/__mocks__/InteractionManager.js b/src/renderers/native/ReactNative/__mocks__/InteractionManager.js new file mode 100644 index 0000000000..de72e164a6 --- /dev/null +++ b/src/renderers/native/ReactNative/__mocks__/InteractionManager.js @@ -0,0 +1,16 @@ +/** + * Copyright 2013-2015, 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'; + +// TODO: Figure out a way to drop this dependency + +var InteractionManager = {}; + +module.exports = InteractionManager; diff --git a/src/renderers/native/ReactNative/__mocks__/JSTimersExecution.js b/src/renderers/native/ReactNative/__mocks__/JSTimersExecution.js new file mode 100644 index 0000000000..3f4b4fdd0f --- /dev/null +++ b/src/renderers/native/ReactNative/__mocks__/JSTimersExecution.js @@ -0,0 +1,14 @@ +/** + * Copyright 2013-2015, 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'; + +// Noop + +// TODO: Move all initialization callers back into react-native diff --git a/src/renderers/native/ReactNative/__mocks__/Platform.js b/src/renderers/native/ReactNative/__mocks__/Platform.js new file mode 100644 index 0000000000..fc04c24549 --- /dev/null +++ b/src/renderers/native/ReactNative/__mocks__/Platform.js @@ -0,0 +1,16 @@ +/** + * Copyright 2013-2015, 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'; + +// Mock of the Native Hooks + +var Platform = {}; + +module.exports = Platform; diff --git a/src/renderers/native/ReactNative/__mocks__/RCTEventEmitter.js b/src/renderers/native/ReactNative/__mocks__/RCTEventEmitter.js new file mode 100644 index 0000000000..3f4b4fdd0f --- /dev/null +++ b/src/renderers/native/ReactNative/__mocks__/RCTEventEmitter.js @@ -0,0 +1,14 @@ +/** + * Copyright 2013-2015, 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'; + +// Noop + +// TODO: Move all initialization callers back into react-native diff --git a/src/renderers/native/ReactNative/__mocks__/RCTLog.js b/src/renderers/native/ReactNative/__mocks__/RCTLog.js new file mode 100644 index 0000000000..3f4b4fdd0f --- /dev/null +++ b/src/renderers/native/ReactNative/__mocks__/RCTLog.js @@ -0,0 +1,14 @@ +/** + * Copyright 2013-2015, 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'; + +// Noop + +// TODO: Move all initialization callers back into react-native diff --git a/src/renderers/native/ReactNative/__mocks__/TextInputState.js b/src/renderers/native/ReactNative/__mocks__/TextInputState.js new file mode 100644 index 0000000000..a0b4e576d7 --- /dev/null +++ b/src/renderers/native/ReactNative/__mocks__/TextInputState.js @@ -0,0 +1,17 @@ +/** + * Copyright 2013-2015, 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'; + +// Mock of the Native Hooks +// TODO: Should this move into the components themselves? E.g. focusable + +var TextInputState = {}; + +module.exports = TextInputState; diff --git a/src/renderers/native/ReactNative/__mocks__/UIManager.js b/src/renderers/native/ReactNative/__mocks__/UIManager.js new file mode 100644 index 0000000000..0c51ab6e6b --- /dev/null +++ b/src/renderers/native/ReactNative/__mocks__/UIManager.js @@ -0,0 +1,21 @@ +/** + * Copyright 2013-2015, 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'; + +// Mock of the Native Hooks + +var RCTUIManager = { + createView: jest.genMockFunction(), + setChildren: jest.genMockFunction(), + manageChildren: jest.genMockFunction(), + updateView: jest.genMockFunction(), +}; + +module.exports = RCTUIManager; diff --git a/src/renderers/native/ReactNative/__mocks__/deepDiffer.js b/src/renderers/native/ReactNative/__mocks__/deepDiffer.js new file mode 100644 index 0000000000..cc58e4b328 --- /dev/null +++ b/src/renderers/native/ReactNative/__mocks__/deepDiffer.js @@ -0,0 +1,63 @@ +/** + * Copyright 2013-2015, 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'; + +// TODO: Move deepDiffer into react + +var deepDiffer = function(one: any, two: any): bool { + if (one === two) { + // Short circuit on identical object references instead of traversing them. + return false; + } + if ((typeof one === 'function') && (typeof two === 'function')) { + // We consider all functions equal + return false; + } + if ((typeof one !== 'object') || (one === null)) { + // Primitives can be directly compared + return one !== two; + } + if ((typeof two !== 'object') || (two === null)) { + // We know they are different because the previous case would have triggered + // otherwise. + return true; + } + if (one.constructor !== two.constructor) { + return true; + } + if (Array.isArray(one)) { + // We know two is also an array because the constructors are equal + var len = one.length; + if (two.length !== len) { + return true; + } + for (var ii = 0; ii < len; ii++) { + if (deepDiffer(one[ii], two[ii])) { + return true; + } + } + } else { + for (var key in one) { + if (deepDiffer(one[key], two[key])) { + return true; + } + } + for (var twoKey in two) { + // The only case we haven't checked yet is keys that are in two but aren't + // in one, which means they are different. + if (one[twoKey] === undefined && two[twoKey] !== undefined) { + return true; + } + } + } + return false; +}; + +module.exports = deepDiffer; diff --git a/src/renderers/native/ReactNative/__mocks__/deepFreezeAndThrowOnMutationInDev.js b/src/renderers/native/ReactNative/__mocks__/deepFreezeAndThrowOnMutationInDev.js new file mode 100644 index 0000000000..ebb81c3335 --- /dev/null +++ b/src/renderers/native/ReactNative/__mocks__/deepFreezeAndThrowOnMutationInDev.js @@ -0,0 +1,16 @@ +/** + * Copyright 2013-2015, 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'; + +// TODO: move into react or fbjs + +var deepFreezeAndThrowOnMutationInDev = function() { }; + +module.exports = deepFreezeAndThrowOnMutationInDev; diff --git a/src/renderers/native/ReactNative/__mocks__/flattenStyle.js b/src/renderers/native/ReactNative/__mocks__/flattenStyle.js new file mode 100644 index 0000000000..a766c705fb --- /dev/null +++ b/src/renderers/native/ReactNative/__mocks__/flattenStyle.js @@ -0,0 +1,16 @@ +/** + * Copyright 2013-2015, 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'; + +// TODO: Move flattenStyle into react + +var flattenStyle = function() { }; + +module.exports = flattenStyle; diff --git a/src/renderers/native/ReactNative/__mocks__/merge.js b/src/renderers/native/ReactNative/__mocks__/merge.js new file mode 100644 index 0000000000..0c2dc8bf5d --- /dev/null +++ b/src/renderers/native/ReactNative/__mocks__/merge.js @@ -0,0 +1,18 @@ +/** + * Copyright 2013-2015, 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'; + +// TODO: Replace all callers with spread + +var merge = function(a, b) { + return {...a, ...b}; +}; + +module.exports = merge; diff --git a/src/renderers/native/ReactNative/__tests__/ReactNativeAttributePayload-test.js b/src/renderers/native/ReactNative/__tests__/ReactNativeAttributePayload-test.js index 6cbaa46a30..d325ca0919 100644 --- a/src/renderers/native/ReactNative/__tests__/ReactNativeAttributePayload-test.js +++ b/src/renderers/native/ReactNative/__tests__/ReactNativeAttributePayload-test.js @@ -11,8 +11,8 @@ jest.dontMock('ReactNativeAttributePayload'); jest.dontMock('ReactNativePropRegistry'); -jest.dontMock('deepDiffer'); -jest.dontMock('flattenStyle'); +// jest.dontMock('deepDiffer'); +// jest.dontMock('flattenStyle'); var ReactNativeAttributePayload = require('ReactNativeAttributePayload'); var ReactNativePropRegistry = require('ReactNativePropRegistry'); diff --git a/src/renderers/native/ReactNative/__tests__/ReactNativeMount-test.js b/src/renderers/native/ReactNative/__tests__/ReactNativeMount-test.js new file mode 100644 index 0000000000..b35ff4068e --- /dev/null +++ b/src/renderers/native/ReactNative/__tests__/ReactNativeMount-test.js @@ -0,0 +1,34 @@ +/** + * Copyright 2013-2015, 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. + * + * @emails react-core + */ + +'use strict'; + +var React; +var ReactNative; +var createReactNativeComponentClass; + +describe('ReactNative', function() { + beforeEach(function() { + React = require('React'); + ReactNative = require('ReactNative'); + createReactNativeComponentClass = require('createReactNativeComponentClass'); + }); + + it('should be able to create and render a native component', function() { + var View = createReactNativeComponentClass({ + validAttributes: { foo: true }, + uiViewClassName: 'View', + }); + + ReactNative.render(, 1); + }); + +});