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.
Showing
6 changed files
with
149 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
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
47 changes: 47 additions & 0 deletions
47
...perset_ui/superset-ui/packages/superset-ui-chart/src/components/createLoadableRenderer.js
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,47 @@ | ||
import Loadable from 'react-loadable'; | ||
import PropTypes from 'prop-types'; | ||
|
||
const propTypes = { | ||
onRenderFailure: PropTypes.func, | ||
onRenderSuccess: PropTypes.func, | ||
}; | ||
|
||
const defaultProps = { | ||
onRenderFailure() {}, | ||
onRenderSuccess() {}, | ||
}; | ||
|
||
export default function createLoadableRenderer(options) { | ||
/* eslint-disable-next-line babel/new-cap */ | ||
const LoadableRenderer = Loadable.Map(options); | ||
|
||
// Extends the behavior of LoadableComponent | ||
// generated by react-loadable | ||
// to provide post-render listeners | ||
class CustomLoadableRenderer extends LoadableRenderer { | ||
componentDidMount() { | ||
this.afterRender(); | ||
} | ||
|
||
componentDidUpdate() { | ||
this.afterRender(); | ||
} | ||
|
||
afterRender() { | ||
const { loaded, loading, error } = this.state; | ||
if (!loading) { | ||
if (error) { | ||
this.props.onRenderFailure(error); | ||
} else if (loaded && Object.keys(loaded).length > 0) { | ||
this.props.onRenderSuccess(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
CustomLoadableRenderer.defaultProps = defaultProps; | ||
CustomLoadableRenderer.propTypes = propTypes; | ||
CustomLoadableRenderer.preload = LoadableRenderer.preload; | ||
|
||
return CustomLoadableRenderer; | ||
} |
3 changes: 3 additions & 0 deletions
3
superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-chart/src/index.js
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
87 changes: 87 additions & 0 deletions
87
...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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import React from 'react'; | ||
import { shallow } from 'enzyme'; | ||
import createLoadableRenderer from '../../src/components/createLoadableRenderer'; | ||
|
||
describe('createLoadableRenderer', () => { | ||
function TestComponent() { | ||
return <div className="test-component">test</div>; | ||
} | ||
let loadChartSuccess; | ||
let render; | ||
let loading; | ||
let LoadableRenderer; | ||
|
||
beforeEach(() => { | ||
loadChartSuccess = jest.fn(() => Promise.resolve(TestComponent)); | ||
render = jest.fn(loaded => { | ||
const { Chart } = loaded; | ||
|
||
return <Chart />; | ||
}); | ||
loading = jest.fn(() => <div>Loading</div>); | ||
LoadableRenderer = createLoadableRenderer({ | ||
loader: { | ||
Chart: loadChartSuccess, | ||
}, | ||
loading, | ||
render, | ||
}); | ||
}); | ||
|
||
describe('returns a LoadableRenderer class', () => { | ||
it('LoadableRenderer.preload() preloads the lazy-load components', () => { | ||
expect(LoadableRenderer.preload).toBeInstanceOf(Function); | ||
LoadableRenderer.preload(); | ||
expect(loadChartSuccess).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('calls onRenderSuccess when succeeds', done => { | ||
const onRenderSuccess = jest.fn(); | ||
const onRenderFailure = jest.fn(); | ||
shallow( | ||
<LoadableRenderer onRenderSuccess={onRenderSuccess} onRenderFailure={onRenderFailure} />, | ||
); | ||
expect(loadChartSuccess).toHaveBeenCalled(); | ||
setTimeout(() => { | ||
expect(render).toHaveBeenCalledTimes(1); | ||
expect(onRenderSuccess).toHaveBeenCalledTimes(1); | ||
expect(onRenderFailure).not.toHaveBeenCalled(); | ||
done(); | ||
}, 10); | ||
}); | ||
|
||
it('calls onRenderFailure when fails', done => { | ||
const loadChartFailure = jest.fn(() => Promise.reject(new Error('Invalid chart'))); | ||
const FailedRenderer = createLoadableRenderer({ | ||
loader: { | ||
Chart: loadChartFailure, | ||
}, | ||
loading, | ||
render, | ||
}); | ||
const onRenderSuccess = jest.fn(); | ||
const onRenderFailure = jest.fn(); | ||
shallow( | ||
<FailedRenderer onRenderSuccess={onRenderSuccess} onRenderFailure={onRenderFailure} />, | ||
); | ||
expect(loadChartFailure).toHaveBeenCalledTimes(1); | ||
setTimeout(() => { | ||
expect(render).not.toHaveBeenCalled(); | ||
expect(onRenderSuccess).not.toHaveBeenCalled(); | ||
expect(onRenderFailure).toHaveBeenCalledTimes(1); | ||
done(); | ||
}, 10); | ||
}); | ||
|
||
it('renders the lazy-load components', done => { | ||
const wrapper = shallow(<LoadableRenderer />); | ||
// lazy-loaded component not rendered immediately | ||
expect(wrapper.find(TestComponent)).toHaveLength(0); | ||
setTimeout(() => { | ||
// but rendered after the component is loaded. | ||
expect(wrapper.find(TestComponent)).toHaveLength(1); | ||
done(); | ||
}, 10); | ||
}); | ||
}); | ||
}); |
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