Threshold.test.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import React from 'react';
  2. import { shallow } from 'enzyme';
  3. import Thresholds from './Thresholds';
  4. import { OptionsProps } from './module';
  5. import { PanelOptionsProps } from '../../../types';
  6. const setup = (propOverrides?: object) => {
  7. const props: PanelOptionsProps<OptionsProps> = {
  8. onChange: jest.fn(),
  9. options: {} as OptionsProps,
  10. };
  11. Object.assign(props, propOverrides);
  12. return shallow(<Thresholds {...props} />).instance() as Thresholds;
  13. };
  14. const thresholds = [
  15. { index: 0, label: 'Min', value: 0, canRemove: false, color: 'rgba(50, 172, 45, 0.97)' },
  16. { index: 1, label: '', value: 50, canRemove: true, color: 'rgba(237, 129, 40, 0.89)' },
  17. { index: 2, label: 'Max', value: 100, canRemove: false },
  18. ];
  19. describe('Add threshold', () => {
  20. it('should add threshold between min and max', () => {
  21. const instance = setup();
  22. instance.onAddThreshold(1);
  23. expect(instance.state.thresholds).toEqual([
  24. { index: 0, label: 'Min', value: 0, canRemove: false, color: 'rgba(50, 172, 45, 0.97)' },
  25. { index: 1, label: '', value: 50, canRemove: true, color: 'rgba(237, 129, 40, 0.89)' },
  26. { index: 2, label: 'Max', value: 100, canRemove: false },
  27. ]);
  28. });
  29. it('should add threshold between min and added threshold', () => {
  30. const instance = setup({
  31. options: { thresholds: thresholds },
  32. });
  33. instance.onAddThreshold(1);
  34. expect(instance.state.thresholds).toEqual([
  35. { index: 0, label: 'Min', value: 0, canRemove: false, color: 'rgba(50, 172, 45, 0.97)' },
  36. { index: 1, label: '', value: 25, canRemove: true, color: 'rgba(237, 129, 40, 0.89)' },
  37. { index: 2, label: '', value: 50, canRemove: true, color: 'rgba(237, 129, 40, 0.89)' },
  38. { index: 3, label: 'Max', value: 100, canRemove: false },
  39. ]);
  40. });
  41. });
  42. describe('Add at index', () => {
  43. it('should return 1, no added thresholds', () => {
  44. const instance = setup();
  45. const result = instance.insertAtIndex(1);
  46. expect(result).toEqual(1);
  47. });
  48. it('should return 1, one added threshold', () => {
  49. const instance = setup();
  50. instance.state = {
  51. thresholds: [
  52. { index: 0, label: 'Min', value: 0, canRemove: false },
  53. { index: 1, label: '', value: 50, canRemove: true },
  54. { index: 2, label: 'Max', value: 100, canRemove: false },
  55. ],
  56. };
  57. const result = instance.insertAtIndex(1);
  58. expect(result).toEqual(1);
  59. });
  60. it('should return 2, two added thresholds', () => {
  61. const instance = setup({
  62. options: {
  63. thresholds: [
  64. { index: 0, label: 'Min', value: 0, canRemove: false },
  65. { index: 1, label: '', value: 25, canRemove: true },
  66. { index: 2, label: '', value: 50, canRemove: true },
  67. { index: 3, label: 'Max', value: 100, canRemove: false },
  68. ],
  69. },
  70. });
  71. const result = instance.insertAtIndex(2);
  72. expect(result).toEqual(2);
  73. });
  74. it('should return 2, one added threshold', () => {
  75. const instance = setup();
  76. instance.state = {
  77. thresholds: [
  78. { index: 0, label: 'Min', value: 0, canRemove: false },
  79. { index: 1, label: '', value: 50, canRemove: true },
  80. { index: 2, label: 'Max', value: 100, canRemove: false },
  81. ],
  82. };
  83. const result = instance.insertAtIndex(2);
  84. expect(result).toEqual(2);
  85. });
  86. });
  87. describe('change threshold value', () => {
  88. it('should update value and resort rows', () => {
  89. const instance = setup();
  90. const mockThresholds = [
  91. { index: 0, label: 'Min', value: 0, canRemove: false, color: 'rgba(50, 172, 45, 0.97)' },
  92. { index: 1, label: '', value: 50, canRemove: true, color: 'rgba(237, 129, 40, 0.89)' },
  93. { index: 2, label: '', value: 75, canRemove: true, color: 'rgba(237, 129, 40, 0.89)' },
  94. { index: 3, label: 'Max', value: 100, canRemove: false },
  95. ];
  96. instance.state = {
  97. thresholds: mockThresholds,
  98. };
  99. const mockEvent = { target: { value: 78 } };
  100. instance.onChangeThresholdValue(mockEvent, mockThresholds[1]);
  101. expect(instance.state.thresholds).toEqual([
  102. { index: 0, label: 'Min', value: 0, canRemove: false, color: 'rgba(50, 172, 45, 0.97)' },
  103. { index: 1, label: '', value: 78, canRemove: true, color: 'rgba(237, 129, 40, 0.89)' },
  104. { index: 2, label: '', value: 75, canRemove: true, color: 'rgba(237, 129, 40, 0.89)' },
  105. { index: 3, label: 'Max', value: 100, canRemove: false },
  106. ]);
  107. });
  108. });