Threshold.test.tsx 4.6 KB

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