groupEventCarousel.spec.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 recommendedUser = TestStubs.User({
  35. options: {
  36. defaultIssueEvent: 'recommended',
  37. },
  38. });
  39. const latestUser = TestStubs.User({
  40. options: {
  41. defaultIssueEvent: 'latest',
  42. },
  43. });
  44. const oldestUser = TestStubs.User({
  45. options: {
  46. defaultIssueEvent: 'oldest',
  47. },
  48. });
  49. it('can navigate to the oldest event', async () => {
  50. jest.spyOn(useMedia, 'default').mockReturnValue(true);
  51. render(<GroupEventCarousel {...defaultProps} />);
  52. await userEvent.click(screen.getByRole('button', {name: /recommended/i}));
  53. await userEvent.click(screen.getByRole('option', {name: /oldest/i}));
  54. expect(browserHistory.push).toHaveBeenCalledWith({
  55. pathname: '/organizations/org-slug/issues/group-id/events/oldest/',
  56. query: {referrer: 'oldest-event'},
  57. });
  58. });
  59. it('can navigate to the latest event', async () => {
  60. jest.spyOn(useMedia, 'default').mockReturnValue(true);
  61. render(<GroupEventCarousel {...defaultProps} />);
  62. await userEvent.click(screen.getByRole('button', {name: /recommended/i}));
  63. await userEvent.click(screen.getByRole('option', {name: /latest/i}));
  64. expect(browserHistory.push).toHaveBeenCalledWith({
  65. pathname: '/organizations/org-slug/issues/group-id/events/latest/',
  66. query: {referrer: 'latest-event'},
  67. });
  68. });
  69. it('can navigate to the recommended event', async () => {
  70. jest.spyOn(useMedia, 'default').mockReturnValue(true);
  71. render(<GroupEventCarousel {...defaultProps} />, {
  72. router: {
  73. params: {eventId: 'latest'},
  74. },
  75. });
  76. await userEvent.click(screen.getByRole('button', {name: /latest/i}));
  77. await userEvent.click(screen.getByRole('option', {name: /recommended/i}));
  78. expect(browserHistory.push).toHaveBeenCalledWith({
  79. pathname: '/organizations/org-slug/issues/group-id/events/recommended/',
  80. query: {referrer: 'recommended-event'},
  81. });
  82. });
  83. it('will disable the dropdown if there is only one event', async () => {
  84. jest.spyOn(useMedia, 'default').mockReturnValue(true);
  85. render(<GroupEventCarousel {...singleEventProps} />);
  86. expect(await screen.getByRole('button', {name: 'Recommended'})).toBeDisabled();
  87. });
  88. it('if user default is recommended, it will show it as default', async () => {
  89. ConfigStore.loadInitialData(TestStubs.Config({user: recommendedUser}));
  90. jest.spyOn(useMedia, 'default').mockReturnValue(true);
  91. render(<GroupEventCarousel {...singleEventProps} />);
  92. expect(await screen.getByText('Recommended')).toBeInTheDocument();
  93. });
  94. it('if user default is latest, it will show it as default', async () => {
  95. ConfigStore.loadInitialData(TestStubs.Config({user: latestUser}));
  96. jest.spyOn(useMedia, 'default').mockReturnValue(true);
  97. render(<GroupEventCarousel {...singleEventProps} />);
  98. expect(await screen.getByText('Latest')).toBeInTheDocument();
  99. });
  100. it('if user default is oldest, it will show it as default', async () => {
  101. ConfigStore.loadInitialData(TestStubs.Config({user: oldestUser}));
  102. jest.spyOn(useMedia, 'default').mockReturnValue(true);
  103. render(<GroupEventCarousel {...singleEventProps} />);
  104. expect(await screen.getByText('Oldest')).toBeInTheDocument();
  105. });
  106. });
  107. it('can navigate next/previous events', () => {
  108. render(<GroupEventCarousel {...defaultProps} />);
  109. expect(screen.getByLabelText(/Previous Event/)).toHaveAttribute(
  110. 'href',
  111. `/organizations/org-slug/issues/group-id/events/prev-event-id/?referrer=previous-event`
  112. );
  113. expect(screen.getByLabelText(/Next Event/)).toHaveAttribute(
  114. 'href',
  115. `/organizations/org-slug/issues/group-id/events/next-event-id/?referrer=next-event`
  116. );
  117. });
  118. it('can copy event ID', async () => {
  119. render(<GroupEventCarousel {...defaultProps} />);
  120. await userEvent.click(screen.getByText(testEvent.id));
  121. expect(navigator.clipboard.writeText).toHaveBeenCalledWith(testEvent.id);
  122. });
  123. it('can copy event link', async () => {
  124. render(<GroupEventCarousel {...defaultProps} />);
  125. await userEvent.click(screen.getByRole('button', {name: /event actions/i}));
  126. await userEvent.click(screen.getByRole('menuitemradio', {name: /copy event link/i}));
  127. expect(navigator.clipboard.writeText).toHaveBeenCalledWith(
  128. `http://localhost/organizations/org-slug/issues/group-id/events/event-id/`
  129. );
  130. });
  131. it('links to full event details when org has discover', async () => {
  132. render(<GroupEventCarousel {...defaultProps} />, {
  133. organization: TestStubs.Organization({features: ['discover-basic']}),
  134. });
  135. await userEvent.click(screen.getByRole('button', {name: /event actions/i}));
  136. expect(
  137. within(screen.getByRole('menuitemradio', {name: /full event details/i})).getByRole(
  138. 'link'
  139. )
  140. ).toHaveAttribute('href', `/organizations/org-slug/discover/project-slug:event-id/`);
  141. });
  142. it('can open event JSON', async () => {
  143. render(<GroupEventCarousel {...defaultProps} />);
  144. await userEvent.click(screen.getByRole('button', {name: /event actions/i}));
  145. await userEvent.click(screen.getByRole('menuitemradio', {name: 'JSON (7 B)'}));
  146. expect(window.open).toHaveBeenCalledWith(
  147. `/api/0/projects/org-slug/project-slug/events/event-id/json/`
  148. );
  149. });
  150. });