histogramQuery.spec.jsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import {Fragment} from 'react';
  2. import {mountWithTheme} from 'sentry-test/enzyme';
  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. const wrapper = mountWithTheme(
  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. await tick();
  69. wrapper.update();
  70. expect(getMock).toHaveBeenCalledTimes(1);
  71. expect(wrapper.find('p')).toHaveLength(1);
  72. expect(wrapper.find('li')).toHaveLength(10);
  73. });
  74. });