Skip to content

Commit

Permalink
[explore-v2] hook up ExploreViewContainer to state and add specs (#1300)
Browse files Browse the repository at this point in the history
* add getParams func to common

* get data from redux state

* specs for chart container and explore view container
  • Loading branch information
Alanna Scott authored Oct 10, 2016
1 parent f8e2ce6 commit fe66557
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 50 deletions.
58 changes: 24 additions & 34 deletions caravel/assets/javascripts/explorev2/components/ChartContainer.jsx
Original file line number Diff line number Diff line change
@@ -1,41 +1,17 @@
import React, { PropTypes } from 'react';
import { connect } from 'react-redux';
import { Panel } from 'react-bootstrap';
import TimeSeriesLineChart from './charts/TimeSeriesLineChart';
import moment from 'moment';

const propTypes = {
viz: PropTypes.shape({
data: PropTypes.array.isRequired,
form_data: PropTypes.shape({
viz_type: PropTypes.string.isRequired,
slice_name: PropTypes.string.isRequired,
}).isRequired,
}).isRequired,
data: PropTypes.array.isRequired,
sliceName: PropTypes.string.isRequired,
vizType: PropTypes.string.isRequired,
height: PropTypes.string.isRequired,
};

export default class ChartContainer extends React.Component {
constructor(props) {
super(props);
this.state = {
params: this.getParamsFromUrl(),
data: props.viz.data,
label1: 'Label 1',
};
}

getParamsFromUrl() {
const hash = window.location.search;
const params = hash.split('?')[1].split('&');
const newParams = {};
params.forEach((p) => {
const value = p.split('=')[1].replace(/\+/g, ' ');
const key = p.split('=')[0];
newParams[key] = value;
});
return newParams;
}

class ChartContainer extends React.Component {
formatDates(values) {
const newValues = values.map(function (val) {
return {
Expand All @@ -48,8 +24,7 @@ export default class ChartContainer extends React.Component {

isLineViz() {
// todo(alanna) generalize this check and map to charts
const vizType = this.props.viz.form_data.viz_type;
return vizType === 'line';
return this.props.vizType === 'line';
}

render() {
Expand All @@ -58,13 +33,14 @@ export default class ChartContainer extends React.Component {
<Panel
style={{ height: this.props.height }}
header={
<div className="panel-title">{this.props.viz.form_data.slice_name}</div>
<div className="panel-title">{this.props.sliceName}</div>
}
>
{this.isLineViz() &&
<TimeSeriesLineChart
data={this.state.data}
label1="Label 1"
data={this.props.data}
xAxisLabel="xAxisLabel"
yAxisLabel="yAxisLabel"
/>
}
</Panel>
Expand All @@ -74,3 +50,17 @@ export default class ChartContainer extends React.Component {
}

ChartContainer.propTypes = propTypes;

function mapStateToProps(state) {
return {
data: state.viz.data,
sliceName: state.sliceName,
vizType: state.viz.formData.vizType,
};
}

function mapDispatchToProps() {
return {};
}

export default connect(mapStateToProps, mapDispatchToProps)(ChartContainer);
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import React, { PropTypes } from 'react';
import React from 'react';
import ChartContainer from './ChartContainer';
import ControlPanelsContainer from './ControlPanelsContainer';
import QueryAndSaveButtons from './QueryAndSaveButtons';

const propTypes = {
data: PropTypes.shape({
viz: PropTypes.object.isRequired,
}).isRequired,
};

export default class ExploreViewContainer extends React.Component {
constructor(props) {
super(props);
Expand Down Expand Up @@ -42,7 +36,6 @@ export default class ExploreViewContainer extends React.Component {
</div>
<div className="col-sm-8">
<ChartContainer
viz={this.props.data.viz}
height={this.state.height}
/>
</div>
Expand All @@ -51,5 +44,3 @@ export default class ExploreViewContainer extends React.Component {
);
}
}

ExploreViewContainer.propTypes = propTypes;
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import Legend from './Legend';

const propTypes = {
data: PropTypes.array.isRequired,
label1: PropTypes.string.isRequired,
xAxisLabel: PropTypes.string.isRequired,
yAxisLabel: PropTypes.string.isRequired,
};

export default class TimeSeriesLineChart extends React.Component {
Expand Down Expand Up @@ -44,12 +45,12 @@ export default class TimeSeriesLineChart extends React.Component {
>
{this.renderLines()}
<V.VictoryAxis
label={this.props.label1}
label={this.props.yAxisLabel}
orientation="left"
/>
<V.VictoryAxis
dependentAxis
label={'label needed'}
label={this.props.xAxisLabel}
orientation="bottom"
tickValues={this.props.data[0].values.map((d) => d.x)}
tickFormat={(x) => moment(new Date(x)).format('YYYY')}
Expand Down
5 changes: 2 additions & 3 deletions caravel/assets/javascripts/explorev2/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const bootstrappedState = Object.assign(initialState, {
datasourceType: bootstrapData.datasource_type,
sliceName: bootstrapData.viz.form_data.slice_name,
viz: {
data: bootstrapData.viz.data,
formData: {
sliceId: bootstrapData.viz.form_data.slice_id,
vizType: bootstrapData.viz.form_data.viz_type,
Expand All @@ -39,9 +40,7 @@ const store = createStore(exploreReducer, bootstrappedState,

ReactDOM.render(
<Provider store={store}>
<ExploreViewContainer
data={bootstrapData}
/>
<ExploreViewContainer />
</Provider>,
exploreViewContainer
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React from 'react';
import { expect } from 'chai';
import { describe, it } from 'mocha';

import ChartContainer from '../../../../javascripts/explorev2/components/ChartContainer';

describe('ChartContainer', () => {
const mockProps = {
data: [
{
classed: '',
key: 'Label 1',
values: [
{
x: -158766400000,
y: 57,
},
{
x: -156766400000,
y: 157,
},
{
x: -157766400000,
y: 257,
},
],
},
],
sliceName: 'Trend Line',
vizType: 'line',
height: '500px',
};

it('renders when vizType is line', () => {
expect(
React.isValidElement(<ChartContainer {...mockProps} />)
).to.equal(true);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React from 'react';
import { expect } from 'chai';
import { describe, it } from 'mocha';
import { shallow } from 'enzyme';

import ExploreViewContainer
from '../../../../javascripts/explorev2/components/ExploreViewContainer';
import QueryAndSaveButtons
from '../../../../javascripts/explorev2/components/QueryAndSaveButtons';
import ControlPanelsContainer
from '../../../../javascripts/explorev2/components/ControlPanelsContainer';
import ChartContainer
from '../../../../javascripts/explorev2/components/ChartContainer';

describe('ExploreViewContainer', () => {
it('renders', () => {
expect(
React.isValidElement(<ExploreViewContainer />)
).to.equal(true);
});

it('renders QueryAndSaveButtons', () => {
const wrapper = shallow(<ExploreViewContainer />);
expect(wrapper.find(QueryAndSaveButtons)).to.have.length(1);
});

it('renders ControlPanelsContainer', () => {
const wrapper = shallow(<ExploreViewContainer />);
expect(wrapper.find(ControlPanelsContainer)).to.have.length(1);
});

it('renders ChartContainer', () => {
const wrapper = shallow(<ExploreViewContainer />);
expect(wrapper.find(ChartContainer)).to.have.length(1);
});
});
12 changes: 12 additions & 0 deletions caravel/assets/utils/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,15 @@ export function getParamFromQuery(query, param) {
export function getLink(baseUrl, params) {
return baseUrl + '?' + params.join('&');
}

export function getParamsFromUrl() {
const hash = window.location.search;
const params = hash.split('?')[1].split('&');
const newParams = {};
params.forEach((p) => {
const value = p.split('=')[1].replace(/\+/g, ' ');
const key = p.split('=')[0];
newParams[key] = value;
});
return newParams;
}

0 comments on commit fe66557

Please sign in to comment.