Skip to content

Commit

Permalink
[Security Solution][Cases] Create useAllCasesModal hook (#72602)
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas authored Jul 27, 2020
1 parent 76150a4 commit bc65c5e
Show file tree
Hide file tree
Showing 11 changed files with 567 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { TestProviders } from '../../../common/mock';
import { useGetCasesMockState } from '../../containers/mock';
import * as i18n from './translations';

import { useKibana } from '../../../common/lib/kibana';
import { createUseKibanaMock } from '../../../common/mock/kibana_react';
import { getEmptyTagValue } from '../../../common/components/empty_value';
import { useDeleteCases } from '../../containers/use_delete_cases';
import { useGetCases } from '../../containers/use_get_cases';
Expand All @@ -26,13 +28,16 @@ jest.mock('../../containers/use_delete_cases');
jest.mock('../../containers/use_get_cases');
jest.mock('../../containers/use_get_cases_status');

const useKibanaMock = useKibana as jest.Mock;
const useDeleteCasesMock = useDeleteCases as jest.Mock;
const useGetCasesMock = useGetCases as jest.Mock;
const useGetCasesStatusMock = useGetCasesStatus as jest.Mock;
const useUpdateCasesMock = useUpdateCases as jest.Mock;

jest.mock('../../../common/components/link_to');

jest.mock('../../../common/lib/kibana');

describe('AllCases', () => {
const dispatchResetIsDeleted = jest.fn();
const dispatchResetIsUpdated = jest.fn();
Expand All @@ -45,6 +50,7 @@ describe('AllCases', () => {
const setSelectedCases = jest.fn();
const updateBulkStatus = jest.fn();
const fetchCasesStatus = jest.fn();
const onRowClick = jest.fn();
const emptyTag = getEmptyTagValue().props.children;

const defaultGetCases = {
Expand Down Expand Up @@ -77,6 +83,9 @@ describe('AllCases', () => {
dispatchResetIsUpdated,
updateBulkStatus,
};

let navigateToApp: jest.Mock;

/* eslint-disable no-console */
// Silence until enzyme fixed to use ReactTestUtils.act()
const originalError = console.error;
Expand All @@ -89,10 +98,20 @@ describe('AllCases', () => {
/* eslint-enable no-console */
beforeEach(() => {
jest.resetAllMocks();
useUpdateCasesMock.mockImplementation(() => defaultUpdateCases);
useGetCasesMock.mockImplementation(() => defaultGetCases);
useDeleteCasesMock.mockImplementation(() => defaultDeleteCases);
useGetCasesStatusMock.mockImplementation(() => defaultCasesStatus);
navigateToApp = jest.fn();
const kibanaMock = createUseKibanaMock()();
useKibanaMock.mockReturnValue({
...kibanaMock,
services: {
application: {
navigateToApp,
},
},
});
useUpdateCasesMock.mockReturnValue(defaultUpdateCases);
useGetCasesMock.mockReturnValue(defaultGetCases);
useDeleteCasesMock.mockReturnValue(defaultDeleteCases);
useGetCasesStatusMock.mockReturnValue(defaultCasesStatus);
moment.tz.setDefault('UTC');
});
it('should render AllCases', () => {
Expand Down Expand Up @@ -125,7 +144,7 @@ describe('AllCases', () => {
);
});
it('should render empty fields', () => {
useGetCasesMock.mockImplementation(() => ({
useGetCasesMock.mockReturnValue({
...defaultGetCases,
data: {
...defaultGetCases.data,
Expand All @@ -141,7 +160,7 @@ describe('AllCases', () => {
},
],
},
}));
});
const wrapper = mount(
<TestProviders>
<AllCases userCanCrud={true} />
Expand Down Expand Up @@ -202,10 +221,10 @@ describe('AllCases', () => {
});
});
it('opens case when row action icon clicked', () => {
useGetCasesMock.mockImplementation(() => ({
useGetCasesMock.mockReturnValue({
...defaultGetCases,
filterOptions: { ...defaultGetCases.filterOptions, status: 'closed' },
}));
});

const wrapper = mount(
<TestProviders>
Expand All @@ -223,10 +242,11 @@ describe('AllCases', () => {
});
});
it('Bulk delete', () => {
useGetCasesMock.mockImplementation(() => ({
useGetCasesMock.mockReturnValue({
...defaultGetCases,
selectedCases: useGetCasesMockState.data.cases,
}));
});

useDeleteCasesMock
.mockReturnValueOnce({
...defaultDeleteCases,
Expand Down Expand Up @@ -257,10 +277,10 @@ describe('AllCases', () => {
);
});
it('Bulk close status update', () => {
useGetCasesMock.mockImplementation(() => ({
useGetCasesMock.mockReturnValue({
...defaultGetCases,
selectedCases: useGetCasesMockState.data.cases,
}));
});

const wrapper = mount(
<TestProviders>
Expand All @@ -272,14 +292,14 @@ describe('AllCases', () => {
expect(updateBulkStatus).toBeCalledWith(useGetCasesMockState.data.cases, 'closed');
});
it('Bulk open status update', () => {
useGetCasesMock.mockImplementation(() => ({
useGetCasesMock.mockReturnValue({
...defaultGetCases,
selectedCases: useGetCasesMockState.data.cases,
filterOptions: {
...defaultGetCases.filterOptions,
status: 'closed',
},
}));
});

const wrapper = mount(
<TestProviders>
Expand All @@ -291,10 +311,10 @@ describe('AllCases', () => {
expect(updateBulkStatus).toBeCalledWith(useGetCasesMockState.data.cases, 'open');
});
it('isDeleted is true, refetch', () => {
useDeleteCasesMock.mockImplementation(() => ({
useDeleteCasesMock.mockReturnValue({
...defaultDeleteCases,
isDeleted: true,
}));
});

mount(
<TestProviders>
Expand All @@ -306,10 +326,10 @@ describe('AllCases', () => {
expect(dispatchResetIsDeleted).toBeCalled();
});
it('isUpdated is true, refetch', () => {
useUpdateCasesMock.mockImplementation(() => ({
useUpdateCasesMock.mockReturnValue({
...defaultUpdateCases,
isUpdated: true,
}));
});

mount(
<TestProviders>
Expand All @@ -320,4 +340,96 @@ describe('AllCases', () => {
expect(fetchCasesStatus).toBeCalled();
expect(dispatchResetIsUpdated).toBeCalled();
});

it('should not render header when modal=true', () => {
const wrapper = mount(
<TestProviders>
<AllCases userCanCrud={true} isModal={true} />
</TestProviders>
);

expect(wrapper.find('[data-test-subj="all-cases-header"]').exists()).toBe(false);
});

it('should not render table utility bar when modal=true', () => {
const wrapper = mount(
<TestProviders>
<AllCases userCanCrud={true} isModal={true} />
</TestProviders>
);

expect(wrapper.find('[data-test-subj="case-table-utility-bar-actions"]').exists()).toBe(false);
});

it('case table should not be selectable when modal=true', () => {
const wrapper = mount(
<TestProviders>
<AllCases userCanCrud={true} isModal={true} />
</TestProviders>
);

expect(wrapper.find('[data-test-subj="cases-table"]').first().prop('isSelectable')).toBe(false);
});

it('should call onRowClick with no cases and modal=true', () => {
useGetCasesMock.mockReturnValue({
...defaultGetCases,
data: {
...defaultGetCases.data,
total: 0,
cases: [],
},
});

const wrapper = mount(
<TestProviders>
<AllCases userCanCrud={true} isModal={true} onRowClick={onRowClick} />
</TestProviders>
);

wrapper.find('[data-test-subj="cases-table-add-case"]').first().simulate('click');
expect(onRowClick).toHaveBeenCalled();
});

it('should call navigateToApp with no cases and modal=false', () => {
useGetCasesMock.mockReturnValue({
...defaultGetCases,
data: {
...defaultGetCases.data,
total: 0,
cases: [],
},
});

const wrapper = mount(
<TestProviders>
<AllCases userCanCrud={true} isModal={false} />
</TestProviders>
);

wrapper.find('[data-test-subj="cases-table-add-case"]').first().simulate('click');
expect(navigateToApp).toHaveBeenCalledWith('securitySolution:case', { path: '/create' });
});

it('should call onRowClick when clicking a case with modal=true', () => {
const wrapper = mount(
<TestProviders>
<AllCases userCanCrud={true} isModal={true} onRowClick={onRowClick} />
</TestProviders>
);

wrapper.find('[data-test-subj="cases-table-row-1"]').first().simulate('click');
expect(onRowClick).toHaveBeenCalledWith('1');
});

it('should NOT call onRowClick when clicking a case with modal=true', () => {
const wrapper = mount(
<TestProviders>
<AllCases userCanCrud={true} isModal={false} />
</TestProviders>
);

wrapper.find('[data-test-subj="cases-table-row-1"]').first().simulate('click');
expect(onRowClick).not.toHaveBeenCalled();
});
});
Loading

0 comments on commit bc65c5e

Please sign in to comment.