forked from apache/superset
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merge superset pr 4941 to wasu branch
- Loading branch information
1 parent
f175095
commit f79fb1b
Showing
16 changed files
with
285 additions
and
6 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
superset/assets/spec/javascripts/sqllab/LimitControl_spec.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,62 @@ | ||
import React from 'react'; | ||
import { shallow } from 'enzyme'; | ||
|
||
import { Label } from 'react-bootstrap'; | ||
import LimitControl from '../../../src/SqlLab/components/LimitControl'; | ||
import ControlHeader from '../../../src/explore/components/ControlHeader'; | ||
describe('LimitControl', () => { | ||
const defaultProps = { | ||
value: 0, | ||
defaultQueryLimit: 1000, | ||
maxRow: 100000, | ||
onChange: () => {}, | ||
}; | ||
let wrapper; | ||
const factory = o => <LimitControl {...o} />; | ||
beforeEach(() => { | ||
wrapper = shallow(factory(defaultProps)); | ||
}); | ||
it('is a valid element', () => { | ||
expect(React.isValidElement(<LimitControl {...defaultProps} />)).toEqual(true); | ||
}); | ||
it('renders a Label', () => { | ||
expect(wrapper.find(Label)).toHaveLength(1); | ||
}); | ||
it('loads the correct state', () => { | ||
const value = 100; | ||
wrapper = shallow(factory({ ...defaultProps, value })); | ||
expect(wrapper.state().textValue).toEqual(value.toString()); | ||
wrapper.find(Label).first().simulate('click'); | ||
expect(wrapper.state().showOverlay).toBe(true); | ||
expect(wrapper.find(ControlHeader).props().validationErrors).toHaveLength(0); | ||
}); | ||
it('handles invalid value', () => { | ||
wrapper.find(Label).first().simulate('click'); | ||
wrapper.setState({ textValue: 'invalid' }); | ||
expect(wrapper.find(ControlHeader).props().validationErrors).toHaveLength(1); | ||
}); | ||
it('handles negative value', () => { | ||
wrapper.find(Label).first().simulate('click'); | ||
wrapper.setState({ textValue: '-1' }); | ||
expect(wrapper.find(ControlHeader).props().validationErrors).toHaveLength(1); | ||
}); | ||
it('handles value above max row', () => { | ||
wrapper.find(Label).first().simulate('click'); | ||
wrapper.setState({ textValue: (defaultProps.maxRow + 1).toString() }); | ||
expect(wrapper.find(ControlHeader).props().validationErrors).toHaveLength(1); | ||
}); | ||
it('opens and closes', () => { | ||
wrapper.find(Label).first().simulate('click'); | ||
expect(wrapper.state().showOverlay).toBe(true); | ||
wrapper.find('.ok').first().simulate('click'); | ||
expect(wrapper.state().showOverlay).toBe(false); | ||
}); | ||
it('resets and closes', () => { | ||
const value = 100; | ||
wrapper = shallow(factory({ ...defaultProps, value })); | ||
wrapper.find(Label).first().simulate('click'); | ||
expect(wrapper.state().textValue).toEqual(value.toString()); | ||
wrapper.find('.reset').simulate('click'); | ||
expect(wrapper.state().textValue).toEqual(defaultProps.defaultQueryLimit.toString()); | ||
}); | ||
}); |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { | ||
Button, | ||
Label, | ||
FormGroup, | ||
FormControl, | ||
Overlay, | ||
Popover, | ||
} from 'react-bootstrap'; | ||
import { t } from '@superset-ui/translation'; | ||
import ControlHeader from '../../explore/components/ControlHeader'; | ||
const propTypes = { | ||
value: PropTypes.number, | ||
defaultQueryLimit: PropTypes.number.isRequired, | ||
maxRow: PropTypes.number.isRequired, | ||
onChange: PropTypes.func.isRequired, | ||
}; | ||
export default class LimitControl extends React.PureComponent { | ||
constructor(props) { | ||
super(props); | ||
const { value, defaultQueryLimit } = props; | ||
this.state = { | ||
textValue: value.toString() || defaultQueryLimit.toString(), | ||
showOverlay: false, | ||
}; | ||
this.handleHide = this.handleHide.bind(this); | ||
this.handleToggle = this.handleToggle.bind(this); | ||
this.submitAndClose = this.submitAndClose.bind(this); | ||
} | ||
setValueAndClose(val) { | ||
this.setState({ textValue: val }, this.submitAndClose); | ||
} | ||
submitAndClose() { | ||
const value = parseInt(this.state.textValue, 10) || this.props.defaultQueryLimit; | ||
this.props.onChange(value); | ||
this.setState({ showOverlay: false }); | ||
} | ||
isValidLimit(limit) { | ||
const value = parseInt(limit, 10); | ||
return !(Number.isNaN(value) || value <= 0 || (this.props.maxRow && value > this.props.maxRow)); | ||
} | ||
handleToggle() { | ||
this.setState({ showOverlay: !this.state.showOverlay }); | ||
} | ||
handleHide() { | ||
this.setState({ showOverlay: false }); | ||
} | ||
renderPopover() { | ||
const textValue = this.state.textValue; | ||
const isValid = this.isValidLimit(textValue); | ||
const errorMsg = 'Row limit must be positive integer' + | ||
(this.props.maxRow ? ` and not greater than ${this.props.maxRow}` : ''); | ||
return ( | ||
<Popover id="sqllab-limit-results"> | ||
<div style={{ width: '100px' }}> | ||
<ControlHeader | ||
label={t('Row limit')} | ||
validationErrors={!isValid ? [t(errorMsg)] : []} | ||
/> | ||
<FormGroup> | ||
<FormControl | ||
type="text" | ||
value={textValue} | ||
placeholder={t(`Max: ${this.props.maxRow}`)} | ||
bsSize="small" | ||
onChange={e => this.setState({ textValue: e.target.value })} | ||
/> | ||
</FormGroup> | ||
<div className="clearfix"> | ||
<Button | ||
bsSize="small" | ||
bsStyle="primary" | ||
className="float-left ok" | ||
disabled={!isValid} | ||
onClick={this.submitAndClose} | ||
> | ||
Ok | ||
</Button> | ||
<Button | ||
bsSize="small" | ||
className="float-right reset" | ||
onClick={this.setValueAndClose.bind(this, this.props.defaultQueryLimit.toString())} | ||
> | ||
Reset | ||
</Button> | ||
</div> | ||
</div> | ||
</Popover> | ||
); | ||
} | ||
render() { | ||
return ( | ||
<div> | ||
<Label | ||
style={{ cursor: 'pointer' }} | ||
onClick={this.handleToggle} | ||
> | ||
LIMIT {this.props.value || this.props.maxRow} | ||
</Label> | ||
<Overlay | ||
rootClose | ||
show={this.state.showOverlay} | ||
onHide={this.handleHide} | ||
trigger="click" | ||
placement="right" | ||
target={this} | ||
> | ||
{this.renderPopover()} | ||
</Overlay> | ||
</div> | ||
); | ||
} | ||
} | ||
LimitControl.propTypes = propTypes; |
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
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
Oops, something went wrong.