Skip to content

Commit a3bdebf

Browse files
committed
Improve tests
1 parent 1da1728 commit a3bdebf

File tree

7 files changed

+160
-65
lines changed

7 files changed

+160
-65
lines changed

x-pack/plugins/security_solution/public/cases/components/all_cases/index.tsx

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -339,32 +339,20 @@ export const AllCases = React.memo<AllCasesProps>(
339339

340340
const TableWrap = useMemo(() => (isModal ? 'span' : Panel), [isModal]);
341341

342-
const onTableRowClick = useMemo(
343-
() =>
344-
memoize<(theCase: Case) => () => void>((theCase) => () => {
342+
const tableRowProps = useCallback(
343+
(theCase: Case) => {
344+
const onTableRowClick = memoize(() => {
345345
if (onRowClick) {
346346
onRowClick(theCase);
347347
}
348-
}),
349-
[onRowClick]
350-
);
348+
});
351349

352-
const tableRowProps = useCallback(
353-
(item) => {
354-
const rowProps = {
355-
'data-test-subj': `cases-table-row-${item.id}`,
350+
return {
351+
'data-test-subj': `cases-table-row-${theCase.id}`,
352+
...(isModal ? { onClick: onTableRowClick } : {}),
356353
};
357-
358-
if (isModal) {
359-
return {
360-
...rowProps,
361-
onClick: onTableRowClick(item),
362-
};
363-
}
364-
365-
return rowProps;
366354
},
367-
[isModal, onTableRowClick]
355+
[isModal, onRowClick]
368356
);
369357

370358
return (

x-pack/plugins/security_solution/public/cases/components/timeline_actions/add_to_case_action.tsx

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import React, { memo, useState, useCallback } from 'react';
7+
import React, { memo, useState, useCallback, useMemo } from 'react';
88
import {
99
EuiPopover,
1010
EuiButtonIcon,
@@ -89,41 +89,47 @@ const AddToCaseActionComponent: React.FC<AddToCaseActionProps> = ({ ecsRowData,
8989
openAllCaseModal();
9090
}, [openAllCaseModal, closePopover]);
9191

92-
const items = [
93-
<EuiContextMenuItem
94-
key="add-new-case-menu-item"
95-
onClick={addNewCaseClick}
96-
aria-label={i18n.ACTION_ADD_NEW_CASE}
97-
data-test-subj="add-new-case-item"
98-
disabled={disabled}
99-
>
100-
<EuiText size="m">{i18n.ACTION_ADD_NEW_CASE}</EuiText>
101-
</EuiContextMenuItem>,
102-
<EuiContextMenuItem
103-
key="add-existing-case-menu-item"
104-
onClick={addExistingCaseClick}
105-
aria-label={i18n.ACTION_ADD_EXISTING_CASE}
106-
data-test-subj="add-existing-case-menu-item"
107-
disabled={disabled}
108-
>
109-
<EuiText size="m">{i18n.ACTION_ADD_EXISTING_CASE}</EuiText>
110-
</EuiContextMenuItem>,
111-
];
112-
113-
const button = (
114-
<EuiToolTip
115-
data-test-subj="attach-alert-to-case-tooltip"
116-
content={i18n.ACTION_ADD_TO_CASE_TOOLTIP}
117-
>
118-
<EuiButtonIcon
119-
aria-label={i18n.ACTION_ADD_TO_CASE_ARIA_LABEL}
120-
data-test-subj="attach-alert-to-case-button"
121-
size="s"
122-
iconType="folderClosed"
123-
onClick={openPopover}
92+
const items = useMemo(
93+
() => [
94+
<EuiContextMenuItem
95+
key="add-new-case-menu-item"
96+
onClick={addNewCaseClick}
97+
aria-label={i18n.ACTION_ADD_NEW_CASE}
98+
data-test-subj="add-new-case-item"
12499
disabled={disabled}
125-
/>
126-
</EuiToolTip>
100+
>
101+
<EuiText size="m">{i18n.ACTION_ADD_NEW_CASE}</EuiText>
102+
</EuiContextMenuItem>,
103+
<EuiContextMenuItem
104+
key="add-existing-case-menu-item"
105+
onClick={addExistingCaseClick}
106+
aria-label={i18n.ACTION_ADD_EXISTING_CASE}
107+
data-test-subj="add-existing-case-menu-item"
108+
disabled={disabled}
109+
>
110+
<EuiText size="m">{i18n.ACTION_ADD_EXISTING_CASE}</EuiText>
111+
</EuiContextMenuItem>,
112+
],
113+
[addExistingCaseClick, addNewCaseClick, disabled]
114+
);
115+
116+
const button = useMemo(
117+
() => (
118+
<EuiToolTip
119+
data-test-subj="attach-alert-to-case-tooltip"
120+
content={i18n.ACTION_ADD_TO_CASE_TOOLTIP}
121+
>
122+
<EuiButtonIcon
123+
aria-label={i18n.ACTION_ADD_TO_CASE_ARIA_LABEL}
124+
data-test-subj="attach-alert-to-case-button"
125+
size="s"
126+
iconType="folderClosed"
127+
onClick={openPopover}
128+
disabled={disabled}
129+
/>
130+
</EuiToolTip>
131+
),
132+
[disabled, openPopover]
127133
);
128134

129135
return (

x-pack/plugins/security_solution/public/cases/components/use_all_cases_modal/all_cases_modal.test.tsx

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,28 @@
33
* or more contributor license agreements. Licensed under the Elastic License;
44
* you may not use this file except in compliance with the Elastic License.
55
*/
6+
7+
/* eslint-disable react/display-name */
68
import { mount } from 'enzyme';
79
import React from 'react';
810
import '../../../common/mock/match_media';
911
import { AllCasesModal } from './all_cases_modal';
1012
import { TestProviders } from '../../../common/mock';
1113

1214
jest.mock('../all_cases', () => {
13-
const AllCases = () => {
14-
return <></>;
15+
return {
16+
AllCases: ({ onRowClick }: { onRowClick: ({ id }: { id: string }) => void }) => {
17+
return (
18+
<button
19+
type="button"
20+
data-test-subj="all-cases-row"
21+
onClick={() => onRowClick({ id: 'case-id' })}
22+
>
23+
{'case-row'}
24+
</button>
25+
);
26+
},
1527
};
16-
return { AllCases };
1728
});
1829

1930
jest.mock('../../../common/lib/kibana', () => {
@@ -82,4 +93,15 @@ describe('AllCasesModal', () => {
8293
isModal: true,
8394
});
8495
});
96+
97+
it('onRowClick called when row is clicked', () => {
98+
const wrapper = mount(
99+
<TestProviders>
100+
<AllCasesModal {...defaultProps} />
101+
</TestProviders>
102+
);
103+
104+
wrapper.find(`[data-test-subj='all-cases-row']`).first().simulate('click');
105+
expect(onRowClick).toHaveBeenCalledWith({ id: 'case-id' });
106+
});
85107
});

x-pack/plugins/security_solution/public/cases/components/use_all_cases_modal/index.test.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66

77
/* eslint-disable react/display-name */
8-
98
import React from 'react';
109
import { renderHook, act } from '@testing-library/react-hooks';
1110
import { render, screen } from '@testing-library/react';
@@ -29,9 +28,9 @@ jest.mock('react-redux', () => {
2928
jest.mock('../../../common/lib/kibana');
3029
jest.mock('../all_cases', () => {
3130
return {
32-
AllCases: ({ onRowClick }: { onRowClick: (id: string, title: string) => void }) => {
31+
AllCases: ({ onRowClick }: { onRowClick: ({ id }: { id: string }) => void }) => {
3332
return (
34-
<button type="button" onClick={() => onRowClick('case-id', 'case title')}>
33+
<button type="button" onClick={() => onRowClick({ id: 'case-id' })}>
3534
{'case-row'}
3635
</button>
3736
);
@@ -130,6 +129,6 @@ describe('useAllCasesModal', () => {
130129
});
131130

132131
expect(result.current.isModalOpen).toBe(false);
133-
expect(onRowClick).toHaveBeenCalledWith('case-id', 'case title');
132+
expect(onRowClick).toHaveBeenCalledWith({ id: 'case-id' });
134133
});
135134
});

x-pack/plugins/security_solution/public/cases/containers/use_post_comment.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ export const usePostComment = (): UsePostComment => {
8181
cancel = true;
8282
};
8383
},
84-
// eslint-disable-next-line react-hooks/exhaustive-deps
85-
[]
84+
[dispatchToaster]
8685
);
8786

8887
return { ...state, postComment: postMyComment };
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import React from 'react';
8+
import { mount } from 'enzyme';
9+
10+
import { useKibana } from '../../../../common/lib/kibana';
11+
import { useDeepEqualSelector } from '../../../../common/hooks/use_selector';
12+
import { mockTimelineModel, TestProviders } from '../../../../common/mock';
13+
import { useAllCasesModal } from '../../../../cases/components/use_all_cases_modal';
14+
import { AddToCaseButton } from '.';
15+
16+
const mockDispatch = jest.fn();
17+
jest.mock('react-redux', () => {
18+
const original = jest.requireActual('react-redux');
19+
return {
20+
...original,
21+
useDispatch: () => mockDispatch,
22+
};
23+
});
24+
25+
jest.mock('../../../../common/lib/kibana');
26+
jest.mock('../../../../common/hooks/use_selector');
27+
jest.mock('../../../../cases/components/use_all_cases_modal');
28+
29+
const useKibanaMock = useKibana as jest.Mocked<typeof useKibana>;
30+
const useAllCasesModalMock = useAllCasesModal as jest.Mock;
31+
32+
describe('EventColumnView', () => {
33+
const navigateToApp = jest.fn();
34+
35+
beforeEach(() => {
36+
useKibanaMock().services.application.navigateToApp = navigateToApp;
37+
(useDeepEqualSelector as jest.Mock).mockReturnValue(mockTimelineModel);
38+
});
39+
40+
it('navigates to the correct path without id', async () => {
41+
useAllCasesModalMock.mockImplementation(({ onRowClick }) => {
42+
onRowClick();
43+
44+
return {
45+
modal: <>{'test'}</>,
46+
openModal: jest.fn(),
47+
isModalOpen: true,
48+
closeModal: jest.fn(),
49+
};
50+
});
51+
52+
mount(
53+
<TestProviders>
54+
<AddToCaseButton timelineId={'timeline-1'} />
55+
</TestProviders>
56+
);
57+
58+
expect(navigateToApp).toHaveBeenCalledWith('securitySolution:case', { path: '/create' });
59+
});
60+
61+
it('navigates to the correct path with id', async () => {
62+
useAllCasesModalMock.mockImplementation(({ onRowClick }) => {
63+
onRowClick({ id: 'case-id' });
64+
65+
return {
66+
modal: <>{'test'}</>,
67+
openModal: jest.fn(),
68+
isModalOpen: true,
69+
closeModal: jest.fn(),
70+
};
71+
});
72+
73+
mount(
74+
<TestProviders>
75+
<AddToCaseButton timelineId={'timeline-1'} />
76+
</TestProviders>
77+
);
78+
79+
expect(navigateToApp).toHaveBeenCalledWith('securitySolution:case', { path: '/case-id' });
80+
});
81+
});

x-pack/plugins/security_solution/public/timelines/components/timeline/body/events/event_column_view.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('EventColumnView', () => {
5151
selectedEventIds: {},
5252
showCheckboxes: false,
5353
showNotes: false,
54-
timelineId: 'timeline-1',
54+
timelineId: 'timeline-test',
5555
toggleShowNotes: jest.fn(),
5656
updateNote: jest.fn(),
5757
isEventPinned: false,

0 commit comments

Comments
 (0)