projectCharts.spec.tsx 4.1 KB

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