getNewMetricsWidget.spec.tsx 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import * as Sentry from '@sentry/react';
  2. import type {MetricAggregation, MetricMeta, MetricType} from 'sentry/types/metrics';
  3. import {emptyMetricsQueryWidget} from 'sentry/utils/metrics/constants';
  4. import {getNewMetricsWidget} from 'sentry/views/metrics/utils/getNewMetricsWidget';
  5. describe('getNewMetricsWidget', () => {
  6. it('creates a default widget', () => {
  7. expect(getNewMetricsWidget()).toEqual(emptyMetricsQueryWidget);
  8. });
  9. it('creates a widget for a count metric', () => {
  10. const meta = createMeta('c');
  11. expect(getNewMetricsWidget(meta)).toEqual({
  12. ...emptyMetricsQueryWidget,
  13. mri: meta.mri,
  14. aggregation: 'sum',
  15. });
  16. });
  17. it('creates a widget for a set metric', () => {
  18. const meta = createMeta('s');
  19. expect(getNewMetricsWidget(meta)).toEqual({
  20. ...emptyMetricsQueryWidget,
  21. mri: meta.mri,
  22. aggregation: 'count_unique',
  23. });
  24. });
  25. it('creates a widget for a gauge metric', () => {
  26. const meta = createMeta('g');
  27. expect(getNewMetricsWidget(meta)).toEqual({
  28. ...emptyMetricsQueryWidget,
  29. mri: meta.mri,
  30. aggregation: 'avg',
  31. });
  32. });
  33. it('creates a widget for a distribution metric', () => {
  34. const meta = createMeta('d');
  35. expect(getNewMetricsWidget(meta)).toEqual({
  36. ...emptyMetricsQueryWidget,
  37. mri: meta.mri,
  38. aggregation: 'avg',
  39. });
  40. });
  41. it('creates a widget for a virtual metric', () => {
  42. const meta = createMeta('v', ['p99', 'min'] as const);
  43. expect(getNewMetricsWidget(meta, 23)).toEqual({
  44. ...emptyMetricsQueryWidget,
  45. mri: meta.mri,
  46. // Uses the first available aggregation
  47. aggregation: 'p99',
  48. condition: 23,
  49. });
  50. });
  51. it('logs message and falls back to default if there is no condition for virtual metrics', () => {
  52. const sentrySpy = jest.spyOn(Sentry, 'captureMessage');
  53. const meta = createMeta('v', ['p99', 'min'] as const);
  54. expect(getNewMetricsWidget(meta)).toEqual(emptyMetricsQueryWidget);
  55. expect(sentrySpy).toHaveBeenCalledTimes(1);
  56. expect(sentrySpy).toHaveBeenCalledWith(
  57. 'Metrics: Trying to create widget from virtual MRI without condition'
  58. );
  59. });
  60. it('logs message and falls back to default if there is no allowed aggregation', () => {
  61. const sentrySpy = jest.spyOn(Sentry, 'captureMessage');
  62. const meta = createMeta('v', ['max_timestamp' as MetricAggregation] as const);
  63. expect(getNewMetricsWidget(meta, 42)).toEqual(emptyMetricsQueryWidget);
  64. expect(sentrySpy).toHaveBeenCalledTimes(1);
  65. expect(sentrySpy).toHaveBeenCalledWith(
  66. 'Metrics: No allowed aggregations available for virtual metric found'
  67. );
  68. });
  69. });
  70. function createMeta(type: MetricType, operations: MetricAggregation[] = []): MetricMeta {
  71. return {
  72. mri: `${type}:custom/my-metrics@none`,
  73. operations,
  74. blockingStatus: [],
  75. projectIds: [],
  76. type,
  77. unit: 'none',
  78. };
  79. }