123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- import {OrganizationFixture} from 'sentry-fixture/organization';
- import {ProjectFixture} from 'sentry-fixture/project';
- import {RouterFixture} from 'sentry-fixture/routerFixture';
- import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
- import {navigateTo} from 'sentry/actionCreators/navigation';
- import CreateAlertButton, {
- CreateAlertFromViewButton,
- } from 'sentry/components/createAlertButton';
- import GuideStore from 'sentry/stores/guideStore';
- import EventView from 'sentry/utils/discover/eventView';
- import useProjects from 'sentry/utils/useProjects';
- import {DEFAULT_EVENT_VIEW} from 'sentry/views/discover/data';
- const onClickMock = jest.fn();
- jest.mock('sentry/utils/useProjects');
- jest.mock('sentry/actionCreators/navigation');
- describe('CreateAlertFromViewButton', () => {
- const organization = OrganizationFixture();
- beforeEach(() => {
- jest.mocked(useProjects).mockReturnValue({
- projects: [],
- onSearch: jest.fn(),
- reloadProjects: jest.fn(),
- placeholders: [],
- fetching: false,
- hasMore: null,
- fetchError: null,
- initiallyLoaded: false,
- });
- });
- afterEach(() => {
- jest.resetAllMocks();
- });
- it('should trigger onClick callback', async () => {
- const eventView = EventView.fromSavedQuery({
- ...DEFAULT_EVENT_VIEW,
- query: 'event.type:error',
- projects: [2],
- });
- render(
- <CreateAlertFromViewButton
- organization={organization}
- eventView={eventView}
- projects={[ProjectFixture()]}
- onClick={onClickMock}
- />
- );
- await userEvent.click(screen.getByRole('button', {name: 'Create Alert'}));
- expect(onClickMock).toHaveBeenCalledTimes(1);
- });
- it('disables the button for org-member', () => {
- const eventView = EventView.fromSavedQuery({
- ...DEFAULT_EVENT_VIEW,
- });
- const noAccessOrg = {
- ...organization,
- access: [],
- };
- const projects = [
- {
- ...ProjectFixture(),
- access: [],
- },
- ];
- jest.mocked(useProjects).mockReturnValue({
- projects,
- onSearch: jest.fn(),
- reloadProjects: jest.fn(),
- placeholders: [],
- fetching: false,
- hasMore: null,
- fetchError: null,
- initiallyLoaded: false,
- });
- render(
- <CreateAlertFromViewButton
- organization={noAccessOrg}
- eventView={eventView}
- projects={projects}
- onClick={onClickMock}
- />,
- {
- organization: noAccessOrg,
- }
- );
- expect(screen.getByRole('button', {name: 'Create Alert'})).toBeDisabled();
- });
- it('enables the button for org-owner/manager', () => {
- const eventView = EventView.fromSavedQuery({
- ...DEFAULT_EVENT_VIEW,
- });
- const projects = [
- {
- ...ProjectFixture(),
- access: [],
- },
- ];
- jest.mocked(useProjects).mockReturnValue({
- projects,
- onSearch: jest.fn(),
- reloadProjects: jest.fn(),
- placeholders: [],
- fetching: false,
- hasMore: null,
- fetchError: null,
- initiallyLoaded: false,
- });
- render(
- <CreateAlertFromViewButton
- organization={organization}
- eventView={eventView}
- projects={projects}
- onClick={onClickMock}
- />,
- {
- organization,
- }
- );
- expect(screen.getByRole('button', {name: 'Create Alert'})).toBeEnabled();
- });
- it('enables the button for team-admin', () => {
- const eventView = EventView.fromSavedQuery({
- ...DEFAULT_EVENT_VIEW,
- });
- const noAccessOrg = {
- ...organization,
- access: [],
- };
- const projects = [
- {
- ...ProjectFixture(),
- id: '1',
- slug: 'admin-team',
- access: ['alerts:write' as const],
- },
- {
- ...ProjectFixture(),
- id: '2',
- slug: 'contributor-team',
- access: ['alerts:read' as const],
- },
- ];
- jest.mocked(useProjects).mockReturnValue({
- projects,
- onSearch: jest.fn(),
- reloadProjects: jest.fn(),
- placeholders: [],
- fetching: false,
- hasMore: null,
- fetchError: null,
- initiallyLoaded: false,
- });
- render(
- <CreateAlertFromViewButton
- organization={noAccessOrg}
- eventView={eventView}
- projects={projects}
- onClick={onClickMock}
- />,
- {
- organization: noAccessOrg,
- }
- );
- expect(screen.getByRole('button', {name: 'Create Alert'})).toBeEnabled();
- });
- it('shows a guide for org-member', () => {
- const noAccessOrg = {
- ...organization,
- access: [],
- };
- render(
- <CreateAlertButton
- aria-label="Create Alert"
- organization={noAccessOrg}
- showPermissionGuide
- />,
- {
- organization: noAccessOrg,
- }
- );
- expect(GuideStore.state.anchors).toEqual(new Set(['alerts_write_member']));
- });
- it('shows a guide for org-owner/manager', () => {
- const adminAccessOrg = OrganizationFixture({
- ...organization,
- access: ['org:write'],
- });
- render(
- <CreateAlertButton
- aria-label="Create Alert"
- organization={adminAccessOrg}
- showPermissionGuide
- />,
- {
- organization: adminAccessOrg,
- }
- );
- expect(GuideStore.state.anchors).toEqual(new Set(['alerts_write_owner']));
- });
- it('redirects to alert wizard with no project', async () => {
- render(<CreateAlertButton aria-label="Create Alert" organization={organization} />, {
- organization,
- });
- await userEvent.click(screen.getByRole('button'));
- expect(navigateTo).toHaveBeenCalledWith(
- `/organizations/org-slug/alerts/wizard/?`,
- expect.objectContaining({
- params: expect.objectContaining({
- orgId: 'org-slug',
- }),
- })
- );
- });
- it('redirects to alert wizard with a project', () => {
- render(
- <CreateAlertButton
- aria-label="Create Alert"
- organization={organization}
- projectSlug="proj-slug"
- />,
- {
- organization,
- }
- );
- expect(screen.getByRole('button')).toHaveAttribute(
- 'href',
- '/organizations/org-slug/alerts/wizard/?project=proj-slug'
- );
- });
- it('removes a duplicate project filter', async () => {
- const router = RouterFixture();
- const projects = [ProjectFixture()];
- jest.mocked(useProjects).mockReturnValue({
- projects,
- onSearch: jest.fn(),
- reloadProjects: jest.fn(),
- placeholders: [],
- fetching: false,
- hasMore: null,
- fetchError: null,
- initiallyLoaded: false,
- });
- const eventView = EventView.fromSavedQuery({
- ...DEFAULT_EVENT_VIEW,
- query: 'event.type:error project:project-slug',
- projects: [2],
- });
- render(
- <CreateAlertFromViewButton
- organization={organization}
- eventView={eventView}
- projects={projects}
- onClick={onClickMock}
- />,
- {router}
- );
- await userEvent.click(screen.getByRole('button'));
- expect(router.push).toHaveBeenCalledWith({
- pathname: `/organizations/org-slug/alerts/new/metric/`,
- query: expect.objectContaining({
- query: 'event.type:error ',
- project: 'project-slug',
- }),
- });
- });
- });
|