parse.spec.jsx 7.5 KB

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