metricReadout.spec.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import {render, screen} from 'sentry-test/reactTestingLibrary';
  2. import {DurationUnit, RateUnit, SizeUnit} from 'sentry/utils/discover/fields';
  3. import {MetricReadout} from 'sentry/views/performance/metricReadout';
  4. describe('MetricReadout', function () {
  5. it('shows a loading spinner if data is loading', () => {
  6. render(
  7. <MetricReadout
  8. title="Duration"
  9. unit={DurationUnit.MILLISECOND}
  10. value={undefined}
  11. isLoading
  12. />
  13. );
  14. expect(screen.getByText('Duration')).toBeInTheDocument();
  15. expect(screen.getByTestId('loading-indicator')).toBeInTheDocument();
  16. });
  17. it('shows placeholder text if data is missing', () => {
  18. render(
  19. <MetricReadout title="Duration" unit={DurationUnit.MILLISECOND} value={undefined} />
  20. );
  21. expect(screen.getByRole('heading', {name: 'Duration'})).toBeInTheDocument();
  22. expect(screen.getByText('--')).toBeInTheDocument();
  23. });
  24. it('parses strings', () => {
  25. render(<MetricReadout title="Rate" unit={RateUnit.PER_MINUTE} value={'17.8'} />);
  26. expect(screen.getByRole('heading', {name: 'Rate'})).toBeInTheDocument();
  27. expect(screen.getByText('17.8/min')).toBeInTheDocument();
  28. });
  29. it('renders rates', () => {
  30. render(<MetricReadout title="Rate" unit={RateUnit.PER_MINUTE} value={17.8} />);
  31. expect(screen.getByRole('heading', {name: 'Rate'})).toBeInTheDocument();
  32. expect(screen.getByText('17.8/min')).toBeInTheDocument();
  33. });
  34. it('limits smallest rate', () => {
  35. render(<MetricReadout title="Rate" unit={RateUnit.PER_MINUTE} value={0.0002441} />);
  36. expect(screen.getByRole('heading', {name: 'Rate'})).toBeInTheDocument();
  37. expect(screen.getByText('<0.01/min')).toBeInTheDocument();
  38. });
  39. it('renders milliseconds', () => {
  40. render(
  41. <MetricReadout title="Duration" unit={DurationUnit.MILLISECOND} value={223142123} />
  42. );
  43. expect(screen.getByRole('heading', {name: 'Duration'})).toBeInTheDocument();
  44. expect(screen.getByText('2.58d')).toBeInTheDocument();
  45. });
  46. it('renders bytes', () => {
  47. render(<MetricReadout title="Size" unit={SizeUnit.BYTE} value={1172316} />);
  48. expect(screen.getByRole('heading', {name: 'Size'})).toBeInTheDocument();
  49. expect(screen.getByText('1.1 MiB')).toBeInTheDocument();
  50. });
  51. it('renders percentages', () => {
  52. render(<MetricReadout title="Percentage" unit="percentage" value={0.2352} />);
  53. expect(screen.getByRole('heading', {name: 'Percentage'})).toBeInTheDocument();
  54. expect(screen.getByText('23.52%')).toBeInTheDocument();
  55. });
  56. it('limits smallest percentage', () => {
  57. render(<MetricReadout title="Percentage" unit="percentage" value={0.000022317} />);
  58. expect(screen.getByRole('heading', {name: 'Percentage'})).toBeInTheDocument();
  59. expect(screen.getByText('<0.01%')).toBeInTheDocument();
  60. });
  61. describe('percent_change', () => {
  62. it('renders negative percent change', () => {
  63. render(
  64. <MetricReadout title="% Difference" unit="percent_change" value={-0.2352} />
  65. );
  66. expect(screen.getByRole('heading', {name: '% Difference'})).toBeInTheDocument();
  67. expect(screen.getByText('-23.52%')).toBeInTheDocument();
  68. });
  69. it('renders positive percent change', () => {
  70. render(<MetricReadout title="% Difference" unit="percent_change" value={0.0552} />);
  71. expect(screen.getByRole('heading', {name: '% Difference'})).toBeInTheDocument();
  72. expect(screen.getByText('+5.52%')).toBeInTheDocument();
  73. });
  74. it('respects preferred negative polarity', () => {
  75. render(
  76. <MetricReadout
  77. title="% Difference"
  78. unit="percent_change"
  79. value={0.0552}
  80. preferredPolarity="-"
  81. />
  82. );
  83. expect(screen.getByText('+5.52%')).toHaveAttribute('data-rating', 'bad');
  84. });
  85. it('respects preferred default polarity', () => {
  86. render(<MetricReadout title="% Difference" unit="percent_change" value={0.0552} />);
  87. expect(screen.getByText('+5.52%')).toHaveAttribute('data-rating', 'good');
  88. });
  89. });
  90. it('renders counts', () => {
  91. render(<MetricReadout title="Count" unit="count" value={7800123} />);
  92. expect(screen.getByRole('heading', {name: 'Count'})).toBeInTheDocument();
  93. expect(screen.getByText('7.8m')).toBeInTheDocument();
  94. });
  95. });