Skip to content

Commit 8ef89f1

Browse files
dmeehan1968mathieudutour
authored andcommitted
Fix View, Text and Artboard style PropTypes to accept ordinals from StyleSheet object (#243)
* View.style prop accepts numbers * Text.style prop accepts numbers * Artboard.style prop accepts numbers * Test View.style prop validation * Test Text.style prop validation * Test Artboard.style prop validation
1 parent e160b24 commit 8ef89f1

File tree

9 files changed

+223
-9
lines changed

9 files changed

+223
-9
lines changed

__tests__/components/Artboard.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
import React from 'react';
22
import renderer from 'react-test-renderer';
33
import Artboard from '../../src/components/Artboard';
4+
import StyleSheet from '../../src/stylesheet';
45

56
describe('<Artboard />', () => {
67
it('renders children', () => {
7-
const tree = renderer.create(<Artboard><foo><bar /></foo></Artboard>).toJSON();
8+
const tree = renderer
9+
.create(
10+
<Artboard>
11+
<foo>
12+
<bar />
13+
</foo>
14+
</Artboard>
15+
)
16+
.toJSON();
817

918
expect(tree).toMatchSnapshot();
1019
});
@@ -24,4 +33,32 @@ describe('<Artboard />', () => {
2433
expect(tree).toMatchSnapshot();
2534
});
2635
});
36+
37+
describe('style', () => {
38+
const styles = StyleSheet.create({
39+
view: {
40+
flex: 1,
41+
},
42+
});
43+
44+
it('accepts a plain object', () => {
45+
const tree = renderer.create(<Artboard style={{ flex: 1 }} />).toJSON();
46+
47+
expect(tree).toMatchSnapshot();
48+
});
49+
50+
it('accepts a StyleSheet ordinal', () => {
51+
const tree = renderer.create(<Artboard style={styles.view} />).toJSON();
52+
53+
expect(tree).toMatchSnapshot();
54+
});
55+
56+
it('accepts an array of plain objects and/or StyleSheet ordinals', () => {
57+
const tree = renderer
58+
.create(<Artboard style={[{ flexGrow: 1 }, styles.view]} />)
59+
.toJSON();
60+
61+
expect(tree).toMatchSnapshot();
62+
});
63+
});
2764
});

__tests__/components/Text.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import renderer from 'react-test-renderer';
33
import Text from '../../src/components/Text';
4+
import StyleSheet from '../../src/stylesheet';
45

56
describe('<Text />', () => {
67
it('passes its children', () => {
@@ -22,4 +23,32 @@ describe('<Text />', () => {
2223
expect(tree).toMatchSnapshot();
2324
});
2425
});
26+
27+
describe('style', () => {
28+
const styles = StyleSheet.create({
29+
view: {
30+
flex: 1,
31+
},
32+
});
33+
34+
it('accepts a plain object', () => {
35+
const tree = renderer.create(<Text style={{ flex: 1 }} />).toJSON();
36+
37+
expect(tree).toMatchSnapshot();
38+
});
39+
40+
it('accepts a StyleSheet ordinal', () => {
41+
const tree = renderer.create(<Text style={styles.view} />).toJSON();
42+
43+
expect(tree).toMatchSnapshot();
44+
});
45+
46+
it('accepts an array of plain objects and/or StyleSheet ordinals', () => {
47+
const tree = renderer
48+
.create(<Text style={[{ flexGrow: 1 }, styles.view]} />)
49+
.toJSON();
50+
51+
expect(tree).toMatchSnapshot();
52+
});
53+
});
2554
});

__tests__/components/View.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import renderer from 'react-test-renderer';
33
import View from '../../src/components/View';
4+
import StyleSheet from '../../src/stylesheet';
45

56
describe('<View />', () => {
67
it('passes its children', () => {
@@ -22,4 +23,32 @@ describe('<View />', () => {
2223
expect(tree).toMatchSnapshot();
2324
});
2425
});
26+
27+
describe('style', () => {
28+
const styles = StyleSheet.create({
29+
view: {
30+
flex: 1,
31+
},
32+
});
33+
34+
it('accepts a plain object', () => {
35+
const tree = renderer.create(<View style={{ flex: 1 }} />).toJSON();
36+
37+
expect(tree).toMatchSnapshot();
38+
});
39+
40+
it('accepts a StyleSheet ordinal', () => {
41+
const tree = renderer.create(<View style={styles.view} />).toJSON();
42+
43+
expect(tree).toMatchSnapshot();
44+
});
45+
46+
it('accepts an array of plain objects and/or StyleSheet ordinals', () => {
47+
const tree = renderer
48+
.create(<View style={[{ flexGrow: 1 }, styles.view]} />)
49+
.toJSON();
50+
51+
expect(tree).toMatchSnapshot();
52+
});
53+
});
2554
});

__tests__/components/__snapshots__/Artboard.js.snap

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,34 @@ exports[`<Artboard /> renders children 1`] = `
1919
</foo>
2020
</artboard>
2121
`;
22+
23+
exports[`<Artboard /> style accepts a StyleSheet ordinal 1`] = `
24+
<artboard
25+
name="Artboard"
26+
style={
27+
Object {
28+
"flex": 1,
29+
}
30+
} />
31+
`;
32+
33+
exports[`<Artboard /> style accepts a plain object 1`] = `
34+
<artboard
35+
name="Artboard"
36+
style={
37+
Object {
38+
"flex": 1,
39+
}
40+
} />
41+
`;
42+
43+
exports[`<Artboard /> style accepts an array of plain objects and/or StyleSheet ordinals 1`] = `
44+
<artboard
45+
name="Artboard"
46+
style={
47+
Object {
48+
"flex": 1,
49+
"flexGrow": 1,
50+
}
51+
} />
52+
`;

__tests__/components/__snapshots__/Text.js.snap

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,37 @@ exports[`<Text /> passes its children 1`] = `
2020
foo
2121
</text>
2222
`;
23+
24+
exports[`<Text /> style accepts a StyleSheet ordinal 1`] = `
25+
<text
26+
name="Text"
27+
resizingConstraint={undefined}
28+
style={
29+
Object {
30+
"flex": 1,
31+
}
32+
} />
33+
`;
34+
35+
exports[`<Text /> style accepts a plain object 1`] = `
36+
<text
37+
name="Text"
38+
resizingConstraint={undefined}
39+
style={
40+
Object {
41+
"flex": 1,
42+
}
43+
} />
44+
`;
45+
46+
exports[`<Text /> style accepts an array of plain objects and/or StyleSheet ordinals 1`] = `
47+
<text
48+
name="Text"
49+
resizingConstraint={undefined}
50+
style={
51+
Object {
52+
"flex": 1,
53+
"flexGrow": 1,
54+
}
55+
} />
56+
`;

__tests__/components/__snapshots__/View.js.snap

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,37 @@ exports[`<View /> passes its children 1`] = `
2020
foo
2121
</view>
2222
`;
23+
24+
exports[`<View /> style accepts a StyleSheet ordinal 1`] = `
25+
<view
26+
name="View"
27+
resizingConstraint={undefined}
28+
style={
29+
Object {
30+
"flex": 1,
31+
}
32+
} />
33+
`;
34+
35+
exports[`<View /> style accepts a plain object 1`] = `
36+
<view
37+
name="View"
38+
resizingConstraint={undefined}
39+
style={
40+
Object {
41+
"flex": 1,
42+
}
43+
} />
44+
`;
45+
46+
exports[`<View /> style accepts an array of plain objects and/or StyleSheet ordinals 1`] = `
47+
<view
48+
name="View"
49+
resizingConstraint={undefined}
50+
style={
51+
Object {
52+
"flex": 1,
53+
"flexGrow": 1,
54+
}
55+
} />
56+
`;

src/components/Artboard.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,16 @@ import ViewStylePropTypes from './ViewStylePropTypes';
66

77
const propTypes = {
88
// TODO(lmr): do some nice warning stuff like RN does
9-
style: PropTypes.shape({
10-
...ViewStylePropTypes,
11-
}),
9+
style: PropTypes.oneOfType([
10+
PropTypes.shape({ ...ViewStylePropTypes }),
11+
PropTypes.arrayOf(
12+
PropTypes.oneOfType([
13+
PropTypes.shape({ ...ViewStylePropTypes }),
14+
PropTypes.number,
15+
])
16+
),
17+
PropTypes.number,
18+
]),
1219
name: PropTypes.string,
1320
children: PropTypes.node,
1421
};

src/components/Text.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,17 @@ import ResizingConstraintPropTypes from './ResizingConstraintPropTypes';
88

99
const propTypes = {
1010
// TODO(lmr): do some nice warning stuff like RN does
11-
style: PropTypes.shape({
12-
...ViewStylePropTypes,
13-
...TextStylePropTypes,
14-
}),
11+
style: PropTypes.oneOfType([
12+
PropTypes.shape({ ...ViewStylePropTypes, ...TextStylePropTypes }),
13+
PropTypes.arrayOf(
14+
PropTypes.oneOfType([
15+
PropTypes.shape({ ...ViewStylePropTypes, ...TextStylePropTypes }),
16+
PropTypes.number,
17+
])
18+
),
19+
PropTypes.number,
20+
]),
21+
1522
name: PropTypes.string,
1623
resizingConstraint: PropTypes.shape({
1724
...ResizingConstraintPropTypes,

src/components/View.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ const propTypes = {
99
// TODO(lmr): do some nice warning stuff like RN does
1010
style: PropTypes.oneOfType([
1111
PropTypes.shape({ ...ViewStylePropTypes }),
12-
PropTypes.arrayOf(PropTypes.shape({ ...ViewStylePropTypes })),
12+
PropTypes.arrayOf(
13+
PropTypes.oneOfType([
14+
PropTypes.shape({ ...ViewStylePropTypes }),
15+
PropTypes.number,
16+
])
17+
),
18+
PropTypes.number,
1319
]),
1420
name: PropTypes.string,
1521
resizingConstraint: PropTypes.shape({

0 commit comments

Comments
 (0)