Skip to content

Commit dc85c6b

Browse files
author
Josh Habdas
committed
Segments only allow Segment children
1 parent 38885cc commit dc85c6b

3 files changed

Lines changed: 28 additions & 3 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"eslint-plugin-mocha": "^1.0.0",
4747
"eslint-plugin-react": "^3.5.1",
4848
"faker": "^3.0.1",
49+
"fbjs": "^0.4.0",
4950
"gulp": "^3.9.0",
5051
"gulp-help": "^1.6.1",
5152
"gulp-html-replace": "^1.5.4",

src/elements/Segment/Segments.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
1-
import React, {Component, PropTypes} from 'react';
1+
import React, {Children, Component, PropTypes} from 'react';
2+
import _ from 'lodash';
3+
import invariant from 'fbjs/lib/invariant';
24
import classNames from 'classnames';
35
import META from 'src/utils/Meta';
6+
import Segment from './Segment';
47

58
export default class Segments extends Component {
69
static propTypes = {
7-
children: PropTypes.node.isRequired,
10+
children: PropTypes.node,
811
className: PropTypes.string,
912
};
1013

14+
componentDidMount() {
15+
const {children} = this.props;
16+
17+
invariant(
18+
!_.any( Children.map(children, child => child.type !== Segment) ),
19+
'May only contain children of type Segment.',
20+
);
21+
}
22+
1123
static _meta = {
1224
library: META.library.semanticUI,
1325
name: 'Segments',

test/specs/elements/Segment/Segments-test.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,24 @@ describe('Segments', () => {
2222
const [component] = render(
2323
<Segments>
2424
<Segment>Top</Segment>
25+
<Segment>Middle</Segment>
2526
<Segment>Bottom</Segment>
2627
</Segments>
2728
).scryClass('sd-segments');
2829

2930
expect(
3031
component.querySelectorAll('.sd-segment').length
31-
).to.equal(2);
32+
).to.equal(3);
33+
});
34+
35+
it('only allows children of type Segment', () => {
36+
expect(global.shallowRender(
37+
<Segments>Some text</Segments>
38+
)).to.throw;
39+
40+
// FIXME: Figure out why this is giving false positives
41+
expect(global.shallowRender(
42+
<Segments>Some text</Segments>
43+
)).to.not.throw;
3244
});
3345
});

0 commit comments

Comments
 (0)