createAlertButton.spec.jsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  2. import {navigateTo} from 'sentry/actionCreators/navigation';
  3. import CreateAlertButton, {
  4. CreateAlertFromViewButton,
  5. } from 'sentry/components/createAlertButton';
  6. import GuideStore from 'sentry/stores/guideStore';
  7. import EventView from 'sentry/utils/discover/eventView';
  8. import {DEFAULT_EVENT_VIEW} from 'sentry/views/eventsV2/data';
  9. const onClickMock = jest.fn();
  10. const context = TestStubs.routerContext();
  11. jest.mock('sentry/actionCreators/navigation');
  12. function renderComponent(organization, eventView) {
  13. return render(
  14. <CreateAlertFromViewButton
  15. location={location}
  16. organization={organization}
  17. eventView={eventView}
  18. projects={[TestStubs.Project()]}
  19. onClick={onClickMock}
  20. />,
  21. {context}
  22. );
  23. }
  24. function renderSimpleComponent(organization, extraProps) {
  25. return render(<CreateAlertButton organization={organization} {...extraProps} />);
  26. }
  27. describe('CreateAlertFromViewButton', () => {
  28. const organization = TestStubs.Organization();
  29. afterEach(() => {
  30. jest.resetAllMocks();
  31. });
  32. it('should trigger onClick callback', () => {
  33. const eventView = EventView.fromSavedQuery({
  34. ...DEFAULT_EVENT_VIEW,
  35. query: 'event.type:error',
  36. projects: [2],
  37. });
  38. renderComponent(organization, eventView);
  39. userEvent.click(screen.getByRole('button', {name: 'Create Alert'}));
  40. expect(onClickMock).toHaveBeenCalledTimes(1);
  41. });
  42. it('disables the create alert button for members', () => {
  43. const eventView = EventView.fromSavedQuery({
  44. ...DEFAULT_EVENT_VIEW,
  45. });
  46. const noAccessOrg = {
  47. ...organization,
  48. access: [],
  49. };
  50. renderComponent(noAccessOrg, eventView);
  51. expect(screen.getByRole('button', {name: 'Create Alert'})).toBeDisabled();
  52. });
  53. it('shows a guide for members', () => {
  54. const noAccessOrg = {
  55. ...organization,
  56. access: [],
  57. };
  58. renderSimpleComponent(noAccessOrg, {
  59. showPermissionGuide: true,
  60. });
  61. expect(GuideStore.state.anchors).toEqual(new Set(['alerts_write_member']));
  62. });
  63. it('shows a guide for owners/admins', () => {
  64. const adminAccessOrg = {
  65. ...organization,
  66. access: ['org:write'],
  67. };
  68. renderSimpleComponent(adminAccessOrg, {
  69. showPermissionGuide: true,
  70. });
  71. expect(GuideStore.state.anchors).toEqual(new Set(['alerts_write_owner']));
  72. });
  73. it('redirects to alert wizard with no project', () => {
  74. renderSimpleComponent(organization);
  75. userEvent.click(screen.getByRole('button'));
  76. expect(navigateTo).toHaveBeenCalledWith(
  77. `/organizations/org-slug/alerts/wizard/`,
  78. undefined
  79. );
  80. });
  81. it('redirects to alert wizard with a project', () => {
  82. renderSimpleComponent(organization, {
  83. projectSlug: 'proj-slug',
  84. });
  85. expect(screen.getByRole('button')).toHaveAttribute(
  86. 'href',
  87. '/organizations/org-slug/alerts/wizard/?project=proj-slug'
  88. );
  89. });
  90. it('removes a duplicate project filter', () => {
  91. const eventView = EventView.fromSavedQuery({
  92. ...DEFAULT_EVENT_VIEW,
  93. query: 'event.type:error project:project-slug',
  94. projects: [2],
  95. });
  96. renderComponent(organization, eventView);
  97. userEvent.click(screen.getByRole('button'));
  98. expect(context.context.router.push).toHaveBeenCalledWith({
  99. pathname: `/organizations/org-slug/alerts/new/metric/`,
  100. query: expect.objectContaining({
  101. query: 'event.type:error ',
  102. project: 'project-slug',
  103. }),
  104. });
  105. });
  106. });