useCustomMeasurements.spec.tsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {render, screen} from 'sentry-test/reactTestingLibrary';
  3. import {CustomMeasurementsProvider} from 'sentry/utils/customMeasurements/customMeasurementsProvider';
  4. import useCustomMeasurements from 'sentry/utils/useCustomMeasurements';
  5. function TestComponent({other}: {other: string}) {
  6. const {customMeasurements} = useCustomMeasurements();
  7. return (
  8. <div>
  9. <span>{other}</span>
  10. {customMeasurements &&
  11. Object.keys(customMeasurements).map(customMeasurement => (
  12. <em key={customMeasurement}>{customMeasurement}</em>
  13. ))}
  14. </div>
  15. );
  16. }
  17. function mockMeasurementsMeta() {
  18. return MockApiClient.addMockResponse({
  19. url: `/organizations/org-slug/measurements-meta/`,
  20. body: {
  21. 'measurements.custom.measurement': {
  22. functions: ['p99'],
  23. },
  24. 'measurements.another.custom.measurement': {
  25. functions: ['p99'],
  26. },
  27. },
  28. });
  29. }
  30. describe('useCustomMeasurements', function () {
  31. it('provides customMeasurements from the custom measurements context', async function () {
  32. const {organization} = initializeOrg({
  33. organization: {features: ['dashboard-custom-measurement-widgets']},
  34. project: undefined,
  35. projects: [],
  36. router: {},
  37. });
  38. const measurementsMetaMock = mockMeasurementsMeta();
  39. render(
  40. <CustomMeasurementsProvider organization={organization}>
  41. <TestComponent other="value" />
  42. </CustomMeasurementsProvider>
  43. );
  44. // Should forward prop
  45. expect(screen.getByText('value')).toBeInTheDocument();
  46. expect(measurementsMetaMock).toHaveBeenCalledTimes(1);
  47. // Renders custom measurements
  48. expect(
  49. await screen.findByText('measurements.custom.measurement')
  50. ).toBeInTheDocument();
  51. expect(
  52. screen.getByText('measurements.another.custom.measurement')
  53. ).toBeInTheDocument();
  54. });
  55. });