getParams.spec.jsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import {
  2. getParams,
  3. parseStatsPeriod,
  4. } from 'app/components/organizations/globalSelectionHeader/getParams';
  5. describe('getParams', function () {
  6. it('should return default statsPeriod if it is not provided or is invalid', function () {
  7. expect(getParams({})).toEqual({statsPeriod: '14d'});
  8. expect(getParams({statsPeriod: 'invalid'})).toEqual({statsPeriod: '14d'});
  9. expect(getParams({statsPeriod: null})).toEqual({statsPeriod: '14d'});
  10. expect(getParams({statsPeriod: undefined})).toEqual({statsPeriod: '14d'});
  11. expect(getParams({statsPeriod: '24f'})).toEqual({statsPeriod: '14d'});
  12. expect(getParams({statsPeriod: '24'})).toEqual({statsPeriod: '24s'});
  13. });
  14. it('should parse statsPeriod', function () {
  15. expect(getParams({statsPeriod: '5s'})).toEqual({statsPeriod: '5s'});
  16. expect(getParams({statsPeriod: '11h'})).toEqual({statsPeriod: '11h'});
  17. expect(getParams({statsPeriod: '14d'})).toEqual({statsPeriod: '14d'});
  18. expect(getParams({statsPeriod: '24w'})).toEqual({statsPeriod: '24w'});
  19. expect(getParams({statsPeriod: '42m'})).toEqual({statsPeriod: '42m'});
  20. });
  21. it('should parse first valid statsPeriod', function () {
  22. expect(getParams({statsPeriod: ['invalid', '24d', '5s']})).toEqual({
  23. statsPeriod: '24d',
  24. });
  25. });
  26. it('should return statsPeriod if statsPeriod, start, and end are provided', function () {
  27. expect(
  28. getParams({
  29. start: '2019-10-01T00:00:00',
  30. end: '2019-10-02T00:00:00',
  31. statsPeriod: '55d',
  32. period: '90d',
  33. })
  34. ).toEqual({statsPeriod: '55d'});
  35. expect(
  36. getParams({
  37. start: '2019-10-01T00:00:00',
  38. end: '2019-10-02T00:00:00',
  39. statsPeriod: '55d',
  40. })
  41. ).toEqual({statsPeriod: '55d'});
  42. expect(
  43. getParams({
  44. start: '2019-10-01T00:00:00',
  45. end: '2019-10-02T00:00:00',
  46. period: '55d',
  47. })
  48. ).toEqual({statsPeriod: '55d'});
  49. });
  50. it('should parse start and end', function () {
  51. expect(getParams({start: '2019-10-01T00:00:00', end: '2019-10-02T00:00:00'})).toEqual(
  52. {start: '2019-10-01T00:00:00.000', end: '2019-10-02T00:00:00.000'}
  53. );
  54. expect(
  55. getParams({start: '2019-10-23T04:28:49+0000', end: '2019-10-26T02:56:17+0000'})
  56. ).toEqual({start: '2019-10-23T04:28:49.000', end: '2019-10-26T02:56:17.000'});
  57. });
  58. it('should parse first valid start and end', function () {
  59. expect(
  60. getParams({
  61. start: ['invalid', '2019-10-01T00:00:00', '2020-10-01T00:00:00'],
  62. end: ['invalid', '2019-10-02T00:00:00', '2020-10-02T00:00:00'],
  63. })
  64. ).toEqual({start: '2019-10-01T00:00:00.000', end: '2019-10-02T00:00:00.000'});
  65. });
  66. it('should return default statsPeriod if both start and end are not provided, or either are invalid', function () {
  67. expect(getParams({start: '2019-10-01T00:00:00'})).toEqual({
  68. statsPeriod: '14d',
  69. });
  70. expect(getParams({start: null})).toEqual({
  71. statsPeriod: '14d',
  72. });
  73. expect(getParams({start: undefined})).toEqual({
  74. statsPeriod: '14d',
  75. });
  76. expect(getParams({end: '2019-10-01T00:00:00'})).toEqual({
  77. statsPeriod: '14d',
  78. });
  79. expect(getParams({end: null})).toEqual({
  80. statsPeriod: '14d',
  81. });
  82. expect(getParams({end: undefined})).toEqual({
  83. statsPeriod: '14d',
  84. });
  85. expect(getParams({start: undefined, end: undefined})).toEqual({
  86. statsPeriod: '14d',
  87. });
  88. expect(getParams({start: null, end: undefined})).toEqual({
  89. statsPeriod: '14d',
  90. });
  91. expect(getParams({start: undefined, end: null})).toEqual({
  92. statsPeriod: '14d',
  93. });
  94. expect(getParams({start: null, end: null})).toEqual({
  95. statsPeriod: '14d',
  96. });
  97. expect(
  98. getParams({
  99. start: ['invalid'],
  100. end: ['invalid'],
  101. })
  102. ).toEqual({statsPeriod: '14d'});
  103. expect(
  104. getParams({
  105. start: ['invalid'],
  106. end: ['invalid', '2019-10-02T00:00:00', '2020-10-02T00:00:00'],
  107. })
  108. ).toEqual({statsPeriod: '14d'});
  109. expect(
  110. getParams({
  111. start: ['invalid', '2019-10-01T00:00:00', '2020-10-01T00:00:00'],
  112. end: ['invalid'],
  113. })
  114. ).toEqual({statsPeriod: '14d'});
  115. });
  116. it('should use pageStart/pageEnd/pageUtc to override start/end/utc', function () {
  117. expect(
  118. getParams(
  119. {
  120. pageStart: '2021-10-23T04:28:49+0000',
  121. pageEnd: '2021-10-26T02:56:17+0000',
  122. pageUtc: 'true',
  123. start: '2019-10-23T04:28:49+0000',
  124. end: '2019-10-26T02:56:17+0000',
  125. utc: 'false',
  126. },
  127. {allowAbsolutePageDatetime: true}
  128. )
  129. ).toEqual({
  130. start: '2021-10-23T04:28:49.000',
  131. end: '2021-10-26T02:56:17.000',
  132. utc: 'true',
  133. });
  134. });
  135. it('should use pageStatsPeriod to override statsPeriod', function () {
  136. expect(
  137. getParams({
  138. pageStart: '2021-10-23T04:28:49+0000',
  139. pageEnd: '2021-10-26T02:56:17+0000',
  140. pageUtc: 'true',
  141. pageStatsPeriod: '90d',
  142. start: '2019-10-23T04:28:49+0000',
  143. end: '2019-10-26T02:56:17+0000',
  144. utc: 'false',
  145. statsPeriod: '14d',
  146. })
  147. ).toEqual({
  148. utc: 'true',
  149. statsPeriod: '90d',
  150. });
  151. });
  152. it('does not return default statsPeriod if `allowEmptyPeriod` option is passed', function () {
  153. expect(getParams({}, {allowEmptyPeriod: true})).toEqual({});
  154. });
  155. it('should parse utc when it is defined', function () {
  156. expect(getParams({utc: 'true'})).toEqual({utc: 'true', statsPeriod: '14d'});
  157. expect(getParams({utc: 'false'})).toEqual({utc: 'false', statsPeriod: '14d'});
  158. expect(getParams({utc: 'invalid'})).toEqual({utc: 'false', statsPeriod: '14d'});
  159. expect(getParams({utc: null})).toEqual({statsPeriod: '14d'});
  160. expect(getParams({utc: undefined})).toEqual({statsPeriod: '14d'});
  161. });
  162. });
  163. describe('parseStatsPeriod', function () {
  164. it('should parse statsPeriod', function () {
  165. expect(parseStatsPeriod('5s')).toEqual({period: '5', periodLength: 's'});
  166. expect(parseStatsPeriod('11h')).toEqual({period: '11', periodLength: 'h'});
  167. expect(parseStatsPeriod('14d')).toEqual({period: '14', periodLength: 'd'});
  168. expect(parseStatsPeriod('24w')).toEqual({period: '24', periodLength: 'w'});
  169. expect(parseStatsPeriod('42m')).toEqual({period: '42', periodLength: 'm'});
  170. });
  171. it('should return default statsPeriod if it is not provided or is invalid', function () {
  172. expect(parseStatsPeriod('invalid')).toEqual(undefined);
  173. expect(parseStatsPeriod('24f')).toEqual(undefined);
  174. expect(parseStatsPeriod('')).toEqual(undefined);
  175. expect(parseStatsPeriod('24')).toEqual({period: '24', periodLength: 's'});
  176. });
  177. it('does not return start and end if `allowAbsoluteDatetime` option is passed', function () {
  178. expect(
  179. getParams(
  180. {start: '2019-10-01T00:00:00', end: '2019-10-02T00:00:00'},
  181. {allowAbsoluteDatetime: false}
  182. )
  183. ).toEqual({statsPeriod: '14d'});
  184. });
  185. });