groupEventCarousel.spec.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import {browserHistory} from 'react-router';
  2. import {render, screen, userEvent, within} from 'sentry-test/reactTestingLibrary';
  3. import ConfigStore from 'sentry/stores/configStore';
  4. import * as useMedia from 'sentry/utils/useMedia';
  5. import {GroupEventCarousel} from 'sentry/views/issueDetails/groupEventCarousel';
  6. describe('GroupEventCarousel', () => {
  7. const testEvent = TestStubs.Event({
  8. id: 'event-id',
  9. size: 7,
  10. dateCreated: '2019-03-20T00:00:00.000Z',
  11. errors: [],
  12. entries: [],
  13. tags: [{key: 'environment', value: 'dev'}],
  14. previousEventID: 'prev-event-id',
  15. nextEventID: 'next-event-id',
  16. });
  17. const singleTestEvent = {...testEvent, previousEventID: null, nextEventID: null};
  18. const defaultProps = {
  19. event: testEvent,
  20. group: TestStubs.Group({id: 'group-id'}),
  21. projectSlug: 'project-slug',
  22. };
  23. const singleEventProps = {...defaultProps, event: singleTestEvent};
  24. beforeEach(() => {
  25. jest.restoreAllMocks();
  26. Object.assign(navigator, {
  27. clipboard: {
  28. writeText: jest.fn().mockResolvedValue(''),
  29. },
  30. });
  31. window.open = jest.fn();
  32. });
  33. describe('recommended event ui', () => {
  34. const orgWithRecommendedEvent = TestStubs.Organization({
  35. features: [
  36. 'issue-details-most-helpful-event',
  37. 'issue-details-most-helpful-event-ui',
  38. ],
  39. });
  40. const recommendedUser = TestStubs.User({
  41. options: {
  42. defaultIssueEvent: 'recommended',
  43. },
  44. });
  45. const latestUser = TestStubs.User({
  46. options: {
  47. defaultIssueEvent: 'latest',
  48. },
  49. });
  50. const oldestUser = TestStubs.User({
  51. options: {
  52. defaultIssueEvent: 'oldest',
  53. },
  54. });
  55. it('can navigate to the oldest event', async () => {
  56. jest.spyOn(useMedia, 'default').mockReturnValue(true);
  57. render(<GroupEventCarousel {...defaultProps} />, {
  58. organization: orgWithRecommendedEvent,
  59. });
  60. await userEvent.click(screen.getByRole('button', {name: /recommended/i}));
  61. await userEvent.click(screen.getByRole('option', {name: /oldest/i}));
  62. expect(browserHistory.push).toHaveBeenCalledWith({
  63. pathname: '/organizations/org-slug/issues/group-id/events/oldest/',
  64. query: {referrer: 'oldest-event'},
  65. });
  66. });
  67. it('can navigate to the latest event', async () => {
  68. jest.spyOn(useMedia, 'default').mockReturnValue(true);
  69. render(<GroupEventCarousel {...defaultProps} />, {
  70. organization: orgWithRecommendedEvent,
  71. });
  72. await userEvent.click(screen.getByRole('button', {name: /recommended/i}));
  73. await userEvent.click(screen.getByRole('option', {name: /latest/i}));
  74. expect(browserHistory.push).toHaveBeenCalledWith({
  75. pathname: '/organizations/org-slug/issues/group-id/events/latest/',
  76. query: {referrer: 'latest-event'},
  77. });
  78. });
  79. it('can navigate to the recommended event', async () => {
  80. jest.spyOn(useMedia, 'default').mockReturnValue(true);
  81. render(<GroupEventCarousel {...defaultProps} />, {
  82. organization: orgWithRecommendedEvent,
  83. router: {
  84. params: {eventId: 'latest'},
  85. },
  86. });
  87. await userEvent.click(screen.getByRole('button', {name: /latest/i}));
  88. await userEvent.click(screen.getByRole('option', {name: /recommended/i}));
  89. expect(browserHistory.push).toHaveBeenCalledWith({
  90. pathname: '/organizations/org-slug/issues/group-id/events/recommended/',
  91. query: {referrer: 'recommended-event'},
  92. });
  93. });
  94. it('will disable the dropdown if there is only one event', async () => {
  95. jest.spyOn(useMedia, 'default').mockReturnValue(true);
  96. render(<GroupEventCarousel {...singleEventProps} />, {
  97. organization: orgWithRecommendedEvent,
  98. });
  99. expect(await screen.getByRole('button', {name: 'Recommended'})).toBeDisabled();
  100. });
  101. it('if user default is recommended, it will show it as default', async () => {
  102. ConfigStore.loadInitialData(TestStubs.Config({user: recommendedUser}));
  103. jest.spyOn(useMedia, 'default').mockReturnValue(true);
  104. render(<GroupEventCarousel {...singleEventProps} />, {
  105. organization: orgWithRecommendedEvent,
  106. });
  107. expect(await screen.getByText('Recommended')).toBeInTheDocument();
  108. });
  109. it('if user default is latest, it will show it as default', async () => {
  110. ConfigStore.loadInitialData(TestStubs.Config({user: latestUser}));
  111. jest.spyOn(useMedia, 'default').mockReturnValue(true);
  112. render(<GroupEventCarousel {...singleEventProps} />, {
  113. organization: orgWithRecommendedEvent,
  114. });
  115. expect(await screen.getByText('Latest')).toBeInTheDocument();
  116. });
  117. it('if user default is oldest, it will show it as default', async () => {
  118. ConfigStore.loadInitialData(TestStubs.Config({user: oldestUser}));
  119. jest.spyOn(useMedia, 'default').mockReturnValue(true);
  120. render(<GroupEventCarousel {...singleEventProps} />, {
  121. organization: orgWithRecommendedEvent,
  122. });
  123. expect(await screen.getByText('Oldest')).toBeInTheDocument();
  124. });
  125. });
  126. it('can navigate next/previous events', () => {
  127. render(<GroupEventCarousel {...defaultProps} />);
  128. expect(screen.getByLabelText(/Previous Event/)).toHaveAttribute(
  129. 'href',
  130. `/organizations/org-slug/issues/group-id/events/prev-event-id/?referrer=previous-event`
  131. );
  132. expect(screen.getByLabelText(/Next Event/)).toHaveAttribute(
  133. 'href',
  134. `/organizations/org-slug/issues/group-id/events/next-event-id/?referrer=next-event`
  135. );
  136. });
  137. it('can copy event ID', async () => {
  138. render(<GroupEventCarousel {...defaultProps} />);
  139. await userEvent.click(screen.getByText(testEvent.id));
  140. expect(navigator.clipboard.writeText).toHaveBeenCalledWith(testEvent.id);
  141. });
  142. it('can copy event link', async () => {
  143. render(<GroupEventCarousel {...defaultProps} />);
  144. await userEvent.click(screen.getByRole('button', {name: /event actions/i}));
  145. await userEvent.click(screen.getByRole('menuitemradio', {name: /copy event link/i}));
  146. expect(navigator.clipboard.writeText).toHaveBeenCalledWith(
  147. `http://localhost/organizations/org-slug/issues/group-id/events/event-id/`
  148. );
  149. });
  150. it('links to full event details when org has discover', async () => {
  151. render(<GroupEventCarousel {...defaultProps} />, {
  152. organization: TestStubs.Organization({features: ['discover-basic']}),
  153. });
  154. await userEvent.click(screen.getByRole('button', {name: /event actions/i}));
  155. expect(
  156. within(screen.getByRole('menuitemradio', {name: /full event details/i})).getByRole(
  157. 'link'
  158. )
  159. ).toHaveAttribute('href', `/organizations/org-slug/discover/project-slug:event-id/`);
  160. });
  161. it('can open event JSON', async () => {
  162. render(<GroupEventCarousel {...defaultProps} />);
  163. await userEvent.click(screen.getByRole('button', {name: /event actions/i}));
  164. await userEvent.click(screen.getByRole('menuitemradio', {name: 'JSON (7 B)'}));
  165. expect(window.open).toHaveBeenCalledWith(
  166. `/api/0/projects/org-slug/project-slug/events/event-id/json/`
  167. );
  168. });
  169. });