Add unit tests to flattenStyle

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
This commit is contained in:
James Ide 2015-10-03 10:12:51 -07:00 committed by facebook-github-bot-9
parent 22d507a680
commit c649211147
1 changed files with 52 additions and 0 deletions

View File

@ -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);
});
});