useReplayTraces.spec.tsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import {OrganizationFixture} from 'sentry-fixture/organization';
  2. import {ReplayRecordFixture} from 'sentry-fixture/replayRecord';
  3. import {makeTestQueryClient} from 'sentry-test/queryClient';
  4. import {renderHook, waitFor} from 'sentry-test/reactTestingLibrary';
  5. import * as useOrganization from 'sentry/utils/useOrganization';
  6. import {useReplayTraces} from './useReplayTraces';
  7. const organization = OrganizationFixture();
  8. const queryClient = makeTestQueryClient();
  9. const replayRecord = ReplayRecordFixture();
  10. describe('useTraceMeta', () => {
  11. beforeEach(function () {
  12. queryClient.clear();
  13. jest.clearAllMocks();
  14. jest.spyOn(useOrganization, 'default').mockReturnValue(organization);
  15. });
  16. it('Returns replay traces', async () => {
  17. const pageLinks =
  18. '<https://sentry.io/fake/previous>; rel="previous"; results="false"; cursor="0:0:1", ' +
  19. '<https://sentry.io/fake/next>; rel="next"; results="false"; cursor="0:20:0"';
  20. // Mock the API calls
  21. MockApiClient.addMockResponse({
  22. method: 'GET',
  23. headers: {Link: pageLinks},
  24. url: '/organizations/org-slug/events/',
  25. body: {
  26. data: [
  27. {
  28. trace: 'trace1',
  29. 'min(timestamp)': 1,
  30. },
  31. {
  32. trace: 'trace2',
  33. 'min(timestamp)': 2,
  34. },
  35. ],
  36. },
  37. });
  38. const {result} = renderHook(() => useReplayTraces({replayRecord}));
  39. expect(result.current.indexComplete).toEqual(false);
  40. await waitFor(() => expect(result.current.indexComplete).toBe(true));
  41. expect(result.current.indexComplete).toEqual(true);
  42. expect(result.current.replayTraces).toEqual([
  43. {traceSlug: 'trace1', timestamp: 1},
  44. {traceSlug: 'trace2', timestamp: 2},
  45. ]);
  46. });
  47. it('Collects errors', async () => {
  48. const pageLinks =
  49. '<https://sentry.io/fake/previous>; rel="previous"; results="false"; cursor="0:0:1", ' +
  50. '<https://sentry.io/fake/next>; rel="next"; results="false"; cursor="0:20:0"';
  51. // Mock the API calls
  52. const mockedResponse = MockApiClient.addMockResponse({
  53. method: 'GET',
  54. headers: {Link: pageLinks},
  55. url: '/organizations/org-slug/events/',
  56. statusCode: 400,
  57. });
  58. const {result} = renderHook(() => useReplayTraces({replayRecord}));
  59. expect(result.current.indexComplete).toEqual(false);
  60. await waitFor(() => expect(result.current.indexComplete).toBe(true));
  61. expect(result.current.indexComplete).toEqual(true);
  62. expect(result.current.replayTraces).toEqual(undefined);
  63. expect(result.current.indexError as Error).toEqual(
  64. expect.objectContaining({status: 400})
  65. );
  66. expect(mockedResponse).toHaveBeenCalledTimes(1);
  67. });
  68. });