AutoRefreshIntervals.test.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import { render, screen } from '@testing-library/react';
  2. import userEvent from '@testing-library/user-event';
  3. import React from 'react';
  4. import { defaultIntervals } from '@grafana/ui';
  5. import { TimeSrv } from '../../services/TimeSrv';
  6. import { AutoRefreshIntervals, getValidIntervals, Props, validateIntervals } from './AutoRefreshIntervals';
  7. const setupTestContext = (options: Partial<Props>) => {
  8. const defaults: Props = {
  9. refreshIntervals: ['1s', '5s', '10s'],
  10. onRefreshIntervalChange: jest.fn(),
  11. getIntervalsFunc: (intervals) => intervals,
  12. validateIntervalsFunc: () => null,
  13. };
  14. const props = { ...defaults, ...options };
  15. const { rerender } = render(<AutoRefreshIntervals {...props} />);
  16. return { rerender, props };
  17. };
  18. describe('AutoRefreshIntervals', () => {
  19. describe('when component is mounted with refreshIntervals', () => {
  20. it('then supplied intervals should be shown', () => {
  21. setupTestContext({ getIntervalsFunc: () => ['5s', '10s'] }); // remove 1s entry to validate we're calling getIntervalsFunc
  22. expect(screen.getByRole('textbox')).toHaveValue('5s,10s');
  23. });
  24. });
  25. describe('when component is mounted without refreshIntervals', () => {
  26. it('then default intervals should be shown', () => {
  27. setupTestContext({ refreshIntervals: null as unknown as string[] });
  28. expect(screen.getByRole('textbox')).toHaveValue('5s,10s,30s,1m,5m,15m,30m,1h,2h,1d');
  29. });
  30. });
  31. describe('when component is updated from Angular', () => {
  32. it('then intervals should be updated', () => {
  33. const { rerender, props } = setupTestContext({});
  34. const newProps = { ...props, renderCount: 1, refreshIntervals: ['2s', '6s', '11s'] };
  35. rerender(<AutoRefreshIntervals {...newProps} />);
  36. expect(screen.getByRole('textbox')).toHaveValue('2s,6s,11s');
  37. });
  38. });
  39. describe('when input loses focus and intervals are valid', () => {
  40. it('then onRefreshIntervalChange should be called', async () => {
  41. const { props } = setupTestContext({ validateIntervalsFunc: () => null });
  42. await userEvent.type(screen.getByRole('textbox'), ',30s');
  43. await userEvent.tab();
  44. expect(screen.getByRole('textbox')).toHaveValue('1s,5s,10s,30s');
  45. expect(props.onRefreshIntervalChange).toHaveBeenCalledTimes(1);
  46. expect(props.onRefreshIntervalChange).toHaveBeenCalledWith(['1s', '5s', '10s', '30s']);
  47. });
  48. });
  49. describe('when input loses focus and intervals are invalid', () => {
  50. it('then onRefreshIntervalChange should not be called', async () => {
  51. const { props } = setupTestContext({ validateIntervalsFunc: () => 'Not valid' });
  52. await userEvent.type(screen.getByRole('textbox'), ',30q');
  53. await userEvent.tab();
  54. expect(screen.getByRole('textbox')).toHaveValue('1s,5s,10s,30q');
  55. expect(props.onRefreshIntervalChange).toHaveBeenCalledTimes(0);
  56. });
  57. });
  58. describe('when input loses focus and previous intervals were invalid', () => {
  59. it('then onRefreshIntervalChange should be called', async () => {
  60. const validateIntervalsFunc = jest.fn().mockReturnValueOnce('Not valid').mockReturnValue(null);
  61. const { props } = setupTestContext({ validateIntervalsFunc });
  62. await userEvent.type(screen.getByRole('textbox'), ',30q');
  63. await userEvent.tab();
  64. await userEvent.type(screen.getByRole('textbox'), '{backspace}s');
  65. await userEvent.tab();
  66. expect(screen.getByRole('textbox')).toHaveValue('1s,5s,10s,30s');
  67. expect(props.onRefreshIntervalChange).toHaveBeenCalledTimes(1);
  68. expect(props.onRefreshIntervalChange).toHaveBeenCalledWith(['1s', '5s', '10s', '30s']);
  69. });
  70. });
  71. });
  72. describe('getValidIntervals', () => {
  73. describe('when called with empty intervals', () => {
  74. it('then is should all non empty intervals', () => {
  75. const emptyIntervals = ['', '5s', ' ', '10s', ' '];
  76. const dependencies = {
  77. getTimeSrv: () =>
  78. ({
  79. getValidIntervals: (intervals: any) => intervals,
  80. } as unknown as TimeSrv),
  81. };
  82. const result = getValidIntervals(emptyIntervals, dependencies);
  83. expect(result).toEqual(['5s', '10s']);
  84. });
  85. });
  86. describe('when called with duplicate intervals', () => {
  87. it('then is should return no duplicates', () => {
  88. const duplicateIntervals = ['5s', '10s', '1m', '5s', '30s', '10s', '5s', '2m'];
  89. const dependencies = {
  90. getTimeSrv: () =>
  91. ({
  92. getValidIntervals: (intervals: any) => intervals,
  93. } as unknown as TimeSrv),
  94. };
  95. const result = getValidIntervals(duplicateIntervals, dependencies);
  96. expect(result).toEqual(['5s', '10s', '1m', '30s', '2m']);
  97. });
  98. });
  99. describe('when called with untrimmed intervals', () => {
  100. it('then is should return trimmed intervals', () => {
  101. const duplicateIntervals = [' 5s', '10s ', ' 1m ', ' 3 0 s ', ' 2 m '];
  102. const dependencies = {
  103. getTimeSrv: () =>
  104. ({
  105. getValidIntervals: (intervals: any) => intervals,
  106. } as unknown as TimeSrv),
  107. };
  108. const result = getValidIntervals(duplicateIntervals, dependencies);
  109. expect(result).toEqual(['5s', '10s', '1m', '30s', '2m']);
  110. });
  111. });
  112. });
  113. describe('validateIntervals', () => {
  114. describe('when getValidIntervals does not throw', () => {
  115. it('then it should return null', () => {
  116. const dependencies = {
  117. getTimeSrv: () =>
  118. ({
  119. getValidIntervals: (intervals: any) => intervals,
  120. } as unknown as TimeSrv),
  121. };
  122. const result = validateIntervals(defaultIntervals, dependencies);
  123. expect(result).toBe(null);
  124. });
  125. });
  126. describe('when getValidIntervals throws', () => {
  127. it('then it should return the exception message', () => {
  128. const dependencies = {
  129. getTimeSrv: () =>
  130. ({
  131. getValidIntervals: () => {
  132. throw new Error('Some error');
  133. },
  134. } as unknown as TimeSrv),
  135. };
  136. const result = validateIntervals(defaultIntervals, dependencies);
  137. expect(result).toEqual('Some error');
  138. });
  139. });
  140. });