From c6492111476859778248cabe707da804e32275dc Mon Sep 17 00:00:00 2001 From: James Ide Date: Sat, 3 Oct 2015 10:12:51 -0700 Subject: [PATCH] Add unit tests to flattenStyle MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: The StyleSheet merging algorithm was modeled after `Object.assign` and the native spread operator. This diff converts `flattenStyle` to actually use `Object.assign`. Closes https://github.com/facebook/react-native/pull/3048 Reviewed By: @​svcscm Differential Revision: D2506387 Pulled By: @vjeux --- .../StyleSheet/__tests__/flattenStyle-test.js | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Libraries/StyleSheet/__tests__/flattenStyle-test.js diff --git a/Libraries/StyleSheet/__tests__/flattenStyle-test.js b/Libraries/StyleSheet/__tests__/flattenStyle-test.js new file mode 100644 index 000000000..80bac695c --- /dev/null +++ b/Libraries/StyleSheet/__tests__/flattenStyle-test.js @@ -0,0 +1,52 @@ +/** + * 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'; + +jest.autoMockOff(); + +var flattenStyle = require('flattenStyle'); + +describe('flattenStyle', () => { + + it('should merge style objects', () => { + var style1 = {width: 10}; + var style2 = {height: 20}; + var flatStyle = flattenStyle([style1, style2]); + expect(flatStyle.width).toBe(10); + expect(flatStyle.height).toBe(20); + }); + + it('should override style properties', () => { + var style1 = {backgroundColor: '#000', width: 10}; + var style2 = {backgroundColor: '#023c69', width: null}; + var flatStyle = flattenStyle([style1, style2]); + expect(flatStyle.backgroundColor).toBe('#023c69'); + expect(flatStyle.width).toBe(null); + }); + + it('should overwrite properties with `undefined`', () => { + var style1 = {backgroundColor: '#000'}; + var style2 = {backgroundColor: undefined}; + var flatStyle = flattenStyle([style1, style2]); + expect(flatStyle.backgroundColor).toBe(undefined); + }); + + it('should not fail on falsy values', () => { + expect(() => flattenStyle([null, false, undefined])).not.toThrow(); + }); + + it('should recursively flatten arrays', () => { + var style1 = {width: 10}; + var style2 = {height: 20}; + var style3 = {width: 30}; + var flatStyle = flattenStyle([null, [], [style1, style2], style3]); + expect(flatStyle.width).toBe(30); + expect(flatStyle.height).toBe(20); + }); +});