projectCharts.spec.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import {SessionsField} from 'sentry-fixture/sessions';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  4. import ProjectCharts from 'sentry/views/projectDetail/projectCharts';
  5. function renderProjectCharts(
  6. features?: string[],
  7. platform?: string,
  8. chartDisplay?: string
  9. ) {
  10. const {organization, router, project} = initializeOrg({
  11. organization: TestStubs.Organization({features}),
  12. projects: [{platform}],
  13. router: {
  14. params: {orgId: 'org-slug', projectId: 'project-slug'},
  15. location: {
  16. pathname: '/organizations/org-slug/projects/project-slug/',
  17. query: {chart1: chartDisplay ?? 'crash_free'},
  18. },
  19. },
  20. } as Parameters<typeof initializeOrg>[0]);
  21. return render(
  22. <ProjectCharts
  23. chartId="chart1"
  24. chartIndex={0}
  25. hasSessions
  26. hasTransactions
  27. location={router.location}
  28. organization={organization}
  29. router={router}
  30. visibleCharts={['chart1', 'chart2']}
  31. project={project}
  32. />
  33. );
  34. }
  35. describe('ProjectDetail > ProjectCharts', () => {
  36. let mockSessions;
  37. beforeEach(() => {
  38. MockApiClient.addMockResponse({
  39. url: `/organizations/org-slug/releases/stats/`,
  40. body: [],
  41. });
  42. mockSessions = MockApiClient.addMockResponse({
  43. method: 'GET',
  44. url: '/organizations/org-slug/sessions/',
  45. body: SessionsField(`sum(session)`),
  46. });
  47. });
  48. afterEach(() => {
  49. jest.resetAllMocks();
  50. });
  51. it('renders ANR options', async () => {
  52. renderProjectCharts(['anr-rate'], 'android');
  53. await userEvent.click(
  54. screen.getByRole('button', {name: 'Display Crash Free Sessions'})
  55. );
  56. expect(screen.getByText('Foreground ANR Rate')).toBeInTheDocument();
  57. expect(screen.getByText('ANR Rate')).toBeInTheDocument();
  58. });
  59. it('does not render ANR options for non-android platforms', async () => {
  60. renderProjectCharts(['anr-rate'], 'python');
  61. await userEvent.click(
  62. screen.getByRole('button', {name: 'Display Crash Free Sessions'})
  63. );
  64. expect(screen.queryByText('Foreground ANR Rate')).not.toBeInTheDocument();
  65. expect(screen.queryByText('ANR Rate')).not.toBeInTheDocument();
  66. });
  67. it('makes the right ANR sessions request', async () => {
  68. const responseBody = {
  69. query: '',
  70. intervals: [
  71. '2021-03-05T00:00:00Z',
  72. '2021-03-06T00:00:00Z',
  73. '2021-03-07T00:00:00Z',
  74. '2021-03-08T00:00:00Z',
  75. '2021-03-09T00:00:00Z',
  76. '2021-03-10T00:00:00Z',
  77. '2021-03-11T00:00:00Z',
  78. '2021-03-12T00:00:00Z',
  79. '2021-03-13T00:00:00Z',
  80. '2021-03-14T00:00:00Z',
  81. '2021-03-15T00:00:00Z',
  82. '2021-03-16T00:00:00Z',
  83. '2021-03-17T00:00:00Z',
  84. '2021-03-18T00:00:00Z',
  85. ],
  86. groups: [
  87. {
  88. by: {},
  89. totals: {
  90. 'anr_rate()': 492,
  91. 'count_unique(user)': 3,
  92. },
  93. series: {
  94. 'anr_rate()': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 490],
  95. 'count_unique(user)': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1],
  96. },
  97. },
  98. ],
  99. };
  100. mockSessions = MockApiClient.addMockResponse({
  101. method: 'GET',
  102. url: '/organizations/org-slug/sessions/',
  103. body: responseBody,
  104. });
  105. renderProjectCharts(['anr-rate'], 'android', 'anr_rate');
  106. expect(screen.getByText('ANR Rate')).toBeInTheDocument();
  107. await waitFor(() =>
  108. expect(mockSessions).toHaveBeenCalledWith(
  109. '/organizations/org-slug/sessions/',
  110. expect.objectContaining({
  111. query: expect.objectContaining({
  112. field: ['anr_rate()', 'count_unique(user)'],
  113. }),
  114. })
  115. )
  116. );
  117. });
  118. });