diff --git a/x-pack/plugins/triggers_actions_ui/public/application/sections/common/components/rule_quick_edit_buttons.test.tsx b/x-pack/plugins/triggers_actions_ui/public/application/sections/common/components/rule_quick_edit_buttons.test.tsx new file mode 100644 index 0000000000000..37c00fab8b6fa --- /dev/null +++ b/x-pack/plugins/triggers_actions_ui/public/application/sections/common/components/rule_quick_edit_buttons.test.tsx @@ -0,0 +1,86 @@ +/* + * 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 * as React from 'react'; +import { mountWithIntl } from '@kbn/test-jest-helpers'; +import { RuleTableItem } from '../../../../types'; +import { RuleQuickEditButtonsWithApi as RuleQuickEditButtons } from './rule_quick_edit_buttons'; + +jest.mock('../../../../common/lib/kibana', () => ({ + useKibana: jest.fn().mockReturnValue({ + services: { + notifications: { toast: { addDanger: jest.fn() } }, + }, + }), +})); + +describe('rule_quick_edit_buttons', () => { + it('renders buttons', async () => { + const mockRule: RuleTableItem = { + id: '1', + enabled: true, + muteAll: false, + } as RuleTableItem; + + const wrapper = mountWithIntl( + {}} + onActionPerformed={() => {}} + setRulesToDelete={() => {}} + setRulesToUpdateAPIKey={() => {}} + /> + ); + + expect(wrapper.find('[data-test-subj="muteAll"]').exists()).toBeTruthy(); + expect(wrapper.find('[data-test-subj="unmuteAll"]').exists()).toBeFalsy(); + expect(wrapper.find('[data-test-subj="enableAll"]').exists()).toBeFalsy(); + expect(wrapper.find('[data-test-subj="disableAll"]').exists()).toBeTruthy(); + expect(wrapper.find('[data-test-subj="updateAPIKeys"]').exists()).toBeTruthy(); + expect(wrapper.find('[data-test-subj="deleteAll"]').exists()).toBeTruthy(); + }); + + it('renders muteAll if rules are all muted', async () => { + const mockRule: RuleTableItem = { + id: '1', + muteAll: true, + } as RuleTableItem; + + const wrapper = mountWithIntl( + {}} + onActionPerformed={() => {}} + setRulesToDelete={() => {}} + setRulesToUpdateAPIKey={() => {}} + /> + ); + + expect(wrapper.find('[data-test-subj="muteAll"]').exists()).toBeFalsy(); + expect(wrapper.find('[data-test-subj="unmuteAll"]').exists()).toBeTruthy(); + }); + + it('renders enableAll if rules are all disabled', async () => { + const mockRule: RuleTableItem = { + id: '1', + enabled: false, + } as RuleTableItem; + + const wrapper = mountWithIntl( + {}} + onActionPerformed={() => {}} + setRulesToDelete={() => {}} + setRulesToUpdateAPIKey={() => {}} + /> + ); + + expect(wrapper.find('[data-test-subj="enableAll"]').exists()).toBeTruthy(); + expect(wrapper.find('[data-test-subj="disableAll"]').exists()).toBeFalsy(); + }); +}); diff --git a/x-pack/plugins/triggers_actions_ui/public/application/sections/common/components/rule_quick_edit_buttons.tsx b/x-pack/plugins/triggers_actions_ui/public/application/sections/common/components/rule_quick_edit_buttons.tsx index 9ecaed9775e68..c6b0a92a91055 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/sections/common/components/rule_quick_edit_buttons.tsx +++ b/x-pack/plugins/triggers_actions_ui/public/application/sections/common/components/rule_quick_edit_buttons.tsx @@ -23,6 +23,7 @@ export type ComponentOpts = { onPerformingAction?: () => void; onActionPerformed?: () => void; setRulesToDelete: React.Dispatch>; + setRulesToUpdateAPIKey: React.Dispatch>; } & BulkOperationsComponentOpts; export const RuleQuickEditButtons: React.FunctionComponent = ({ @@ -34,6 +35,7 @@ export const RuleQuickEditButtons: React.FunctionComponent = ({ enableRules, disableRules, setRulesToDelete, + setRulesToUpdateAPIKey, }: ComponentOpts) => { const { notifications: { toasts }, @@ -44,6 +46,7 @@ export const RuleQuickEditButtons: React.FunctionComponent = ({ const [isEnablingRules, setIsEnablingRules] = useState(false); const [isDisablingRules, setIsDisablingRules] = useState(false); const [isDeletingRules, setIsDeletingRules] = useState(false); + const [isUpdatingRuleAPIKeys, setIsUpdatingRuleAPIKeys] = useState(false); const allRulesMuted = selectedItems.every(isRuleMuted); const allRulesDisabled = selectedItems.every(isRuleDisabled); @@ -154,6 +157,26 @@ export const RuleQuickEditButtons: React.FunctionComponent = ({ } } + async function updateAPIKeysClick() { + onPerformingAction(); + setIsUpdatingRuleAPIKeys(true); + try { + setRulesToUpdateAPIKey(selectedItems.map((selected: any) => selected.id)); + } catch (e) { + toasts.addDanger({ + title: i18n.translate( + 'xpack.triggersActionsUI.sections.rulesList.bulkActionPopover.failedToUpdateRuleAPIKeysMessage', + { + defaultMessage: 'Failed to update API keys for rule(s)', + } + ), + }); + } finally { + setIsUpdatingRuleAPIKeys(false); + onActionPerformed(); + } + } + return ( {!allRulesMuted && ( @@ -216,6 +239,19 @@ export const RuleQuickEditButtons: React.FunctionComponent = ({ )} + + + + +