Threshold.test.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import React from 'react';
  2. import { shallow } from 'enzyme';
  3. import Thresholds from './Thresholds';
  4. import { defaultProps, OptionsProps } from './module';
  5. import { BasicGaugeColor } from 'app/types';
  6. import { PanelOptionsProps } from '@grafana/ui';
  7. const setup = (propOverrides?: object) => {
  8. const props: PanelOptionsProps<OptionsProps> = {
  9. onChange: jest.fn(),
  10. options: {
  11. ...defaultProps.options,
  12. thresholds: [],
  13. },
  14. };
  15. Object.assign(props, propOverrides);
  16. return shallow(<Thresholds {...props} />).instance() as Thresholds;
  17. };
  18. describe('Add threshold', () => {
  19. it('should add threshold', () => {
  20. const instance = setup();
  21. instance.onAddThreshold(0);
  22. expect(instance.state.thresholds).toEqual([{ index: 0, value: 50, color: 'rgb(127, 115, 64)' }]);
  23. });
  24. it('should add another threshold above a first', () => {
  25. const instance = setup({
  26. options: {
  27. ...defaultProps.options,
  28. thresholds: [{ index: 0, value: 50, color: 'rgb(127, 115, 64)' }],
  29. },
  30. });
  31. instance.onAddThreshold(1);
  32. expect(instance.state.thresholds).toEqual([
  33. { index: 1, value: 75, color: 'rgb(170, 95, 61)' },
  34. { index: 0, value: 50, color: 'rgb(127, 115, 64)' },
  35. ]);
  36. });
  37. });
  38. describe('change threshold value', () => {
  39. it('should update value and resort rows', () => {
  40. const instance = setup();
  41. const mockThresholds = [
  42. { index: 0, value: 50, color: 'rgba(237, 129, 40, 0.89)' },
  43. { index: 1, value: 75, color: 'rgba(237, 129, 40, 0.89)' },
  44. ];
  45. instance.state = {
  46. baseColor: BasicGaugeColor.Green,
  47. thresholds: mockThresholds,
  48. };
  49. const mockEvent = { target: { value: 78 } };
  50. instance.onChangeThresholdValue(mockEvent, mockThresholds[0]);
  51. expect(instance.state.thresholds).toEqual([
  52. { index: 0, value: 78, color: 'rgba(237, 129, 40, 0.89)' },
  53. { index: 1, value: 75, color: 'rgba(237, 129, 40, 0.89)' },
  54. ]);
  55. });
  56. });