groupEventCarousel.spec.tsx 6.7 KB

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