Files
grafana/public/app/plugins/panel/gauge/Threshold.test.tsx

143 lines
4.6 KiB
TypeScript
Raw Normal View History

2018-11-23 16:12:53 +01:00
import React from 'react';
import { shallow } from 'enzyme';
import Thresholds from './Thresholds';
2018-12-11 16:06:36 +01:00
import { defaultProps, OptionsProps } from './module';
2018-11-23 16:12:53 +01:00
import { PanelOptionsProps } from '../../../types';
const setup = (propOverrides?: object) => {
const props: PanelOptionsProps<OptionsProps> = {
onChange: jest.fn(),
2018-12-11 16:06:36 +01:00
options: {
...defaultProps.options,
thresholds: [
{ index: 0, label: 'Min', value: 0, canRemove: false, color: 'rgba(50, 172, 45, 0.97)' },
{ index: 1, label: 'Max', value: 100, canRemove: false },
],
},
2018-11-23 16:12:53 +01:00
};
Object.assign(props, propOverrides);
return shallow(<Thresholds {...props} />).instance() as Thresholds;
};
describe('Add threshold', () => {
2018-11-27 13:42:13 +01:00
it('should add threshold between min and max', () => {
2018-11-23 16:12:53 +01:00
const instance = setup();
instance.onAddThreshold(1);
expect(instance.state.thresholds).toEqual([
2018-11-29 13:44:35 +01:00
{ index: 0, label: 'Min', value: 0, canRemove: false, color: 'rgba(50, 172, 45, 0.97)' },
{ index: 1, label: '', value: 50, canRemove: true, color: 'rgba(237, 129, 40, 0.89)' },
2018-11-23 16:12:53 +01:00
{ index: 2, label: 'Max', value: 100, canRemove: false },
]);
});
it('should add threshold between min and added threshold', () => {
2018-11-29 15:04:49 +01:00
const instance = setup({
2018-12-11 16:06:36 +01:00
options: {
...defaultProps.options,
thresholds: [
{ index: 0, label: 'Min', value: 0, canRemove: false, color: 'rgba(50, 172, 45, 0.97)' },
{ index: 1, label: '', value: 50, canRemove: true, color: 'rgba(237, 129, 40, 0.89)' },
{ index: 2, label: 'Max', value: 100, canRemove: false },
],
},
2018-11-29 15:04:49 +01:00
});
2018-11-23 16:12:53 +01:00
instance.onAddThreshold(1);
expect(instance.state.thresholds).toEqual([
2018-11-29 13:44:35 +01:00
{ index: 0, label: 'Min', value: 0, canRemove: false, color: 'rgba(50, 172, 45, 0.97)' },
{ index: 1, label: '', value: 25, canRemove: true, color: 'rgba(237, 129, 40, 0.89)' },
{ index: 2, label: '', value: 50, canRemove: true, color: 'rgba(237, 129, 40, 0.89)' },
2018-11-23 16:12:53 +01:00
{ index: 3, label: 'Max', value: 100, canRemove: false },
]);
});
});
describe('Add at index', () => {
it('should return 1, no added thresholds', () => {
const instance = setup();
const result = instance.insertAtIndex(1);
expect(result).toEqual(1);
});
it('should return 1, one added threshold', () => {
const instance = setup();
instance.state = {
thresholds: [
{ index: 0, label: 'Min', value: 0, canRemove: false },
{ index: 1, label: '', value: 50, canRemove: true },
{ index: 2, label: 'Max', value: 100, canRemove: false },
],
};
const result = instance.insertAtIndex(1);
expect(result).toEqual(1);
});
it('should return 2, two added thresholds', () => {
2018-11-29 15:04:49 +01:00
const instance = setup({
options: {
thresholds: [
{ index: 0, label: 'Min', value: 0, canRemove: false },
{ index: 1, label: '', value: 25, canRemove: true },
{ index: 2, label: '', value: 50, canRemove: true },
{ index: 3, label: 'Max', value: 100, canRemove: false },
],
},
});
2018-11-23 16:12:53 +01:00
const result = instance.insertAtIndex(2);
expect(result).toEqual(2);
});
it('should return 2, one added threshold', () => {
const instance = setup();
instance.state = {
thresholds: [
{ index: 0, label: 'Min', value: 0, canRemove: false },
{ index: 1, label: '', value: 50, canRemove: true },
{ index: 2, label: 'Max', value: 100, canRemove: false },
],
};
const result = instance.insertAtIndex(2);
expect(result).toEqual(2);
});
});
2018-11-27 13:42:13 +01:00
describe('change threshold value', () => {
it('should update value and resort rows', () => {
const instance = setup();
const mockThresholds = [
2018-11-29 13:44:35 +01:00
{ index: 0, label: 'Min', value: 0, canRemove: false, color: 'rgba(50, 172, 45, 0.97)' },
{ index: 1, label: '', value: 50, canRemove: true, color: 'rgba(237, 129, 40, 0.89)' },
{ index: 2, label: '', value: 75, canRemove: true, color: 'rgba(237, 129, 40, 0.89)' },
2018-11-27 13:42:13 +01:00
{ index: 3, label: 'Max', value: 100, canRemove: false },
];
instance.state = {
thresholds: mockThresholds,
};
const mockEvent = { target: { value: 78 } };
instance.onChangeThresholdValue(mockEvent, mockThresholds[1]);
expect(instance.state.thresholds).toEqual([
2018-11-29 13:44:35 +01:00
{ index: 0, label: 'Min', value: 0, canRemove: false, color: 'rgba(50, 172, 45, 0.97)' },
{ index: 1, label: '', value: 78, canRemove: true, color: 'rgba(237, 129, 40, 0.89)' },
{ index: 2, label: '', value: 75, canRemove: true, color: 'rgba(237, 129, 40, 0.89)' },
2018-11-27 13:42:13 +01:00
{ index: 3, label: 'Max', value: 100, canRemove: false },
]);
});
});