forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
173 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...riggers_actions_ui/public/application/sections/alert_form/alert_conditions_group.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
import * as React from 'react'; | ||
import { mountWithIntl, nextTick } from 'test_utils/enzyme_helpers'; | ||
import { act } from 'react-dom/test-utils'; | ||
import { ReactWrapper } from 'enzyme'; | ||
import { AlertConditionsGroup } from './alert_conditions_group'; | ||
import { EuiFormRow, EuiButtonIcon } from '@elastic/eui'; | ||
|
||
describe('alert_conditions_group', () => { | ||
async function setup(element: React.ReactElement): Promise<ReactWrapper<unknown>> { | ||
const wrapper = mountWithIntl(element); | ||
|
||
// Wait for active space to resolve before requesting the component to update | ||
await act(async () => { | ||
await nextTick(); | ||
wrapper.update(); | ||
}); | ||
|
||
return wrapper; | ||
} | ||
|
||
it('renders with actionGroup name as label', async () => { | ||
const InnerComponent = () => <div>{'inner component'}</div>; | ||
const wrapper = await setup( | ||
<AlertConditionsGroup | ||
actionGroup={{ | ||
id: 'myGroup', | ||
name: 'My Group', | ||
}} | ||
> | ||
<InnerComponent /> | ||
</AlertConditionsGroup> | ||
); | ||
|
||
expect(wrapper.find(EuiFormRow).prop('label')).toMatchInlineSnapshot(` | ||
<EuiTitle | ||
size="s" | ||
> | ||
<strong> | ||
My Group | ||
</strong> | ||
</EuiTitle> | ||
`); | ||
expect(wrapper.find(InnerComponent).prop('actionGroup')).toMatchInlineSnapshot(` | ||
Object { | ||
"id": "myGroup", | ||
"name": "My Group", | ||
} | ||
`); | ||
}); | ||
|
||
it('renders a reset button when onResetConditionsFor is specified', async () => { | ||
const onResetConditionsFor = jest.fn(); | ||
const wrapper = await setup( | ||
<AlertConditionsGroup | ||
actionGroup={{ | ||
id: 'myGroup', | ||
name: 'My Group', | ||
}} | ||
onResetConditionsFor={onResetConditionsFor} | ||
> | ||
<div>{'inner component'}</div> | ||
</AlertConditionsGroup> | ||
); | ||
|
||
expect(wrapper.find(EuiButtonIcon).prop('aria-label')).toMatchInlineSnapshot(`"Remove"`); | ||
|
||
wrapper.find(EuiButtonIcon).simulate('click'); | ||
|
||
expect(onResetConditionsFor).toHaveBeenCalledWith({ | ||
id: 'myGroup', | ||
name: 'My Group', | ||
}); | ||
}); | ||
|
||
it('shouldnt render a reset button when isRequired is true', async () => { | ||
const onResetConditionsFor = jest.fn(); | ||
const wrapper = await setup( | ||
<AlertConditionsGroup | ||
actionGroup={{ | ||
id: 'myGroup', | ||
name: 'My Group', | ||
conditions: true, | ||
isRequired: true, | ||
}} | ||
onResetConditionsFor={onResetConditionsFor} | ||
> | ||
<div>{'inner component'}</div> | ||
</AlertConditionsGroup> | ||
); | ||
|
||
expect(wrapper.find(EuiButtonIcon).length).toEqual(0); | ||
}); | ||
}); |
60 changes: 60 additions & 0 deletions
60
...ins/triggers_actions_ui/public/application/sections/alert_form/alert_conditions_group.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
import React, { Fragment, PropsWithChildren } from 'react'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { EuiFormRow, EuiButtonIcon, EuiTitle } from '@elastic/eui'; | ||
import { AlertConditionsProps, ActionGroupWithCondition } from './alert_conditions'; | ||
|
||
export type AlertConditionsGroupProps<ConditionProps> = { | ||
actionGroup?: ActionGroupWithCondition<ConditionProps>; | ||
} & Pick<AlertConditionsProps<ConditionProps>, 'onResetConditionsFor'>; | ||
|
||
export const AlertConditionsGroup = <ConditionProps extends unknown>({ | ||
actionGroup, | ||
onResetConditionsFor, | ||
children, | ||
...otherProps | ||
}: PropsWithChildren<AlertConditionsGroupProps<ConditionProps>>) => { | ||
if (!actionGroup) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<EuiFormRow | ||
label={ | ||
<EuiTitle size="s"> | ||
<strong>{actionGroup.name}</strong> | ||
</EuiTitle> | ||
} | ||
fullWidth | ||
labelAppend={ | ||
onResetConditionsFor && | ||
!actionGroup.isRequired && ( | ||
<EuiButtonIcon | ||
iconType="minusInCircle" | ||
color="danger" | ||
aria-label={i18n.translate( | ||
'xpack.triggersActionsUI.sections.alertAdd.conditions.removeConditionLabel', | ||
{ | ||
defaultMessage: 'Remove', | ||
} | ||
)} | ||
onClick={() => onResetConditionsFor(actionGroup)} | ||
/> | ||
) | ||
} | ||
> | ||
{React.isValidElement(children) ? ( | ||
React.cloneElement(React.Children.only(children), { | ||
actionGroup, | ||
...otherProps, | ||
}) | ||
) : ( | ||
<Fragment /> | ||
)} | ||
</EuiFormRow> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters