Skip to content

Commit 1411610

Browse files
committed
Add multiple prop to TunnelPlaceholder and more tests
1 parent 5e7231f commit 1411610

File tree

2 files changed

+43
-34
lines changed

2 files changed

+43
-34
lines changed

src/TunnelPlaceholder.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class TunnelPlaceholder extends Component {
55
static propTypes = {
66
children: PropTypes.func,
77
id: PropTypes.string.isRequired,
8+
multiple: PropTypes.bool,
89
}
910

1011
static contextTypes = {
@@ -29,30 +30,26 @@ class TunnelPlaceholder extends Component {
2930

3031
render() {
3132
const { tunnelState } = this.context
32-
const { id, children: renderChildren } = this.props
33+
const { id, children: renderChildren, multiple } = this.props
3334
const tunnelProps = tunnelState.getTunnelProps(id)
3435

35-
if (Array.isArray(tunnelProps)) {
36-
if (renderChildren) {
37-
return createElement(renderChildren, { items: tunnelProps })
38-
}
39-
40-
if (tunnelProps.length > 0) {
41-
return <Fragment>{tunnelProps.map(props => props.children)}</Fragment>
42-
}
43-
} else {
44-
if (renderChildren) {
36+
if (renderChildren) {
37+
if (Array.isArray(tunnelProps) || multiple) {
38+
return !tunnelProps
39+
? createElement(renderChildren, { items: [] })
40+
: createElement(renderChildren, {
41+
items: Array.isArray(tunnelProps) ? tunnelProps : [tunnelProps],
42+
})
43+
} else {
4544
return createElement(renderChildren, tunnelProps || {})
4645
}
46+
}
4747

48-
if (!tunnelProps) {
49-
return null
50-
}
51-
52-
return <Fragment>{tunnelProps.children}</Fragment>
48+
if (!tunnelProps) {
49+
return null
5350
}
5451

55-
return null
52+
return <Fragment>{tunnelProps.children}</Fragment>
5653
}
5754
}
5855

tests/Tunnel.test.js

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('Tunnel', () => {
2222
<TunnelPlaceholder id={TUNNEL_ID}>
2323
{({ message }) => <span>{message}</span>}
2424
</TunnelPlaceholder>
25-
<Tunnel id={TUNNEL_ID} props={props} />
25+
<Tunnel id={TUNNEL_ID} {...props} />
2626
</div>
2727
</TunnelProvider>,
2828
)
@@ -43,16 +43,39 @@ describe('Tunnel', () => {
4343
assertTunnelPlaceholderContent(wrapper, props.message)
4444
})
4545

46-
it('should render passing a component', () => {
46+
it('should render multiple tunnels props when there is a multiple prop', () => {
4747
const wrapper = mount(
4848
<TunnelProvider>
4949
<div>
50-
<TunnelPlaceholder id={TUNNEL_ID} component={Msg} />
51-
<Tunnel id={TUNNEL_ID} props={props} />
50+
<TunnelPlaceholder id={TUNNEL_ID} multiple>
51+
{({ items = [] }) =>
52+
items.map((props, i) => <div key={i}>{props.message}</div>)
53+
}
54+
</TunnelPlaceholder>
55+
<Tunnel id={TUNNEL_ID} message="Foo" />
5256
</div>
5357
</TunnelProvider>,
5458
)
55-
assertTunnelPlaceholderContent(wrapper, props.message)
59+
expect(wrapper.contains([<div key={0}>Foo</div>])).toEqual(true)
60+
})
61+
62+
it('should render multiple tunnels props when there are many tunnels', () => {
63+
const wrapper = mount(
64+
<TunnelProvider>
65+
<div>
66+
<TunnelPlaceholder id={TUNNEL_ID}>
67+
{({ items = [] }) =>
68+
items.map((props, i) => <div key={i}>{props.message}</div>)
69+
}
70+
</TunnelPlaceholder>
71+
<Tunnel id={TUNNEL_ID} message="Foo" />
72+
<Tunnel id={TUNNEL_ID} message="Bar" />
73+
</div>
74+
</TunnelProvider>,
75+
)
76+
expect(
77+
wrapper.contains([<div key={0}>Foo</div>, <div key={1}>Bar</div>]),
78+
).toEqual(true)
5679
})
5780

5881
describe('given Tunnel is not defined', () => {
@@ -67,17 +90,6 @@ describe('Tunnel', () => {
6790
assertTunnelPlaceholderContent(wrapper, 'Empty')
6891
})
6992

70-
it('should render TunnelPlaceholder component passing empty props', () => {
71-
const wrapper = mount(
72-
<TunnelProvider>
73-
<div>
74-
<TunnelPlaceholder id={TUNNEL_ID} component={Msg} />
75-
</div>
76-
</TunnelProvider>,
77-
)
78-
assertTunnelPlaceholderContent(wrapper, 'defaultMessage')
79-
})
80-
8193
it('should render empty is TunnelPlaceholder does not have children', () => {
8294
const wrapper = mount(
8395
<TunnelProvider>
@@ -118,7 +130,7 @@ describe('Tunnel', () => {
118130
assertTunnelPlaceholderContent(wrapper, msg1)
119131
})
120132

121-
it.only('should update TunnelPlaceholder when Tunnel is unmounted', () => {
133+
it('should update TunnelPlaceholder when Tunnel is unmounted', () => {
122134
const wrapper = mount(<Component msg={msg1} visible />)
123135
assertTunnelPlaceholderContent(wrapper, msg1)
124136
wrapper.setProps({ visible: false })

0 commit comments

Comments
 (0)