Files
react-native/Libraries/StyleSheet/__tests__/flattenStyle-test.js
James Ide 0ee5f68929 Migrate "Libraries" from Haste to standard path-based requires (sans vendor & renderers) (#24749)
Summary:
This is the next step in moving RN towards standard path-based requires. All the requires in `Libraries` have been rewritten to use relative requires with a few exceptions, namely, `vendor` and `Renderer/oss` since those need to be changed upstream. This commit uses relative requires instead of `react-native/...` so that if Facebook were to stop syncing out certain folders and therefore remove code from the react-native package, internal code at Facebook would not need to change.

See the umbrella issue at https://github.com/facebook/react-native/issues/24316 for more detail.

[General] [Changed] - Migrate "Libraries" from Haste to standard path-based requires
Pull Request resolved: https://github.com/facebook/react-native/pull/24749

Differential Revision: D15258017

Pulled By: cpojer

fbshipit-source-id: a1f480ea36c05c659b6f37c8f02f6f9216d5a323
2019-05-08 08:48:59 -07:00

159 lines
4.6 KiB
JavaScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @emails oncall+react_native
*/
'use strict';
const StyleSheet = require('../StyleSheet');
const StyleSheetValidation = require('../StyleSheetValidation');
const flattenStyle = require('../flattenStyle');
function getFixture() {
StyleSheetValidation.addValidStylePropTypes({
styleA: () => {},
styleB: () => {},
});
return StyleSheet.create({
elementA: {
styleA: 'moduleA/elementA/styleA',
styleB: 'moduleA/elementA/styleB',
},
elementB: {
styleB: 'moduleA/elementB/styleB',
},
});
}
describe('flattenStyle', () => {
it('should merge style objects', () => {
const style1 = {width: 10};
const style2 = {height: 20};
const flatStyle = flattenStyle([style1, style2]);
expect(flatStyle.width).toBe(10);
expect(flatStyle.height).toBe(20);
});
it('should override style properties', () => {
const style1 = {backgroundColor: '#000', width: 10};
const style2 = {backgroundColor: '#023c69', width: null};
const flatStyle = flattenStyle([style1, style2]);
expect(flatStyle.backgroundColor).toBe('#023c69');
expect(flatStyle.width).toBe(null);
});
it('should overwrite properties with `undefined`', () => {
const style1 = {backgroundColor: '#000'};
const style2 = {backgroundColor: undefined};
const 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', () => {
const style1 = {width: 10};
const style2 = {height: 20};
const style3 = {width: 30};
const flatStyle = flattenStyle([null, [], [style1, style2], style3]);
expect(flatStyle.width).toBe(30);
expect(flatStyle.height).toBe(20);
});
it('should not allocate an object when there is no style', () => {
const nullStyle = flattenStyle(null);
const nullStyleAgain = flattenStyle(null);
expect(nullStyle).toBe(nullStyleAgain);
expect(nullStyle).toBe(undefined);
});
it('should not allocate an object when there is a style', () => {
const style = {a: 'b'};
const nullStyle = flattenStyle(style);
expect(nullStyle).toBe(style);
});
it('should not allocate an object when there is a single class', () => {
const fixture = getFixture();
const singleStyle = flattenStyle(fixture.elementA);
const singleStyleAgain = flattenStyle(fixture.elementA);
expect(singleStyle).toBe(singleStyleAgain);
expect(singleStyle).toEqual({
styleA: 'moduleA/elementA/styleA',
styleB: 'moduleA/elementA/styleB',
});
});
it('should merge single class and style properly', () => {
const fixture = getFixture();
const style = {styleA: 'overrideA', styleC: 'overrideC'};
const arrayStyle = flattenStyle([fixture.elementA, style]);
expect(arrayStyle).toEqual({
styleA: 'overrideA',
styleB: 'moduleA/elementA/styleB',
styleC: 'overrideC',
});
});
it('should merge multiple classes', () => {
const fixture = getFixture();
const AthenB = flattenStyle([fixture.elementA, fixture.elementB]);
const BthenA = flattenStyle([fixture.elementB, fixture.elementA]);
expect(AthenB).toEqual({
styleA: 'moduleA/elementA/styleA',
styleB: 'moduleA/elementB/styleB',
});
expect(BthenA).toEqual({
styleA: 'moduleA/elementA/styleA',
styleB: 'moduleA/elementA/styleB',
});
});
it('should merge multiple classes with style', () => {
const fixture = getFixture();
const style = {styleA: 'overrideA'};
const AthenB = flattenStyle([fixture.elementA, fixture.elementB, style]);
const BthenA = flattenStyle([fixture.elementB, fixture.elementA, style]);
expect(AthenB).toEqual({
styleA: 'overrideA',
styleB: 'moduleA/elementB/styleB',
});
expect(BthenA).toEqual({
styleA: 'overrideA',
styleB: 'moduleA/elementA/styleB',
});
});
it('should flatten recursively', () => {
const fixture = getFixture();
const style = [{styleA: 'newA', styleB: 'newB'}, {styleA: 'newA2'}];
const AthenB = flattenStyle([fixture.elementA, fixture.elementB, style]);
expect(AthenB).toEqual({
styleA: 'newA2',
styleB: 'newB',
});
});
it('should ignore invalid class names', () => {
const invalid = flattenStyle(1234, null);
expect(invalid).toEqual(undefined);
// Invalid class name 1234 skipping ...
});
});