teamMisery.spec.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import range from 'lodash/range';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import {
  4. render,
  5. screen,
  6. userEvent,
  7. waitForElementToBeRemoved,
  8. } from 'sentry-test/reactTestingLibrary';
  9. import TeamMisery from 'sentry/views/organizationStats/teamInsights/teamMisery';
  10. describe('TeamMisery', () => {
  11. const {routerProps} = initializeOrg();
  12. it('should render misery from projects and expand hidden items', async () => {
  13. const project = TestStubs.Project();
  14. const meta = {
  15. fields: {
  16. transaction: 'string',
  17. project: 'string',
  18. tpm: 'number',
  19. 'count_unique(user)': 'number',
  20. 'count_miserable(user)': 'number',
  21. 'user_misery()': 'number',
  22. },
  23. };
  24. const extraData = {
  25. project: project.slug,
  26. tpm: 30,
  27. count_unique_user: 1000,
  28. count_miserable_user: 122,
  29. project_threshold_config: ['duration', 300],
  30. };
  31. const noChangeItems = 10;
  32. const noChange = range(0, noChangeItems).map(x => ({
  33. transaction: `/apple/${x}`,
  34. 'user_misery()': 0.1,
  35. ...extraData,
  36. }));
  37. const weekMisery = MockApiClient.addMockResponse({
  38. url: `/organizations/org-slug/events/`,
  39. body: {
  40. meta,
  41. data: [
  42. {
  43. transaction: '/apple/cart',
  44. 'user_misery()': 0.5,
  45. ...extraData,
  46. },
  47. {
  48. transaction: '/apple/checkout',
  49. 'user_misery()': 0.1,
  50. ...extraData,
  51. },
  52. ...noChange,
  53. ],
  54. },
  55. match: [MockApiClient.matchQuery({statsPeriod: '7d'})],
  56. });
  57. const periodMisery = MockApiClient.addMockResponse({
  58. url: `/organizations/org-slug/events/`,
  59. body: {
  60. meta,
  61. data: [
  62. {
  63. transaction: '/apple/cart',
  64. 'user_misery()': 0.25,
  65. ...extraData,
  66. },
  67. {
  68. transaction: '/apple/checkout',
  69. 'user_misery()': 0.2,
  70. ...extraData,
  71. },
  72. ...noChange,
  73. ],
  74. },
  75. match: [MockApiClient.matchQuery({statsPeriod: '8w'})],
  76. });
  77. render(
  78. <TeamMisery
  79. organization={TestStubs.Organization()}
  80. projects={[project]}
  81. period="8w"
  82. teamId="0"
  83. {...routerProps}
  84. />
  85. );
  86. await waitForElementToBeRemoved(() => screen.queryByTestId('loading-indicator'));
  87. expect(weekMisery).toHaveBeenCalledTimes(1);
  88. expect(periodMisery).toHaveBeenCalledTimes(1);
  89. // Should have 8 items, the rest are collapsed.
  90. expect(screen.getAllByText(project.slug)).toHaveLength(5);
  91. expect(screen.getByText('10% better')).toBeInTheDocument();
  92. expect(screen.getByText('25% worse')).toBeInTheDocument();
  93. expect(screen.getAllByText('0% change')).toHaveLength(3);
  94. expect(screen.getByText('Show 7 More')).toBeInTheDocument();
  95. await userEvent.click(screen.getByText('Show 7 More'));
  96. expect(screen.getAllByText('0% change')).toHaveLength(noChangeItems);
  97. });
  98. it('should render empty state', () => {
  99. render(
  100. <TeamMisery
  101. organization={TestStubs.Organization()}
  102. projects={[]}
  103. period="8w"
  104. teamId="0"
  105. {...routerProps}
  106. />
  107. );
  108. expect(
  109. screen.getByText('No key transactions starred by this team')
  110. ).toBeInTheDocument();
  111. });
  112. it('should render empty state on error', async () => {
  113. MockApiClient.addMockResponse({
  114. url: `/organizations/org-slug/events/`,
  115. statusCode: 500,
  116. body: {},
  117. });
  118. render(
  119. <TeamMisery
  120. organization={TestStubs.Organization()}
  121. projects={[TestStubs.Project()]}
  122. period="8w"
  123. teamId="0"
  124. {...routerProps}
  125. />
  126. );
  127. await waitForElementToBeRemoved(screen.queryByTestId('loading-indicator'));
  128. expect(screen.getByText('There was an error loading data.')).toBeInTheDocument();
  129. });
  130. });