index.spec.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import {GroupsFixture} from 'sentry-fixture/groups';
  2. import {ProjectFixture} from 'sentry-fixture/project';
  3. import {RouterContextFixture} from 'sentry-fixture/routerContextFixture';
  4. import {RouterFixture} from 'sentry-fixture/routerFixture';
  5. import {
  6. render,
  7. renderGlobalModal,
  8. screen,
  9. userEvent,
  10. waitFor,
  11. } from 'sentry-test/reactTestingLibrary';
  12. import GroupSimilarIssues from 'sentry/views/issueDetails/groupSimilarIssues';
  13. const MockNavigate = jest.fn();
  14. jest.mock('sentry/utils/useNavigate', () => ({
  15. useNavigate: () => MockNavigate,
  16. }));
  17. describe('Issues Similar View', function () {
  18. let mock;
  19. const project = ProjectFixture({
  20. features: ['similarity-view'],
  21. });
  22. const routerContext = RouterContextFixture([
  23. {
  24. router: {
  25. ...RouterFixture(),
  26. params: {orgId: 'org-slug', projectId: 'project-slug', groupId: 'group-id'},
  27. },
  28. },
  29. ]);
  30. const scores = [
  31. {'exception:stacktrace:pairs': 0.375},
  32. {'exception:stacktrace:pairs': 0.01264},
  33. {'exception:stacktrace:pairs': 0.875},
  34. {'exception:stacktrace:pairs': 0.001488},
  35. ];
  36. const mockData = {
  37. similar: GroupsFixture().map((issue, i) => [issue, scores[i]]),
  38. };
  39. const router = RouterFixture();
  40. beforeEach(function () {
  41. mock = MockApiClient.addMockResponse({
  42. url: '/organizations/org-slug/issues/group-id/similar/?limit=50',
  43. body: mockData.similar,
  44. });
  45. });
  46. afterEach(() => {
  47. MockApiClient.clearMockResponses();
  48. jest.clearAllMocks();
  49. });
  50. it('renders with mocked data', async function () {
  51. render(
  52. <GroupSimilarIssues
  53. project={project}
  54. params={{orgId: 'org-slug', groupId: 'group-id'}}
  55. location={router.location}
  56. router={router}
  57. routeParams={router.params}
  58. routes={router.routes}
  59. route={{}}
  60. />,
  61. {context: routerContext}
  62. );
  63. expect(screen.getByTestId('loading-indicator')).toBeInTheDocument();
  64. await waitFor(() => expect(mock).toHaveBeenCalled());
  65. });
  66. it('can merge and redirect to new parent', async function () {
  67. const merge = MockApiClient.addMockResponse({
  68. method: 'PUT',
  69. url: '/projects/org-slug/project-slug/issues/',
  70. body: {
  71. merge: {children: ['123'], parent: '321'},
  72. },
  73. });
  74. render(
  75. <GroupSimilarIssues
  76. project={project}
  77. params={{orgId: 'org-slug', groupId: 'group-id'}}
  78. location={router.location}
  79. router={router}
  80. routeParams={router.params}
  81. routes={router.routes}
  82. route={{}}
  83. />,
  84. {context: routerContext}
  85. );
  86. renderGlobalModal();
  87. await userEvent.click(await screen.findByTestId('similar-item-row'));
  88. await userEvent.click(await screen.findByRole('button', {name: 'Merge (1)'}));
  89. await userEvent.click(screen.getByRole('button', {name: 'Confirm'}));
  90. await waitFor(() => {
  91. expect(merge).toHaveBeenCalledWith(
  92. '/projects/org-slug/project-slug/issues/',
  93. expect.objectContaining({
  94. data: {merge: 1},
  95. })
  96. );
  97. });
  98. expect(MockNavigate).toHaveBeenCalledWith(
  99. '/organizations/org-slug/issues/321/similar/'
  100. );
  101. });
  102. it('correctly shows merge count', async function () {
  103. render(
  104. <GroupSimilarIssues
  105. project={project}
  106. params={{orgId: 'org-slug', groupId: 'group-id'}}
  107. location={router.location}
  108. router={router}
  109. routeParams={router.params}
  110. routes={router.routes}
  111. route={{}}
  112. />,
  113. {context: routerContext}
  114. );
  115. renderGlobalModal();
  116. await userEvent.click(await screen.findByTestId('similar-item-row'));
  117. expect(screen.getByText('Merge (1)')).toBeInTheDocument();
  118. // Correctly show "Merge (0)" when the item is un-clicked
  119. await userEvent.click(await screen.findByTestId('similar-item-row'));
  120. expect(screen.getByText('Merge (0)')).toBeInTheDocument();
  121. });
  122. });