evidencePreview.spec.tsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  2. import * as useApi from 'sentry/utils/useApi';
  3. import {EvidencePreview} from './evidencePreview';
  4. describe('EvidencePreview', () => {
  5. beforeEach(() => {
  6. jest.useFakeTimers();
  7. jest.restoreAllMocks();
  8. MockApiClient.clearMockResponses();
  9. MockApiClient.addMockResponse({
  10. url: '/organizations/org-slug/issues/group-id/',
  11. });
  12. });
  13. it('does not fetch before hover', () => {
  14. const api = new MockApiClient();
  15. jest.spyOn(useApi, 'default').mockReturnValue(api);
  16. const spy = jest.spyOn(api, 'requestPromise');
  17. render(<EvidencePreview groupId="group-id">Hover me</EvidencePreview>);
  18. jest.runAllTimers();
  19. expect(spy).not.toHaveBeenCalled();
  20. });
  21. it('shows error when request fails', async () => {
  22. const api = new MockApiClient();
  23. jest.spyOn(useApi, 'default').mockReturnValue(api);
  24. jest.spyOn(api, 'requestPromise').mockRejectedValue(new Error());
  25. render(<EvidencePreview groupId="group-id">Hover me</EvidencePreview>);
  26. await userEvent.hover(screen.getByText('Hover me'), {delay: null});
  27. await screen.findByText('Failed to load preview');
  28. });
  29. it('renders the span evidence correctly when request succeeds', async () => {
  30. const event = TestStubs.Event({
  31. occurrence: {
  32. evidenceDisplay: [
  33. {name: 'Transaction', value: '/api/0/transaction-test-endpoint/'},
  34. {name: 'Parent Span', value: 'db - connect'},
  35. {name: 'Repeating Span', value: 'db - group me'},
  36. ],
  37. },
  38. });
  39. MockApiClient.addMockResponse({
  40. url: `/organizations/org-slug/issues/group-id/events/recommended/`,
  41. body: event,
  42. });
  43. render(<EvidencePreview groupId="group-id">Hover me</EvidencePreview>);
  44. await userEvent.hover(screen.getByText('Hover me'), {delay: null});
  45. await screen.findByTestId('evidence-preview-body');
  46. expect(screen.getByRole('cell', {name: 'Transaction'})).toBeInTheDocument();
  47. expect(
  48. screen.getByRole('cell', {name: '/api/0/transaction-test-endpoint/'})
  49. ).toBeInTheDocument();
  50. expect(screen.getByRole('cell', {name: 'Parent Span'})).toBeInTheDocument();
  51. expect(screen.getByRole('cell', {name: 'db - connect'})).toBeInTheDocument();
  52. expect(screen.getByRole('cell', {name: 'Repeating Span'})).toBeInTheDocument();
  53. expect(screen.getByRole('cell', {name: 'db - group me'})).toBeInTheDocument();
  54. });
  55. });