metrics.spec.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import {doMetricsRequest} from 'sentry/actionCreators/metrics';
  2. import {Client} from 'sentry/api';
  3. import {SessionMetric} from 'sentry/utils/metrics/fields';
  4. describe('Metrics ActionCreator', function () {
  5. const api = new Client();
  6. const orgSlug = TestStubs.Organization().slug;
  7. describe('doMetricsRequest', function () {
  8. const options = {
  9. field: [SessionMetric.SESSION],
  10. orgSlug,
  11. cursor: undefined,
  12. environment: [],
  13. groupBy: ['session.status'],
  14. interval: '1h',
  15. limit: 5,
  16. orderBy: SessionMetric.SESSION,
  17. project: [TestStubs.Project().id],
  18. query: 'release:123',
  19. statsPeriod: '14d',
  20. };
  21. let mock;
  22. beforeEach(function () {
  23. MockApiClient.clearMockResponses();
  24. mock = MockApiClient.addMockResponse({
  25. url: `/organizations/${orgSlug}/metrics/data/`,
  26. body: {
  27. data: [],
  28. },
  29. });
  30. });
  31. it('requests metrics data', function () {
  32. doMetricsRequest(api, options);
  33. expect(mock).toHaveBeenCalledTimes(1);
  34. expect(mock).toHaveBeenLastCalledWith(
  35. `/organizations/${orgSlug}/metrics/data/`,
  36. expect.objectContaining({
  37. query: {
  38. environment: [],
  39. field: ['sentry.sessions.session'],
  40. groupBy: ['session.status'],
  41. interval: '1h',
  42. orderBy: 'sentry.sessions.session',
  43. per_page: 5,
  44. project: ['2'],
  45. query: 'release:123',
  46. statsPeriod: '14d',
  47. },
  48. })
  49. );
  50. });
  51. it('fills in interval based on start and end', function () {
  52. doMetricsRequest(api, {
  53. ...options,
  54. statsPeriod: undefined,
  55. interval: undefined,
  56. start: '2022-01-01T00:00:00',
  57. end: '2022-03-01T00:00:00',
  58. });
  59. expect(mock).toHaveBeenCalledTimes(1);
  60. expect(mock).toHaveBeenLastCalledWith(
  61. `/organizations/${orgSlug}/metrics/data/`,
  62. expect.objectContaining({
  63. query: {
  64. environment: [],
  65. field: ['sentry.sessions.session'],
  66. groupBy: ['session.status'],
  67. interval: '4h',
  68. orderBy: 'sentry.sessions.session',
  69. per_page: 5,
  70. project: ['2'],
  71. query: 'release:123',
  72. start: '2022-01-01T00:00:00.000',
  73. end: '2022-03-01T00:00:00.000',
  74. },
  75. })
  76. );
  77. });
  78. it('ignores falsy fields and groupBys', function () {
  79. doMetricsRequest(api, {
  80. ...options,
  81. field: [SessionMetric.SESSION, ''],
  82. groupBy: ['session.status', ''],
  83. });
  84. expect(mock).toHaveBeenCalledTimes(1);
  85. expect(mock).toHaveBeenLastCalledWith(
  86. `/organizations/${orgSlug}/metrics/data/`,
  87. expect.objectContaining({
  88. query: expect.objectContaining({
  89. field: ['sentry.sessions.session'],
  90. groupBy: ['session.status'],
  91. }),
  92. })
  93. );
  94. });
  95. });
  96. });