Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ import { unifiedSearchPluginMock } from '@kbn/unified-search-plugin/public/mocks
import type { AlertActionsProps } from '@kbn/triggers-actions-ui-plugin/public/types';
import { getAlertsTableDefaultAlertActionsLazy } from '@kbn/triggers-actions-ui-plugin/public/common/get_alerts_table_default_row_actions';
import { lensPluginMock } from '@kbn/lens-plugin/public/mocks';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';

const queryClient = new QueryClient({
defaultOptions: {
queries: {
retry: false,
},
},
logger: {
log: () => {},
warn: () => {},
error: () => {},
},
});

const triggersActionsUiStartMock = {
createStart() {
Expand All @@ -29,7 +43,11 @@ const triggersActionsUiStartMock = {
<div data-test-subj="alerts-state-table">mocked component</div>
)),
getAlertsTableDefaultAlertActions: (props: AlertActionsProps) => {
return getAlertsTableDefaultAlertActionsLazy(props);
return (
<QueryClientProvider client={queryClient}>
{getAlertsTableDefaultAlertActionsLazy(props)}
</QueryClientProvider>
);
},
getAddRuleFlyout: jest.fn(() => <div data-test-subj="add-rule-flyout">mocked component</div>),
getEditRuleFlyout: jest.fn(() => (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import React from 'react';
import DefaultAlertActions from './default_alert_actions';
import { render, screen } from '@testing-library/react';
import type { AlertActionsProps } from '../../../../types';

jest.mock('../../../hooks/use_load_rule_types_query', () => ({
useLoadRuleTypesQuery: jest.fn(),
}));
jest.mock('./view_rule_details_alert_action', () => {
return {
ViewRuleDetailsAlertAction: () => (
<div data-test-subj="viewRuleDetailsAlertAction">{'ViewRuleDetailsAlertAction'}</div>
),
};
});
jest.mock('./view_alert_details_alert_action', () => {
return {
ViewAlertDetailsAlertAction: () => (
<div data-test-subj="viewAlertDetailsAlertAction">{'ViewAlertDetailsAlertAction'}</div>
),
};
});
jest.mock('./mute_alert_action', () => {
return { MuteAlertAction: () => <div data-test-subj="muteAlertAction">{'MuteAlertAction'}</div> };
});
jest.mock('./mark_as_untracked_alert_action', () => {
return {
MarkAsUntrackedAlertAction: () => (
<div data-test-subj="markAsUntrackedAlertAction">{'MarkAsUntrackedAlertAction'}</div>
),
};
});

const { useLoadRuleTypesQuery } = jest.requireMock('../../../hooks/use_load_rule_types_query');
const props = { alert: {}, refresh: jest.fn() } as unknown as AlertActionsProps;

describe('DefaultAlertActions component', () => {
it('should show "Mute" and "Marked as untracted" option', async () => {
useLoadRuleTypesQuery.mockReturnValue({ authorizedToCreateAnyRules: true });

render(<DefaultAlertActions {...props} />);

expect(await screen.findByText('MuteAlertAction')).toBeInTheDocument();
expect(await screen.findByText('MarkAsUntrackedAlertAction')).toBeInTheDocument();
});

it('should hide "Mute" and "Marked as untracted" option', async () => {
useLoadRuleTypesQuery.mockReturnValue({ authorizedToCreateAnyRules: false });

render(<DefaultAlertActions {...props} />);

expect(screen.queryByText('MuteAlertAction')).not.toBeInTheDocument();
expect(screen.queryByText('MarkAsUntrackedAlertAction')).not.toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,22 @@ import type { AlertActionsProps } from '../../../../types';
import { ViewAlertDetailsAlertAction } from './view_alert_details_alert_action';
import { MuteAlertAction } from './mute_alert_action';
import { MarkAsUntrackedAlertAction } from './mark_as_untracked_alert_action';
import { useLoadRuleTypesQuery } from '../../../hooks/use_load_rule_types_query';

/**
* Common alerts table row actions
*/
export const DefaultAlertActions = (props: AlertActionsProps) => {
const { authorizedToCreateAnyRules } = useLoadRuleTypesQuery({
filteredRuleTypes: [],
});

return (
<>
<ViewRuleDetailsAlertAction {...props} />
<ViewAlertDetailsAlertAction {...props} />
<MarkAsUntrackedAlertAction {...props} />
<MuteAlertAction {...props} />
{authorizedToCreateAnyRules && <MarkAsUntrackedAlertAction {...props} />}
{authorizedToCreateAnyRules && <MuteAlertAction {...props} />}
</>
);
};
Expand Down