sampleImages.spec.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 {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 {SpanMetricsField} from 'sentry/views/starfish/types';
  11. const {SPAN_GROUP, HTTP_RESPONSE_CONTENT_LENGTH, SPAN_DESCRIPTION} = SpanMetricsField;
  12. jest.mock('sentry/utils/useLocation');
  13. jest.mock('sentry/utils/usePageFilters');
  14. jest.mock('sentry/utils/useOrganization');
  15. describe('SampleImages', function () {
  16. const organization = OrganizationFixture({
  17. features: [
  18. 'starfish-browser-resource-module-ui',
  19. 'starfish-view',
  20. 'performance-database-view',
  21. ],
  22. });
  23. beforeEach(() => {
  24. setupMocks(organization);
  25. });
  26. afterEach(function () {
  27. jest.resetAllMocks();
  28. });
  29. describe('When project setting is enabled', () => {
  30. beforeEach(() => {
  31. setupMockRequests(organization, {enableImages: true});
  32. });
  33. it('should render images', async () => {
  34. render(<SampleImages groupId="group123" projectId={2} />);
  35. await waitForElementToBeRemoved(() => screen.queryAllByTestId('loading-indicator'));
  36. expect(screen.queryByTestId('sample-image')).toHaveAttribute(
  37. 'src',
  38. 'https://cdn.com/image.png'
  39. );
  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. },
  104. ],
  105. },
  106. });
  107. MockApiClient.addMockResponse({
  108. url: `/api/0/projects/org-slug/project-slug/performance/configure/`,
  109. method: 'GET',
  110. body: {
  111. enable_images: enableImages,
  112. },
  113. });
  114. };