From b05f1d8e63d6fa904c8a813cdeab571b5423207e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Fri, 15 Feb 2019 17:02:02 +0100 Subject: [PATCH] Added basic tests --- .../components/BarGauge/BarGauge.story.tsx | 6 +- .../src/components/BarGauge/BarGauge.test.tsx | 62 +++++++++++++++++++ .../src/components/BarGauge/BarGauge.tsx | 5 +- .../__snapshots__/BarGauge.test.tsx.snap | 35 +++++++++++ public/app/plugins/panel/bargauge/types.ts | 6 +- 5 files changed, 107 insertions(+), 7 deletions(-) create mode 100644 packages/grafana-ui/src/components/BarGauge/BarGauge.test.tsx create mode 100644 packages/grafana-ui/src/components/BarGauge/__snapshots__/BarGauge.test.tsx.snap diff --git a/packages/grafana-ui/src/components/BarGauge/BarGauge.story.tsx b/packages/grafana-ui/src/components/BarGauge/BarGauge.story.tsx index fa1ab4eaac1..c7a53af5ccf 100644 --- a/packages/grafana-ui/src/components/BarGauge/BarGauge.story.tsx +++ b/packages/grafana-ui/src/components/BarGauge/BarGauge.story.tsx @@ -36,8 +36,8 @@ BarGaugeStories.add('Vertical, with basic thresholds', () => { } = getKnobs(); return renderComponentWithTheme(BarGauge, { - width: 300, - height: 600, + width: 200, + height: 400, value: value, minValue: minValue, maxValue: maxValue, @@ -46,7 +46,7 @@ BarGaugeStories.add('Vertical, with basic thresholds', () => { postfix: '', decimals: decimals, thresholds: [ - { index: 0, value: null, color: 'green' }, + { index: 0, value: -Infinity, color: 'green' }, { index: 1, value: threshold1Value, color: threshold1Color }, { index: 1, value: threshold2Value, color: threshold2Color }, ], diff --git a/packages/grafana-ui/src/components/BarGauge/BarGauge.test.tsx b/packages/grafana-ui/src/components/BarGauge/BarGauge.test.tsx new file mode 100644 index 00000000000..c6ea9f22978 --- /dev/null +++ b/packages/grafana-ui/src/components/BarGauge/BarGauge.test.tsx @@ -0,0 +1,62 @@ +import React from 'react'; +import { shallow } from 'enzyme'; +import { BarGauge, Props } from './BarGauge'; +import { getTheme } from '../../themes'; + +jest.mock('jquery', () => ({ + plot: jest.fn(), +})); + +const setup = (propOverrides?: object) => { + const props: Props = { + maxValue: 100, + valueMappings: [], + minValue: 0, + prefix: '', + suffix: '', + thresholds: [{ index: 0, value: -Infinity, color: '#7EB26D' }], + unit: 'none', + height: 300, + width: 300, + value: 25, + decimals: 0, + theme: getTheme(), + }; + + Object.assign(props, propOverrides); + + const wrapper = shallow(); + const instance = wrapper.instance() as BarGauge; + + return { + instance, + wrapper, + }; +}; + +describe('Get font color', () => { + it('should get first threshold color when only one threshold', () => { + const { instance } = setup({ thresholds: [{ index: 0, value: -Infinity, color: '#7EB26D' }] }); + + expect(instance.getColors().value).toEqual('#7EB26D'); + }); + + it('should get the threshold color if value is same as a threshold', () => { + const { instance } = setup({ + thresholds: [ + { index: 2, value: 75, color: '#6ED0E0' }, + { index: 1, value: 10, color: '#EAB839' }, + { index: 0, value: -Infinity, color: '#7EB26D' }, + ], + }); + + expect(instance.getColors().value).toEqual('#EAB839'); + }); +}); + +describe('Render BarGauge with basic options', () => { + it('should render', () => { + const { wrapper } = setup(); + expect(wrapper).toMatchSnapshot(); + }); +}); diff --git a/packages/grafana-ui/src/components/BarGauge/BarGauge.tsx b/packages/grafana-ui/src/components/BarGauge/BarGauge.tsx index 8f251c612f4..5e1a00617e1 100644 --- a/packages/grafana-ui/src/components/BarGauge/BarGauge.tsx +++ b/packages/grafana-ui/src/components/BarGauge/BarGauge.tsx @@ -43,7 +43,6 @@ export class BarGauge extends PureComponent { const { thresholds, theme, value } = this.props; const activeThreshold = getThresholdForValue(thresholds, value); - console.log(thresholds, value); if (activeThreshold !== null) { const color = getColorFromHexRgbOrName(activeThreshold.color, theme.type); @@ -68,7 +67,7 @@ export class BarGauge extends PureComponent { const { width } = this.props; const guess = width / value.length; - const fontSize = Math.max(guess, 14); + const fontSize = Math.min(Math.max(guess, 14), 40); return { color: color, @@ -82,7 +81,7 @@ export class BarGauge extends PureComponent { const numericValue = this.getNumericValue(); const barMaxHeight = height * 0.8; // 20% for value & name const valuePercent = numericValue / (maxValue - minValue); - const barHeight = valuePercent * barMaxHeight; + const barHeight = Math.max(valuePercent * barMaxHeight, 0); const formatFunc = getValueFormat(unit); const valueFormatted = formatFunc(numericValue, decimals); diff --git a/packages/grafana-ui/src/components/BarGauge/__snapshots__/BarGauge.test.tsx.snap b/packages/grafana-ui/src/components/BarGauge/__snapshots__/BarGauge.test.tsx.snap new file mode 100644 index 00000000000..6da70cade2f --- /dev/null +++ b/packages/grafana-ui/src/components/BarGauge/__snapshots__/BarGauge.test.tsx.snap @@ -0,0 +1,35 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Render BarGauge with basic options should render 1`] = ` +
+
+ 25 +
+
+
+`; diff --git a/public/app/plugins/panel/bargauge/types.ts b/public/app/plugins/panel/bargauge/types.ts index e0c7e1e8799..799a37741ba 100644 --- a/public/app/plugins/panel/bargauge/types.ts +++ b/public/app/plugins/panel/bargauge/types.ts @@ -18,6 +18,10 @@ export const PanelDefaults: BarGaugeOptions = { suffix: '', stat: 'avg', unit: 'none', - thresholds: [], + thresholds: [ + { index: 0, value: -Infinity, color: 'green' }, + { index: 1, value: 50, color: 'orange' }, + { index: 2, value: 80, color: 'red' }, + ], valueMappings: [], };