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.
chore(superset-ui-control-uitils): add MetricOption and dependencies …
…to control utils (apache#593) * feat: metric option added to control utils * add test * Add MetricOptions to superset-ui control utils and fix tests * add suggestions * Update packages/superset-ui-control-utils/test/components/InfoTooltipWithTrigger.test.tsx Co-authored-by: Evan Rusackas <evan@preset.io> * remove prop assignments * fix lint Co-authored-by: Evan Rusackas <evan@preset.io>
- Loading branch information
1 parent
422f4e6
commit 3b27a08
Showing
7 changed files
with
197 additions
and
5 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
86 changes: 86 additions & 0 deletions
86
...temporary_superset_ui/superset-ui/packages/superset-ui-control-utils/src/MetricOption.tsx
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,86 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import React from 'react'; | ||
import InfoTooltipWithTrigger from './InfoTooltipWithTrigger'; | ||
import { ColumnTypeLabel } from './ColumnTypeLabel'; | ||
|
||
export interface MetricOptionProps { | ||
metric: { | ||
// eslint-disable-next-line camelcase | ||
verbose_name: string; | ||
// eslint-disable-next-line camelcase | ||
metric_name: string; | ||
label: string; | ||
description: string; | ||
// eslint-disable-next-line camelcase | ||
warning_text: string; | ||
expression: string; | ||
}; | ||
openInNewWindow: boolean; | ||
showFormula: boolean; | ||
showType: boolean; | ||
url: string; | ||
} | ||
|
||
export function MetricOption({ | ||
metric, | ||
openInNewWindow = false, | ||
showFormula = true, | ||
showType = false, | ||
url = '', | ||
}: MetricOptionProps) { | ||
const verbose = metric.verbose_name || metric.metric_name || metric.label; | ||
const link = url ? ( | ||
<a href={url} target={openInNewWindow ? '_blank' : ''}> | ||
{verbose} | ||
</a> | ||
) : ( | ||
verbose | ||
); | ||
return ( | ||
<div className="metric-option"> | ||
{showType && <ColumnTypeLabel type="expression" />} | ||
<span className="option-label">{link}</span> | ||
{metric.description && ( | ||
<InfoTooltipWithTrigger | ||
className="text-muted" | ||
icon="info" | ||
tooltip={metric.description} | ||
label={`descr-${metric.metric_name}`} | ||
/> | ||
)} | ||
{showFormula && ( | ||
<InfoTooltipWithTrigger | ||
className="text-muted" | ||
icon="question-circle-o" | ||
tooltip={metric.expression} | ||
label={`expr-${metric.metric_name}`} | ||
/> | ||
)} | ||
{metric.warning_text && ( | ||
<InfoTooltipWithTrigger | ||
className="text-danger" | ||
icon="warning" | ||
tooltip={metric.warning_text} | ||
label={`warn-${metric.metric_name}`} | ||
/> | ||
)} | ||
</div> | ||
); | ||
} |
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
20 changes: 19 additions & 1 deletion
20
...tils/test/InfoTooltipWithTrigger.test.tsx → ...omponents/InfoTooltipWithTrigger.test.tsx
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
86 changes: 86 additions & 0 deletions
86
...t_ui/superset-ui/packages/superset-ui-control-utils/test/components/MetricOption.test.tsx
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,86 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import React from 'react'; | ||
import { shallow, ShallowWrapper } from 'enzyme'; | ||
import { MetricOption, MetricOptionProps } from '../../src/MetricOption'; | ||
|
||
describe('MetricOption', () => { | ||
const defaultProps = { | ||
metric: { | ||
metric_name: 'foo', | ||
verbose_name: 'Foo', | ||
expression: 'SUM(foo)', | ||
label: 'test', | ||
description: 'Foo is the greatest metric of all', | ||
warning_text: 'Be careful when using foo', | ||
}, | ||
openInNewWindow: false, | ||
showFormula: true, | ||
showType: true, | ||
url: '', | ||
}; | ||
|
||
let wrapper: ShallowWrapper; | ||
let props: MetricOptionProps; | ||
const factory = (o: MetricOptionProps) => <MetricOption {...o} />; | ||
beforeEach(() => { | ||
wrapper = shallow(factory(defaultProps)); | ||
props = { ...defaultProps }; | ||
}); | ||
it('is a valid element', () => { | ||
expect(React.isValidElement(<MetricOption {...defaultProps} />)).toBe(true); | ||
}); | ||
it('shows a label with verbose_name', () => { | ||
const lbl = wrapper.find('.option-label'); | ||
expect(lbl).toHaveLength(1); | ||
expect(lbl.first().text()).toBe('Foo'); | ||
}); | ||
it('shows 3 InfoTooltipWithTrigger', () => { | ||
expect(wrapper.find('InfoTooltipWithTrigger')).toHaveLength(3); | ||
}); | ||
it('shows only 2 InfoTooltipWithTrigger when no descr', () => { | ||
props.metric.description = ''; | ||
wrapper = shallow(factory(props)); | ||
expect(wrapper.find('InfoTooltipWithTrigger')).toHaveLength(2); | ||
}); | ||
it('shows a label with metric_name when no verbose_name', () => { | ||
props.metric.verbose_name = ''; | ||
wrapper = shallow(factory(props)); | ||
expect(wrapper.find('.option-label').first().text()).toBe('foo'); | ||
}); | ||
it('shows only 1 InfoTooltipWithTrigger when no descr and no warning', () => { | ||
props.metric.warning_text = ''; | ||
wrapper = shallow(factory(props)); | ||
expect(wrapper.find('InfoTooltipWithTrigger')).toHaveLength(1); | ||
}); | ||
it('sets target="_blank" when openInNewWindow is true', () => { | ||
props.url = 'https://github.com/apache/incubator-superset'; | ||
wrapper = shallow(factory(props)); | ||
expect(wrapper.find('a').prop('target')).toBe(''); | ||
|
||
props.openInNewWindow = true; | ||
wrapper = shallow(factory(props)); | ||
expect(wrapper.find('a').prop('target')).toBe('_blank'); | ||
}); | ||
it('shows a metric type label when showType is true', () => { | ||
props.showType = true; | ||
wrapper = shallow(factory(props)); | ||
expect(wrapper.find('ColumnTypeLabel')).toHaveLength(1); | ||
}); | ||
}); |