PromSettings.test.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { render, screen } from '@testing-library/react';
  2. import React, { SyntheticEvent } from 'react';
  3. import { SelectableValue } from '@grafana/data';
  4. import { EventsWithValidation } from '@grafana/ui';
  5. import { getValueFromEventItem, promSettingsValidationEvents, PromSettings } from './PromSettings';
  6. import { createDefaultConfigOptions } from './mocks';
  7. describe('PromSettings', () => {
  8. describe('getValueFromEventItem', () => {
  9. describe('when called with undefined', () => {
  10. it('then it should return empty string', () => {
  11. const result = getValueFromEventItem(
  12. undefined as unknown as SyntheticEvent<HTMLInputElement> | SelectableValue<string>
  13. );
  14. expect(result).toEqual('');
  15. });
  16. });
  17. describe('when called with an input event', () => {
  18. it('then it should return value from currentTarget', () => {
  19. const value = 'An input value';
  20. const result = getValueFromEventItem({ currentTarget: { value } });
  21. expect(result).toEqual(value);
  22. });
  23. });
  24. describe('when called with a select event', () => {
  25. it('then it should return value', () => {
  26. const value = 'A select value';
  27. const result = getValueFromEventItem({ value });
  28. expect(result).toEqual(value);
  29. });
  30. });
  31. });
  32. describe('promSettingsValidationEvents', () => {
  33. const validationEvents = promSettingsValidationEvents;
  34. it('should have one event handlers', () => {
  35. expect(Object.keys(validationEvents).length).toEqual(1);
  36. });
  37. it('should have an onBlur handler', () => {
  38. expect(validationEvents.hasOwnProperty(EventsWithValidation.onBlur)).toBe(true);
  39. });
  40. it('should have one rule', () => {
  41. expect(validationEvents[EventsWithValidation.onBlur].length).toEqual(1);
  42. });
  43. describe('when calling the rule with an empty string', () => {
  44. it('then it should return true', () => {
  45. expect(validationEvents[EventsWithValidation.onBlur][0].rule('')).toBe(true);
  46. });
  47. });
  48. it.each`
  49. value | expected
  50. ${'1ms'} | ${true}
  51. ${'1M'} | ${true}
  52. ${'1w'} | ${true}
  53. ${'1d'} | ${true}
  54. ${'1h'} | ${true}
  55. ${'1m'} | ${true}
  56. ${'1s'} | ${true}
  57. ${'1y'} | ${true}
  58. `(
  59. "when calling the rule with correct formatted value: '$value' then result should be '$expected'",
  60. ({ value, expected }) => {
  61. expect(validationEvents[EventsWithValidation.onBlur][0].rule(value)).toBe(expected);
  62. }
  63. );
  64. it.each`
  65. value | expected
  66. ${'1 ms'} | ${false}
  67. ${'1x'} | ${false}
  68. ${' '} | ${false}
  69. ${'w'} | ${false}
  70. ${'1.0s'} | ${false}
  71. `(
  72. "when calling the rule with incorrect formatted value: '$value' then result should be '$expected'",
  73. ({ value, expected }) => {
  74. expect(validationEvents[EventsWithValidation.onBlur][0].rule(value)).toBe(expected);
  75. }
  76. );
  77. });
  78. describe('PromSettings component', () => {
  79. const defaultProps = createDefaultConfigOptions();
  80. it('should show POST httpMethod if no httpMethod', () => {
  81. const options = defaultProps;
  82. options.url = '';
  83. options.jsonData.httpMethod = '';
  84. render(
  85. <div>
  86. <PromSettings onOptionsChange={() => {}} options={options} />
  87. </div>
  88. );
  89. expect(screen.getByText('POST')).toBeInTheDocument();
  90. });
  91. it('should show POST httpMethod if POST httpMethod is configured', () => {
  92. const options = defaultProps;
  93. options.url = 'test_url';
  94. options.jsonData.httpMethod = 'POST';
  95. render(
  96. <div>
  97. <PromSettings onOptionsChange={() => {}} options={options} />
  98. </div>
  99. );
  100. expect(screen.getByText('POST')).toBeInTheDocument();
  101. });
  102. it('should show GET httpMethod if GET httpMethod is configured', () => {
  103. const options = defaultProps;
  104. options.url = 'test_url';
  105. options.jsonData.httpMethod = 'GET';
  106. render(
  107. <div>
  108. <PromSettings onOptionsChange={() => {}} options={options} />
  109. </div>
  110. );
  111. expect(screen.getByText('GET')).toBeInTheDocument();
  112. });
  113. });
  114. });