-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Cases] Add severity field to create case (#131626)
- Loading branch information
Showing
7 changed files
with
184 additions
and
3 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
79 changes: 79 additions & 0 deletions
79
x-pack/plugins/cases/public/components/create/severity.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,79 @@ | ||
/* | ||
* 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 { CaseSeverity } from '../../../common/api'; | ||
import React from 'react'; | ||
import { AppMockRenderer, createAppMockRenderer } from '../../common/mock'; | ||
import { Form, FormHook, useForm } from '../../common/shared_imports'; | ||
import { Severity } from './severity'; | ||
import { FormProps, schema } from './schema'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { waitFor } from '@testing-library/dom'; | ||
|
||
let globalForm: FormHook; | ||
const MockHookWrapperComponent: React.FC = ({ children }) => { | ||
const { form } = useForm<FormProps>({ | ||
defaultValue: { severity: CaseSeverity.LOW }, | ||
schema: { | ||
severity: schema.severity, | ||
}, | ||
}); | ||
|
||
globalForm = form; | ||
|
||
return <Form form={form}>{children}</Form>; | ||
}; | ||
describe('Severity form field', () => { | ||
let appMockRender: AppMockRenderer; | ||
beforeEach(() => { | ||
appMockRender = createAppMockRenderer(); | ||
}); | ||
it('renders', () => { | ||
const result = appMockRender.render( | ||
<MockHookWrapperComponent> | ||
<Severity isLoading={false} /> | ||
</MockHookWrapperComponent> | ||
); | ||
expect(result.getByTestId('caseSeverity')).toBeTruthy(); | ||
expect(result.getByTestId('case-severity-selection')).not.toHaveAttribute('disabled'); | ||
}); | ||
|
||
// default to LOW in this test configuration | ||
it('defaults to the correct value', () => { | ||
const result = appMockRender.render( | ||
<MockHookWrapperComponent> | ||
<Severity isLoading={false} /> | ||
</MockHookWrapperComponent> | ||
); | ||
expect(result.getByTestId('caseSeverity')).toBeTruthy(); | ||
// two items. one for the popover one for the selected field | ||
expect(result.getAllByTestId('case-severity-selection-low').length).toBe(2); | ||
}); | ||
|
||
it('selects the correct value when changed', async () => { | ||
const result = appMockRender.render( | ||
<MockHookWrapperComponent> | ||
<Severity isLoading={false} /> | ||
</MockHookWrapperComponent> | ||
); | ||
expect(result.getByTestId('caseSeverity')).toBeTruthy(); | ||
userEvent.click(result.getByTestId('case-severity-selection')); | ||
userEvent.click(result.getByTestId('case-severity-selection-high')); | ||
await waitFor(() => { | ||
expect(globalForm.getFormData()).toEqual({ severity: 'high' }); | ||
}); | ||
}); | ||
|
||
it('disables when loading data', () => { | ||
const result = appMockRender.render( | ||
<MockHookWrapperComponent> | ||
<Severity isLoading={true} /> | ||
</MockHookWrapperComponent> | ||
); | ||
expect(result.getByTestId('case-severity-selection')).toHaveAttribute('disabled'); | ||
}); | ||
}); |
50 changes: 50 additions & 0 deletions
50
x-pack/plugins/cases/public/components/create/severity.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,50 @@ | ||
/* | ||
* 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 { EuiFormRow } from '@elastic/eui'; | ||
import React, { memo } from 'react'; | ||
import { CaseSeverity } from '../../../common/api'; | ||
import { UseField, useFormContext, useFormData } from '../../common/shared_imports'; | ||
import { SeveritySelector } from '../severity/selector'; | ||
import { SEVERITY_TITLE } from '../severity/translations'; | ||
|
||
interface Props { | ||
isLoading: boolean; | ||
} | ||
|
||
const SeverityFieldFormComponent = ({ isLoading }: { isLoading: boolean }) => { | ||
const { setFieldValue } = useFormContext(); | ||
const [{ severity }] = useFormData({ watch: ['severity'] }); | ||
const onSeverityChange = (newSeverity: CaseSeverity) => { | ||
setFieldValue('severity', newSeverity); | ||
}; | ||
return ( | ||
<EuiFormRow data-test-subj="caseSeverity" fullWidth={true} label={SEVERITY_TITLE}> | ||
<SeveritySelector | ||
isLoading={isLoading} | ||
isDisabled={isLoading} | ||
selectedSeverity={severity ?? CaseSeverity.LOW} | ||
onSeverityChange={onSeverityChange} | ||
/> | ||
</EuiFormRow> | ||
); | ||
}; | ||
SeverityFieldFormComponent.displayName = 'SeverityFieldForm'; | ||
|
||
const SeverityComponent: React.FC<Props> = ({ isLoading }) => ( | ||
<UseField | ||
path={'severity'} | ||
component={SeverityFieldFormComponent} | ||
componentProps={{ | ||
isLoading, | ||
}} | ||
/> | ||
); | ||
|
||
SeverityComponent.displayName = 'SeverityComponent'; | ||
|
||
export const Severity = memo(SeverityComponent); |