Skip to content

Commit 1755d40

Browse files
author
Dumora Guillaume
committed
[MAKE_RELEASE] fix : fix problem when we have empty children and renderOn with FlagsConsumer
1 parent 002ecc9 commit 1755d40

File tree

3 files changed

+57
-11
lines changed

3 files changed

+57
-11
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
import FlagsConsumer from "./src/FlagsConsumer";
2+
13
# Changelog
24
All notable changes to this project are documented in this file.
35

6+
## [1.0.28] - 2019-06-27
7+
### Fix
8+
- Fix a problem when no children and no renderOn are provided to the FlagsConsumer.
9+
410
## [1.0.27] - 2019-06-17
511
### Added
612
- `JSON, Numbers and Strings` are now injected as props for multivariant flags.

src/FlagsConsumer.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,45 @@ export default class FlagsConsumer extends Component {
1313
};
1414

1515
static defaultProps = {
16-
renderOn: () => null,
17-
fallbackRender: () => null
16+
renderOn: undefined,
17+
fallbackRender: undefined
18+
};
19+
20+
renderChildrenOrRenderOn = (featureProps, children, renderOn) => {
21+
if (renderOn) {
22+
return renderOn(featureProps);
23+
}
24+
if (children) {
25+
return React.cloneElement(children, { flag: featureProps });
26+
}
27+
return React.Fragment;
1828
};
1929

2030
render() {
31+
const { flag, children, renderOn, fallbackRender } = this.props;
2132
return (
2233
<FlagsContext.Consumer>
2334
{ldClient => {
24-
const flagValue = ldClient.variation(this.props.flag, false);
35+
const flagValue = ldClient.variation(flag, false);
2536
const featureProps = {
26-
[camelize(this.props.flag)]: flagValue
37+
[camelize(flag)]: flagValue
2738
};
2839
return (() => {
2940
if (flagValue === true) {
30-
return (
31-
this.props.renderOn(featureProps) ||
32-
React.cloneElement(this.props.children, { flag: featureProps })
41+
return this.renderChildrenOrRenderOn(
42+
featureProps,
43+
children,
44+
renderOn
3345
);
3446
}
3547
if (flagValue === false) {
36-
return this.props.fallbackRender(featureProps) || null;
48+
return fallbackRender ? fallbackRender(featureProps) : null;
3749
}
3850
if (typeof flagValue !== 'boolean') {
39-
return (
40-
this.props.renderOn(featureProps) ||
41-
React.cloneElement(this.props.children, { flag: featureProps })
51+
return this.renderChildrenOrRenderOn(
52+
featureProps,
53+
children,
54+
renderOn
4255
);
4356
}
4457
return null;

src/__tests__/FlagsConsumer.spec.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@ describe('Flags', () => {
3232
expect(wrapper.find('h4').length).toBe(1);
3333
});
3434

35+
it('should return nothing when we have no children or renderProps and flags is active (true)', () => {
36+
const Flags = getComponentWithContext({
37+
variation: jest.fn(() => true)
38+
});
39+
const wrapper = mount(<Flags flag="beta-only" />);
40+
expect(wrapper.find('Consumer').children().length).toBe(0);
41+
});
42+
43+
it('should return nothing when we have a children with a condition and flags is active (true)', () => {
44+
const Flags = getComponentWithContext({
45+
variation: jest.fn(() => true)
46+
});
47+
const wrapper = mount(
48+
<Flags flag="beta-only">{1 === 0 && <h4>for beta users</h4>}</Flags>
49+
);
50+
expect(wrapper.find('Consumer').children().length).toBe(0);
51+
});
52+
3553
it('should return the component or element given by children props when flags is active (true) and there no renderProps defined', () => {
3654
const Flags = getComponentWithContext({
3755
variation: jest.fn(() => true)
@@ -46,6 +64,7 @@ describe('Flags', () => {
4664
<h4 className="children">for beta users</h4>
4765
</Flags>
4866
);
67+
4968
expect(wrapper.find('h4.fallbackRender').length).toBe(0);
5069
expect(wrapper.find('h4.children').length).toBe(1);
5170
});
@@ -190,6 +209,14 @@ describe('Flags', () => {
190209
expect(wrapper.find('h4').length).toBe(1);
191210
});
192211

212+
it('should return nothing when we have no children or renderProps and flags is active (json)', () => {
213+
const Flags = getComponentWithContext({
214+
variation: jest.fn(() => ({ test1: 1, test2: 2 }))
215+
});
216+
const wrapper = mount(<Flags flag="multi-variant-json" />);
217+
expect(wrapper.find('Consumer').children().length).toBe(0);
218+
});
219+
193220
it('should return the component or element given by children props when flags is active (json) and there no renderProps defined', () => {
194221
const Flags = getComponentWithContext({
195222
variation: jest.fn(() => ({ test1: 1, test2: 2 }))

0 commit comments

Comments
 (0)