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