forked from apache/superset
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add `reactify` function from `incubator-superset`
- Loading branch information
1 parent
c2182c6
commit 2aff22d
Showing
6 changed files
with
148 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
.../temporary_superset_ui/superset-ui/packages/superset-ui-chart/src/components/reactify.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React from 'react'; | ||
|
||
export default function reactify(renderFn) { | ||
class ReactifiedComponent extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.setContainerRef = this.setContainerRef.bind(this); | ||
} | ||
|
||
componentDidMount() { | ||
this.execute(); | ||
} | ||
|
||
componentDidUpdate() { | ||
this.execute(); | ||
} | ||
|
||
componentWillUnmount() { | ||
this.container = null; | ||
} | ||
|
||
setContainerRef(c) { | ||
this.container = c; | ||
} | ||
|
||
execute() { | ||
if (this.container) { | ||
renderFn(this.container, this.props); | ||
} | ||
} | ||
|
||
render() { | ||
const { id, className } = this.props; | ||
|
||
return <div id={id} className={className} ref={this.setContainerRef} />; | ||
} | ||
} | ||
|
||
if (renderFn.displayName) { | ||
ReactifiedComponent.displayName = renderFn.displayName; | ||
} | ||
/* eslint-disable-next-line react/forbid-foreign-prop-types */ | ||
if (renderFn.propTypes) { | ||
ReactifiedComponent.propTypes = renderFn.propTypes; | ||
} | ||
if (renderFn.defaultProps) { | ||
ReactifiedComponent.defaultProps = renderFn.defaultProps; | ||
} | ||
|
||
return ReactifiedComponent; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ui/superset-ui/packages/superset-ui-chart/test/components/createLoadableRenderer.test.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
...rary_superset_ui/superset-ui/packages/superset-ui-chart/test/components/reactify.test.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import PropTypes from 'prop-types'; | ||
import React from 'react'; | ||
import { mount } from 'enzyme'; | ||
import { reactify } from '../../src'; | ||
|
||
describe('reactify(renderFn)', () => { | ||
const renderFn = jest.fn((element, props) => { | ||
const container = element; | ||
container.innerHTML = ''; | ||
const child = document.createElement('b'); | ||
child.innerHTML = props.content; | ||
container.appendChild(child); | ||
}); | ||
|
||
renderFn.displayName = 'BoldText'; | ||
renderFn.propTypes = { | ||
content: PropTypes.string, | ||
}; | ||
renderFn.defaultProps = { | ||
content: 'ghi', | ||
}; | ||
|
||
const TheChart = reactify(renderFn); | ||
|
||
class TestComponent extends React.PureComponent { | ||
constructor(props) { | ||
super(props); | ||
this.state = { content: 'abc' }; | ||
} | ||
|
||
componentDidMount() { | ||
setTimeout(() => { | ||
this.setState({ content: 'def' }); | ||
}, 10); | ||
} | ||
|
||
render() { | ||
const { content } = this.state; | ||
|
||
return <TheChart content={content} />; | ||
} | ||
} | ||
|
||
it('returns a React component class', done => { | ||
const wrapper = mount(<TestComponent />); | ||
expect(renderFn).toHaveBeenCalledTimes(1); | ||
expect(wrapper.html()).toEqual('<div><b>abc</b></div>'); | ||
setTimeout(() => { | ||
expect(renderFn).toHaveBeenCalledTimes(2); | ||
expect(wrapper.html()).toEqual('<div><b>def</b></div>'); | ||
wrapper.unmount(); | ||
done(); | ||
}, 20); | ||
}); | ||
describe('displayName', () => { | ||
it('has displayName if renderFn.displayName is defined', () => { | ||
expect(TheChart.displayName).toEqual('BoldText'); | ||
}); | ||
it('does not have displayName if renderFn.displayName is not defined', () => { | ||
const AnotherChart = reactify(() => {}); | ||
expect(AnotherChart.displayName).toBeUndefined(); | ||
}); | ||
}); | ||
describe('propTypes', () => { | ||
it('has propTypes if renderFn.propTypes is defined', () => { | ||
/* eslint-disable-next-line react/forbid-foreign-prop-types */ | ||
expect(TheChart.propTypes).toBe(renderFn.propTypes); | ||
}); | ||
it('does not have propTypes if renderFn.propTypes is not defined', () => { | ||
const AnotherChart = reactify(() => {}); | ||
/* eslint-disable-next-line react/forbid-foreign-prop-types */ | ||
expect(AnotherChart.propTypes).toBeUndefined(); | ||
}); | ||
}); | ||
describe('defaultProps', () => { | ||
it('has defaultProps if renderFn.defaultProps is defined', () => { | ||
expect(TheChart.defaultProps).toBe(renderFn.defaultProps); | ||
const wrapper = mount(<TheChart />); | ||
expect(wrapper.html()).toEqual('<div><b>ghi</b></div>'); | ||
}); | ||
it('does not have defaultProps if renderFn.defaultProps is not defined', () => { | ||
const AnotherChart = reactify(() => {}); | ||
expect(AnotherChart.defaultProps).toBeUndefined(); | ||
}); | ||
}); | ||
it('does not try to render if not mounted', () => { | ||
const anotherRenderFn = jest.fn(); | ||
const AnotherChart = reactify(anotherRenderFn); | ||
new AnotherChart().execute(); | ||
expect(anotherRenderFn).not.toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters