Skip to content

Commit

Permalink
Fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Jun 26, 2023
1 parent 0e67ae3 commit 1cb1cdd
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export const useCasesAddToExistingCaseModal = (props: AddToExistingCaseModalProp
getAttachments?: ({ theCase }: { theCase?: CaseUI }) => CaseAttachmentsWithoutOwner
) => {
const attachments = getAttachments?.({ theCase }) ?? [];

// when the case is undefined in the modal
// the user clicked "create new case"
if (theCase === undefined) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ describe('bulk action hooks', () => {
const refresh = jest.fn();
const clearSelection = jest.fn();
const openNewCase = jest.fn();

const openExistingCase = jest.fn().mockImplementation(({ getAttachments }) => {
getAttachments({ theCase: { id: caseId } });
});

mockCaseService.helpers.canUseCases = jest.fn().mockReturnValue({ create: true, read: true });
mockCaseService.ui.getCasesContext = jest.fn().mockReturnValue(() => 'Cases context');

Expand Down Expand Up @@ -124,6 +126,55 @@ describe('bulk action hooks', () => {
expect(openExistingCase).toHaveBeenCalled();
});

it('should open the flyout from the case modal', async () => {
openExistingCase.mockImplementationOnce(({ getAttachments }) => {
getAttachments({ theCase: undefined });
});

const alerts = [
{
_id: 'alert0',
_index: 'idx0',
data: [
{
field: 'kibana.alert.case_ids',
value: [caseId],
},
],
ecs: {
_id: 'alert0',
_index: 'idx0',
},
},
{
_id: 'alert1',
_index: 'idx1',
data: [
{
field: 'kibana.alert.case_ids',
value: ['test-case-2'],
},
],
ecs: {
_id: 'alert1',
_index: 'idx1',
},
},
];

const { result } = renderHook(
() => useBulkAddToCaseActions({ casesConfig, refresh, clearSelection }),
{
wrapper: appMockRender.AppWrapper,
}
);

// @ts-expect-error: cases do not need all arguments
result.current[1].onClick(alerts);

expect(mockCaseService.helpers.groupAlertsByRule).toHaveBeenCalledWith(alerts);
});

it('should remove alerts that are already attached to the case', async () => {
const { result } = renderHook(
() => useBulkAddToCaseActions({ casesConfig, refresh, clearSelection }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ export const useBulkAddToCaseActions = ({
onClick: (alerts?: TimelineItem[]) => {
selectCaseModal.open({
getAttachments: ({ theCase }) => {
if (theCase == null) {
return alerts ? casesService?.helpers.groupAlertsByRule(alerts) ?? [] : [];
}

return getCaseAttachments({
alerts,
caseId: theCase.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type UseCasesAddToExistingCaseModal = (props?: Record<string, unknown>) => {
open: ({
getAttachments,
}: {
getAttachments: ({ theCase }: { theCase: { id: string } }) => any[];
getAttachments: ({ theCase }: { theCase?: { id: string } }) => any[];
}) => void;
close: () => void;
};
Expand Down

0 comments on commit 1cb1cdd

Please sign in to comment.