Skip to content

Commit

Permalink
[Security Solution][Detection Engine] fixes apply alert tags issue (e…
Browse files Browse the repository at this point in the history
…lastic#194428)

## Summary

- fixes elastic#192084

### Checklist

Delete any items that are not applicable to this PR.

- [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
  • Loading branch information
vitaliidm authored Oct 3, 2024
1 parent 568e40a commit da4a872
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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 type { State } from './reducer';
import { createAlertTagsReducer } from './reducer';

const initialState: State = {
selectableAlertTags: [],
tagsToAdd: new Set<string>(['False positive']),
tagsToRemove: new Set<string>(['Duplicate']),
};

describe('createAlertTagsReducer', () => {
it('should update state on addAlertTag action', () => {
const reducer = createAlertTagsReducer();
const state = reducer(initialState, { type: 'addAlertTag', value: 'Duplicate' });

expect(Array.from(state.tagsToAdd)).toEqual(['False positive', 'Duplicate']);
expect(Array.from(state.tagsToRemove)).toEqual([]);

// reducer action must not mutate previous state
expect(state.tagsToAdd).not.toBe(initialState.tagsToAdd);
expect(state.tagsToRemove).not.toBe(initialState.tagsToRemove);
expect(state).not.toBe(initialState);
});
it('should update state on removeAlertTag action', () => {
const reducer = createAlertTagsReducer();
const state = reducer(initialState, { type: 'removeAlertTag', value: 'False positive' });

expect(Array.from(state.tagsToRemove)).toEqual(['Duplicate', 'False positive']);
expect(Array.from(state.tagsToAdd)).toEqual([]);

// reducer action must not mutate previous state
expect(state.tagsToRemove).not.toBe(initialState.tagsToRemove);
expect(state.tagsToAdd).not.toBe(initialState.tagsToAdd);
expect(state).not.toBe(initialState);
});
it('should update state on setSelectableAlertTags action', () => {
const reducer = createAlertTagsReducer();
const state = reducer(initialState, {
type: 'setSelectableAlertTags',
value: [{ label: 'Duplicate' }],
});

expect(state.selectableAlertTags).toEqual([{ label: 'Duplicate' }]);
// reducer action must not mutate previous state
expect(state).not.toBe(initialState);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,21 @@ export const createAlertTagsReducer =
switch (action.type) {
case 'addAlertTag': {
const { value } = action;
state.tagsToAdd.add(value);
state.tagsToRemove.delete(value);
return state;
const newTagsToAdd = new Set(state.tagsToAdd);
newTagsToAdd.add(value);
const newTagsToRemove = new Set(state.tagsToRemove);
newTagsToRemove.delete(value);

return { ...state, tagsToAdd: newTagsToAdd, tagsToRemove: newTagsToRemove };
}
case 'removeAlertTag': {
const { value } = action;
state.tagsToRemove.add(value);
state.tagsToAdd.delete(value);
return state;
const newTagsToRemove = new Set(state.tagsToRemove);
newTagsToRemove.add(value);
const newTagsToAdd = new Set(state.tagsToAdd);
newTagsToAdd.delete(value);

return { ...state, tagsToAdd: newTagsToAdd, tagsToRemove: newTagsToRemove };
}
case 'setSelectableAlertTags': {
const { value } = action;
Expand Down

0 comments on commit da4a872

Please sign in to comment.