Allowing withVerification to remove all options if none are valid (#7652)

This commit is contained in:
michellethomas 2019-06-18 14:06:05 -07:00 committed by GitHub
parent f278faa8be
commit 5864ddc079
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 4 deletions

View File

@ -44,7 +44,7 @@ const defaultProps = {
const VALID_METRIC = { metric_name: 'sum__value', expression: 'SUM(energy_usage.value)' };
function setup(overrides) {
function setup(overrides, validMetric) {
const onChange = sinon.spy();
const props = {
onChange,
@ -53,7 +53,7 @@ function setup(overrides) {
};
const VerifiedControl = withVerification(MetricsControl, 'metric_name', 'savedMetrics');
const wrapper = shallow(<VerifiedControl {...props} />);
fetchMock.mock('glob:*/valid_metrics*', `["${VALID_METRIC.metric_name}"]`);
fetchMock.mock('glob:*/valid_metrics*', validMetric || `["${VALID_METRIC.metric_name}"]`);
return { props, wrapper, onChange };
}
@ -103,4 +103,14 @@ describe('VerifiedMetricsControl', () => {
fetchMock.reset();
}, 0);
});
it('Returns no verified options if none are valid', () => {
const { wrapper } = setup({}, []);
setTimeout(() => {
expect(fetchMock.calls(defaultProps.getEndpoint())).toHaveLength(1);
const child = wrapper.find(MetricsControl);
expect(child.props().savedMetrics).toEqual([]);
fetchMock.reset();
}, 0);
});
});

View File

@ -31,7 +31,7 @@ export default function withVerification(WrappedComponent, optionLabel, optionsN
constructor(props) {
super(props);
this.state = {
validOptions: new Set(),
validOptions: null,
hasRunVerification: false,
};
@ -69,7 +69,7 @@ export default function withVerification(WrappedComponent, optionLabel, optionsN
render() {
const { validOptions } = this.state;
const options = this.props[optionsName];
const verifiedOptions = validOptions.size ?
const verifiedOptions = validOptions ?
options.filter(o => (validOptions.has(o[optionLabel]))) :
options;