-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ResponseOps][Cases] Allow users to create templates (#184104)
## Summary Merging into feature branch. Implements create functionality #181864 Add template from case settings page. <img width="1728" alt="image" src="https://github.com/elastic/kibana/assets/117571355/4a4565e5-3ca8-4a7c-a5c9-0ac15b72a0d0"> **How to test** - Go to Cases > Settings - Click on add templates - Fill the form - Save Scenarios: - Create template with different custom fields - Create template with connector Flaky test runner [here](https://buildkite.com/elastic/kibana-flaky-test-suite-runner/builds/6213) ### Checklist Delete any items that are not applicable to this PR. - [x] Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md) - [ ] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios - [x] [Flaky Test Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was used on any tests changed - [x] Any UI touched in this PR is usable by keyboard only (learn more about [keyboard accessibility](https://webaim.org/techniques/keyboard/)) - [x] This renders correctly on smaller devices using a responsive layout. (You can test this [in your browser](https://www.browserstack.com/guide/responsive-testing-on-local-server)) --------- Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
- Loading branch information
1 parent
5986cc9
commit c511009
Showing
67 changed files
with
4,820 additions
and
726 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
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
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
140 changes: 140 additions & 0 deletions
140
x-pack/plugins/cases/public/components/case_form_fields/custom_fields.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,140 @@ | ||
/* | ||
* 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 { screen, waitFor } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
import type { AppMockRenderer } from '../../common/mock'; | ||
import { createAppMockRenderer } from '../../common/mock'; | ||
import { FormTestComponent } from '../../common/test_utils'; | ||
import { customFieldsConfigurationMock } from '../../containers/mock'; | ||
import { CustomFields } from './custom_fields'; | ||
import * as i18n from './translations'; | ||
|
||
describe('CustomFields', () => { | ||
let appMockRender: AppMockRenderer; | ||
const onSubmit = jest.fn(); | ||
|
||
const defaultProps = { | ||
configurationCustomFields: customFieldsConfigurationMock, | ||
isLoading: false, | ||
setCustomFieldsOptional: false, | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
appMockRender = createAppMockRenderer(); | ||
}); | ||
|
||
it('renders correctly', async () => { | ||
appMockRender.render( | ||
<FormTestComponent onSubmit={onSubmit}> | ||
<CustomFields {...defaultProps} /> | ||
</FormTestComponent> | ||
); | ||
|
||
expect(await screen.findByText(i18n.ADDITIONAL_FIELDS)).toBeInTheDocument(); | ||
expect(await screen.findByTestId('caseCustomFields')).toBeInTheDocument(); | ||
|
||
for (const item of customFieldsConfigurationMock) { | ||
expect( | ||
await screen.findByTestId(`${item.key}-${item.type}-create-custom-field`) | ||
).toBeInTheDocument(); | ||
} | ||
}); | ||
|
||
it('should not show the custom fields if the configuration is empty', async () => { | ||
appMockRender.render( | ||
<FormTestComponent onSubmit={onSubmit}> | ||
<CustomFields | ||
isLoading={false} | ||
setCustomFieldsOptional={false} | ||
configurationCustomFields={[]} | ||
/> | ||
</FormTestComponent> | ||
); | ||
|
||
expect(screen.queryByText(i18n.ADDITIONAL_FIELDS)).not.toBeInTheDocument(); | ||
expect(screen.queryAllByTestId('create-custom-field', { exact: false }).length).toEqual(0); | ||
}); | ||
|
||
it('should render as optional fields for text custom fields', async () => { | ||
appMockRender.render( | ||
<FormTestComponent onSubmit={onSubmit}> | ||
<CustomFields | ||
isLoading={false} | ||
configurationCustomFields={customFieldsConfigurationMock} | ||
setCustomFieldsOptional={true} | ||
/> | ||
</FormTestComponent> | ||
); | ||
|
||
expect(screen.getAllByTestId('form-optional-field-label')).toHaveLength(2); | ||
}); | ||
|
||
it('should sort the custom fields correctly', async () => { | ||
const reversedCustomFieldsConfiguration = [...customFieldsConfigurationMock].reverse(); | ||
|
||
appMockRender.render( | ||
<FormTestComponent onSubmit={onSubmit}> | ||
<CustomFields | ||
isLoading={false} | ||
setCustomFieldsOptional={false} | ||
configurationCustomFields={reversedCustomFieldsConfiguration} | ||
/> | ||
</FormTestComponent> | ||
); | ||
|
||
const customFieldsWrapper = await screen.findByTestId('caseCustomFields'); | ||
|
||
const customFields = customFieldsWrapper.querySelectorAll('.euiFormRow'); | ||
|
||
expect(customFields).toHaveLength(4); | ||
|
||
expect(customFields[0]).toHaveTextContent('My test label 1'); | ||
expect(customFields[1]).toHaveTextContent('My test label 2'); | ||
expect(customFields[2]).toHaveTextContent('My test label 3'); | ||
expect(customFields[3]).toHaveTextContent('My test label 4'); | ||
}); | ||
|
||
it('should update the custom fields', async () => { | ||
appMockRender.render( | ||
<FormTestComponent onSubmit={onSubmit}> | ||
<CustomFields {...defaultProps} /> | ||
</FormTestComponent> | ||
); | ||
|
||
const textField = customFieldsConfigurationMock[2]; | ||
const toggleField = customFieldsConfigurationMock[3]; | ||
|
||
userEvent.type( | ||
await screen.findByTestId(`${textField.key}-${textField.type}-create-custom-field`), | ||
'hello' | ||
); | ||
userEvent.click( | ||
await screen.findByTestId(`${toggleField.key}-${toggleField.type}-create-custom-field`) | ||
); | ||
|
||
userEvent.click(await screen.findByText('Submit')); | ||
|
||
await waitFor(() => { | ||
// data, isValid | ||
expect(onSubmit).toHaveBeenCalledWith( | ||
{ | ||
customFields: { | ||
[customFieldsConfigurationMock[0].key]: customFieldsConfigurationMock[0].defaultValue, | ||
[customFieldsConfigurationMock[1].key]: customFieldsConfigurationMock[1].defaultValue, | ||
[textField.key]: 'hello', | ||
[toggleField.key]: true, | ||
}, | ||
}, | ||
true | ||
); | ||
}); | ||
}); | ||
}); |
73 changes: 73 additions & 0 deletions
73
x-pack/plugins/cases/public/components/case_form_fields/custom_fields.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,73 @@ | ||
/* | ||
* 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, { useMemo } from 'react'; | ||
import { sortBy } from 'lodash'; | ||
import { EuiFlexGroup, EuiFlexItem, EuiSpacer, EuiText } from '@elastic/eui'; | ||
|
||
import type { CasesConfigurationUI } from '../../../common/ui'; | ||
import { builderMap as customFieldsBuilderMap } from '../custom_fields/builder'; | ||
import * as i18n from './translations'; | ||
|
||
interface Props { | ||
isLoading: boolean; | ||
setCustomFieldsOptional: boolean; | ||
configurationCustomFields: CasesConfigurationUI['customFields']; | ||
} | ||
|
||
const CustomFieldsComponent: React.FC<Props> = ({ | ||
isLoading, | ||
setCustomFieldsOptional, | ||
configurationCustomFields, | ||
}) => { | ||
const sortedCustomFields = useMemo( | ||
() => sortCustomFieldsByLabel(configurationCustomFields), | ||
[configurationCustomFields] | ||
); | ||
|
||
const customFieldsComponents = sortedCustomFields.map( | ||
(customField: CasesConfigurationUI['customFields'][number]) => { | ||
const customFieldFactory = customFieldsBuilderMap[customField.type]; | ||
const customFieldType = customFieldFactory().build(); | ||
|
||
const CreateComponent = customFieldType.Create; | ||
|
||
return ( | ||
<CreateComponent | ||
isLoading={isLoading} | ||
customFieldConfiguration={customField} | ||
key={customField.key} | ||
setAsOptional={setCustomFieldsOptional} | ||
/> | ||
); | ||
} | ||
); | ||
|
||
if (!configurationCustomFields.length) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<EuiFlexGroup direction="column" gutterSize="s"> | ||
<EuiText size="m"> | ||
<h3>{i18n.ADDITIONAL_FIELDS}</h3> | ||
</EuiText> | ||
<EuiSpacer size="xs" /> | ||
<EuiFlexItem data-test-subj="caseCustomFields">{customFieldsComponents}</EuiFlexItem> | ||
</EuiFlexGroup> | ||
); | ||
}; | ||
|
||
CustomFieldsComponent.displayName = 'CustomFields'; | ||
|
||
export const CustomFields = React.memo(CustomFieldsComponent); | ||
|
||
const sortCustomFieldsByLabel = (configCustomFields: CasesConfigurationUI['customFields']) => { | ||
return sortBy(configCustomFields, (configCustomField) => { | ||
return configCustomField.label; | ||
}); | ||
}; |
Oops, something went wrong.