ThresholdsEditor.test.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import React from 'react';
  2. import { shallow } from 'enzyme';
  3. import { ThresholdsEditor, Props } from './ThresholdsEditor';
  4. import { BasicGaugeColor } from '../../types';
  5. const setup = (propOverrides?: object) => {
  6. const props: Props = {
  7. onChange: jest.fn(),
  8. thresholds: [],
  9. };
  10. Object.assign(props, propOverrides);
  11. return shallow(<ThresholdsEditor {...props} />).instance() as ThresholdsEditor;
  12. };
  13. describe('Initialization', () => {
  14. it('should add a base threshold if missing', () => {
  15. const instance = setup();
  16. expect(instance.state.thresholds).toEqual([{ index: 0, value: -Infinity, color: '#299c46' }]);
  17. });
  18. });
  19. describe('Add threshold', () => {
  20. it('should add threshold', () => {
  21. const instance = setup();
  22. instance.onAddThreshold(1);
  23. expect(instance.state.thresholds).toEqual([
  24. { index: 1, value: 50, color: '#EAB839' },
  25. { index: 0, value: -Infinity, color: '#299c46' },
  26. ]);
  27. });
  28. it('should add another threshold above a first', () => {
  29. const instance = setup({
  30. thresholds: [{ index: 0, value: -Infinity, color: '#299c46' }, { index: 1, value: 50, color: '#EAB839' }],
  31. });
  32. instance.onAddThreshold(2);
  33. expect(instance.state.thresholds).toEqual([
  34. { index: 2, value: 75, color: '#6ED0E0' },
  35. { index: 1, value: 50, color: '#EAB839' },
  36. { index: 0, value: -Infinity, color: '#299c46' },
  37. ]);
  38. });
  39. it('should add another threshold between first and second index', () => {
  40. const instance = setup({
  41. thresholds: [
  42. { index: 0, value: -Infinity, color: '#299c46' },
  43. { index: 1, value: 50, color: '#EAB839' },
  44. { index: 2, value: 75, color: '#6ED0E0' },
  45. ],
  46. });
  47. instance.onAddThreshold(2);
  48. expect(instance.state.thresholds).toEqual([
  49. { index: 3, value: 75, color: '#EF843C' },
  50. { index: 2, value: 62.5, color: '#6ED0E0' },
  51. { index: 1, value: 50, color: '#EAB839' },
  52. { index: 0, value: -Infinity, color: '#299c46' },
  53. ]);
  54. });
  55. });
  56. describe('change threshold value', () => {
  57. it('should update value and resort rows', () => {
  58. const instance = setup();
  59. const thresholds = [
  60. { index: 0, value: -Infinity, color: '#299c46' },
  61. { index: 1, value: 50, color: '#EAB839' },
  62. { index: 2, value: 75, color: '#6ED0E0' },
  63. ];
  64. instance.state = {
  65. baseColor: BasicGaugeColor.Green,
  66. thresholds,
  67. };
  68. const mockEvent = { target: { value: 78 } };
  69. instance.onChangeThresholdValue(mockEvent, thresholds[1]);
  70. expect(instance.state.thresholds).toEqual([
  71. { index: 0, value: -Infinity, color: '#299c46' },
  72. { index: 1, value: 78, color: '#EAB839' },
  73. { index: 2, value: 75, color: '#6ED0E0' },
  74. ]);
  75. });
  76. });