Skip to content

Commit 0613cf7

Browse files
committed
Make chips avatar detection work when decorated
1 parent a8e1bb7 commit 0613cf7

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

components/chip/Chip.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ const factory = (Avatar) => {
88
const Chip = ({children, className, deletable, onDeleteClick, theme, ...other}) => {
99
let hasAvatar = false;
1010
if (React.Children.count(children)) {
11-
const firstChild = children[0];
11+
const flatChildren = React.Children.toArray(children);
12+
const firstChild = flatChildren[0];
1213
hasAvatar = firstChild && firstChild.type && firstChild.type === Avatar;
1314
}
1415

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import expect from 'expect';
2+
import React from 'react';
3+
import ReactDOM from 'react-dom';
4+
import ReactTestUtils from 'react-addons-test-utils';
5+
import { themr } from 'react-css-themr';
6+
import { CHIP } from '../../identifiers.js';
7+
import { chipFactory } from '../Chip';
8+
import { tooltipFactory } from '../../tooltip';
9+
10+
const Avatar = ({title}) => <span>{title}</span>; // eslint-disable-line react/prop-types
11+
const Chip = themr(CHIP)(chipFactory(Avatar));
12+
13+
describe('Chip', function () {
14+
describe('with avatar', function () {
15+
it('adds the avatar class to the element', function () {
16+
const tree = ReactTestUtils.renderIntoDocument(
17+
<Chip theme={{avatar: 'avatar-class'}}>
18+
<Avatar title='Test'/>
19+
<span>Test</span>
20+
</Chip>
21+
);
22+
const chip = ReactTestUtils.findRenderedComponentWithType(tree, Chip);
23+
const chipNode = ReactDOM.findDOMNode(chip);
24+
expect(chipNode.className).toMatch(/\bavatar-class\b/);
25+
});
26+
27+
it('works with non-flat children', function () {
28+
const TooltippedChip = tooltipFactory()(Chip);
29+
const tree = ReactTestUtils.renderIntoDocument(
30+
<TooltippedChip theme={{avatar: 'avatar-class'}} tooltip='Test tooltip'>
31+
<Avatar title='Test'/>
32+
<span>Test</span>
33+
</TooltippedChip>
34+
);
35+
const chip = ReactTestUtils.findRenderedComponentWithType(tree, Chip);
36+
const chipNode = ReactDOM.findDOMNode(chip);
37+
expect(chipNode.className).toMatch(/\bavatar-class\b/);
38+
});
39+
});
40+
41+
describe('without avatar', function () {
42+
it('does not add avatar class to the element', function () {
43+
const tree = ReactTestUtils.renderIntoDocument(
44+
<Chip theme={{avatar: 'avatar-class'}}>
45+
<span>Test</span>
46+
</Chip>
47+
);
48+
const chip = ReactTestUtils.findRenderedComponentWithType(tree, Chip);
49+
const chipNode = ReactDOM.findDOMNode(chip);
50+
expect(chipNode.className).toNotMatch(/\bavatar-class\b/);
51+
});
52+
});
53+
});

0 commit comments

Comments
 (0)