histogramQuery.spec.jsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import {Fragment} from 'react';
  2. import {render, screen} from 'sentry-test/reactTestingLibrary';
  3. import {Client} from 'sentry/api';
  4. import EventView from 'sentry/utils/discover/eventView';
  5. import HistogramQuery from 'sentry/utils/performance/histogram/histogramQuery';
  6. function renderHistogram({isLoading, error, histograms}) {
  7. if (isLoading) {
  8. return 'loading';
  9. }
  10. if (error !== null) {
  11. return 'error';
  12. }
  13. return (
  14. <Fragment>
  15. {Object.keys(histograms).map(name => (
  16. <Fragment key={name}>
  17. <p>{name}</p>
  18. <ul>
  19. {histograms[name].map(bar => (
  20. <li key={bar.bin}>{`${bar.bin} - ${bar.count}`}</li>
  21. ))}
  22. </ul>
  23. </Fragment>
  24. ))}
  25. </Fragment>
  26. );
  27. }
  28. describe('HistogramQuery', function () {
  29. let api, eventView, location;
  30. beforeEach(function () {
  31. api = new Client();
  32. location = {
  33. pathname: '/',
  34. query: {},
  35. };
  36. eventView = EventView.fromSavedQuery({
  37. id: '',
  38. name: '',
  39. version: 2,
  40. fields: ['id'],
  41. projects: [],
  42. environment: ['dev'],
  43. });
  44. });
  45. it('fetches data on mount', async function () {
  46. const getMock = MockApiClient.addMockResponse({
  47. url: '/organizations/test-org/events-histogram/',
  48. body: {
  49. 'measurements.fp': Array(10)
  50. .fill(null)
  51. .map((_, i) => ({bin: i * 1000, count: i})),
  52. },
  53. });
  54. render(
  55. <HistogramQuery
  56. api={api}
  57. location={location}
  58. eventView={eventView}
  59. orgSlug="test-org"
  60. numBuckets={10}
  61. fields={['fp']}
  62. min={0}
  63. max={10000}
  64. >
  65. {renderHistogram}
  66. </HistogramQuery>
  67. );
  68. expect(await screen.findByText('measurements.fp')).toBeInTheDocument();
  69. expect(screen.getAllByRole('listitem')).toHaveLength(10);
  70. expect(getMock).toHaveBeenCalledTimes(1);
  71. });
  72. });