sampleImages.spec.tsx 4.2 KB

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