sampleImages.spec.tsx 4.1 KB

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