teamMisery.spec.tsx 3.9 KB

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