sampleImages.spec.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import {OrganizationFixture} from 'sentry-fixture/organization';
  2. import {ProjectFixture} from 'sentry-fixture/project';
  3. import {render, screen, waitForElementToBeRemoved} from 'sentry-test/reactTestingLibrary';
  4. import ProjectsStore from 'sentry/stores/projectsStore';
  5. import type {Organization} from 'sentry/types/organization';
  6. import {useLocation} from 'sentry/utils/useLocation';
  7. import usePageFilters from 'sentry/utils/usePageFilters';
  8. import SampleImages from 'sentry/views/insights/browser/resources/components/sampleImages';
  9. import {SpanIndexedField} from 'sentry/views/insights/types';
  10. const {SPAN_GROUP, HTTP_RESPONSE_CONTENT_LENGTH, RAW_DOMAIN, SPAN_DESCRIPTION} =
  11. SpanIndexedField;
  12. jest.mock('sentry/utils/useLocation');
  13. jest.mock('sentry/utils/usePageFilters');
  14. describe('SampleImages', function () {
  15. const organization = OrganizationFixture({
  16. features: ['insights-initial-modules'],
  17. });
  18. beforeEach(() => {
  19. setupMocks();
  20. });
  21. afterEach(function () {
  22. jest.resetAllMocks();
  23. });
  24. describe('When project setting is enabled', () => {
  25. beforeEach(() => {
  26. setupMockRequests(organization, {enableImages: true});
  27. });
  28. it('should render images', async () => {
  29. render(<SampleImages groupId="group123" projectId={2} />, {organization});
  30. await waitForElementToBeRemoved(() => screen.queryAllByTestId('loading-indicator'));
  31. const sampleImages = screen.queryAllByTestId('sample-image');
  32. expect(sampleImages[0]).toHaveAttribute('src', 'https://cdn.com/image.png');
  33. expect(sampleImages[1]).toHaveAttribute('src', 'https://cdn.com/image2.png');
  34. });
  35. });
  36. describe('When project setting is disabled', () => {
  37. beforeEach(() => {
  38. setupMockRequests(organization, {enableImages: false});
  39. });
  40. it('should ask to enable images', async () => {
  41. render(<SampleImages groupId="group123" projectId={2} />, {organization});
  42. await waitForElementToBeRemoved(() => screen.queryAllByTestId('loading-indicator'));
  43. expect(screen.queryByTestId('sample-image')).not.toBeInTheDocument();
  44. expect(screen.queryByTestId('enable-sample-images-button')).toBeInTheDocument();
  45. });
  46. });
  47. });
  48. const setupMocks = () => {
  49. const mockProjects = [ProjectFixture()];
  50. ProjectsStore.loadInitialData(mockProjects);
  51. jest.mocked(usePageFilters).mockReturnValue({
  52. isReady: true,
  53. desyncedFilters: new Set(),
  54. pinnedFilters: new Set(),
  55. shouldPersist: true,
  56. selection: {
  57. datetime: {
  58. period: '10d',
  59. start: null,
  60. end: null,
  61. utc: false,
  62. },
  63. environments: [],
  64. projects: [2],
  65. },
  66. });
  67. jest.mocked(useLocation).mockReturnValue({
  68. pathname: '',
  69. search: '',
  70. query: {statsPeriod: '10d'},
  71. hash: '',
  72. state: undefined,
  73. action: 'PUSH',
  74. key: '',
  75. });
  76. };
  77. const setupMockRequests = (
  78. organization: Organization,
  79. settings: {enableImages: boolean} = {enableImages: true}
  80. ) => {
  81. const {enableImages} = settings;
  82. MockApiClient.addMockResponse({
  83. url: `/organizations/${organization.slug}/events/`,
  84. method: 'GET',
  85. match: [
  86. MockApiClient.matchQuery({referrer: 'api.performance.resources.sample-images'}),
  87. ],
  88. body: {
  89. data: [
  90. {
  91. [SPAN_GROUP]: 'group123',
  92. [`measurements.${HTTP_RESPONSE_CONTENT_LENGTH}`]: 1234,
  93. project: 'javascript',
  94. [SPAN_DESCRIPTION]: 'https://cdn.com/image.png',
  95. 'any(id)': 'anyId123',
  96. [RAW_DOMAIN]: '',
  97. },
  98. {
  99. [SPAN_GROUP]: 'group123',
  100. [`measurements.${HTTP_RESPONSE_CONTENT_LENGTH}`]: 1234,
  101. project: 'javascript',
  102. [SPAN_DESCRIPTION]: '/image2.png',
  103. 'any(id)': 'anyId123',
  104. [RAW_DOMAIN]: 'https://cdn.com',
  105. },
  106. ],
  107. },
  108. });
  109. MockApiClient.addMockResponse({
  110. url: `/projects/org-slug/project-slug/performance/configure/`,
  111. method: 'GET',
  112. body: {
  113. enable_images: enableImages,
  114. },
  115. });
  116. };