Cleans comment state when submiting and when init the store (#101861) (#101890)

Co-authored-by: David Sánchez <davidsansol92@gmail.com>
This commit is contained in:
Kibana Machine 2021-06-10 10:53:30 -04:00 committed by GitHub
parent aa3c54879e
commit 1d7c3d2769
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 59 additions and 1 deletions

View file

@ -22,6 +22,11 @@ import { EventFiltersListPageState, EventFiltersService } from '../types';
import { getFoundExceptionListItemSchemaMock } from '../../../../../../lists/common/schemas/response/found_exception_list_item_schema.mock';
import { isFailedResourceState, isLoadedResourceState } from '../../../state';
import { getListFetchError } from './selector';
import type {
ExceptionListItemSchema,
CreateExceptionListItemSchema,
} from '@kbn/securitysolution-io-ts-list-types';
import { Immutable } from '../../../../../common/endpoint/types';
const createEventFiltersServiceMock = (): jest.Mocked<EventFiltersService> => ({
addEventFilters: jest.fn(),
@ -208,6 +213,38 @@ describe('Event filters middleware', () => {
});
});
it('does submit when entry has empty comments with white spaces', async () => {
service.addEventFilters.mockImplementation(
async (exception: Immutable<ExceptionListItemSchema | CreateExceptionListItemSchema>) => {
expect(exception.comments).toStrictEqual(createdEventFilterEntryMock().comments);
return createdEventFilterEntryMock();
}
);
const entry = getInitialExceptionFromEvent(ecsEventMock());
store.dispatch({
type: 'eventFiltersInitForm',
payload: { entry },
});
store.dispatch({
type: 'eventFiltersChangeForm',
payload: { newComment: ' ', entry },
});
store.dispatch({ type: 'eventFiltersCreateStart' });
await spyMiddleware.waitForAction('eventFiltersFormStateChanged');
expect(store.getState()).toStrictEqual({
...initialState,
form: {
...store.getState().form,
submissionResourceState: {
type: 'LoadedResourceState',
data: createdEventFilterEntryMock(),
},
},
});
});
it('does throw error when creating', async () => {
service.addEventFilters.mockRejectedValue({
body: { message: 'error message', statusCode: 500, error: 'Internal Server Error' },

View file

@ -57,7 +57,8 @@ const addNewComments = (
): UpdateExceptionListItemSchema | CreateExceptionListItemSchema => {
if (newComment) {
if (!entry.comments) entry.comments = [];
entry.comments.push({ comment: newComment });
const trimmedComment = newComment.trim();
if (trimmedComment) entry.comments.push({ comment: trimmedComment });
}
return entry;
};

View file

@ -88,6 +88,25 @@ describe('event filters reducer', () => {
});
});
it('clean form after change form status', () => {
const entry = getInitialExceptionFromEvent(ecsEventMock());
const nameChanged = 'name changed';
const newComment = 'new comment';
const result = eventFiltersPageReducer(initialState, {
type: 'eventFiltersChangeForm',
payload: { entry: { ...entry, name: nameChanged }, newComment },
});
const cleanState = eventFiltersPageReducer(result, {
type: 'eventFiltersInitForm',
payload: { entry },
});
expect(cleanState).toStrictEqual({
...initialState,
form: { ...initialState.form, entry, hasNameError: true, newComment: '' },
});
});
it('create is success and force list refresh', () => {
const initialStateWithListPageActive = {
...initialState,

View file

@ -86,6 +86,7 @@ const eventFiltersInitForm: CaseReducer<EventFiltersInitForm> = (state, action)
entry: action.payload.entry,
hasNameError: !action.payload.entry.name,
hasOSError: !action.payload.entry.os_types?.length,
newComment: '',
submissionResourceState: {
type: 'UninitialisedResourceState',
},