Skip to content

Commit

Permalink
switches tests to use internal mock
Browse files Browse the repository at this point in the history
  • Loading branch information
dplumlee committed Aug 13, 2020
1 parent 4452cd7 commit 3a57767
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,26 @@ import { useFetchOrCreateRuleExceptionList } from '../use_fetch_or_create_rule_e
import { useSignalIndex } from '../../../../detections/containers/detection_engine/alerts/use_signal_index';
import { createUseKibanaMock } from '../../../mock/kibana_react';
import { TimelineNonEcsData, Ecs } from '../../../../graphql/types';
import { ExceptionBuilderComponent } from '../builder';
import * as helpers from '../helpers';

jest.mock('../../../../detections/containers/detection_engine/alerts/use_signal_index');
jest.mock('../../../../common/lib/kibana');
jest.mock('../../../../detections/containers/detection_engine/rules');
jest.mock('../use_add_exception');
jest.mock('../use_fetch_or_create_rule_exception_list');
jest.mock('../builder');

const useKibanaMock = useKibana as jest.Mock;

describe('When the add exception modal is opened', () => {
const ruleName = 'test rule';
let defaultEndpointItems: jest.SpyInstance<ReturnType<
typeof helpers.defaultEndpointExceptionItems
>>;

beforeEach(() => {
defaultEndpointItems = jest.spyOn(helpers, 'defaultEndpointExceptionItems');
const kibanaMock = createUseKibanaMock()();
useKibanaMock.mockImplementation(() => ({
...kibanaMock,
Expand All @@ -55,6 +62,12 @@ describe('When the add exception modal is opened', () => {
},
]);
(useCurrentUser as jest.Mock).mockReturnValue({ username: 'test-username' });
// Unsafe casting for mocking of internal component
((ExceptionBuilderComponent as unknown) as jest.Mock).mockReturnValue(null);
});

afterEach(() => {
jest.restoreAllMocks();
});

describe('when the modal is loading', () => {
Expand Down Expand Up @@ -104,11 +117,6 @@ describe('When the add exception modal is opened', () => {
it('should render the exception builder', () => {
expect(wrapper.find('[data-test-subj="alert-exception-builder"]').exists()).toBeTruthy();
});
it('has the add exception button disabled', () => {
expect(
wrapper.find('button[data-test-subj="add-exception-confirm-button"]').getDOMNode()
).toBeDisabled();
});
it('should not render the close on add exception checkbox', () => {
expect(
wrapper.find('[data-test-subj="close-alert-on-add-add-exception-checkbox"]').exists()
Expand All @@ -118,7 +126,7 @@ describe('When the add exception modal is opened', () => {

describe('when there is alert data passed to an endpoint list exception', () => {
let wrapper: ReactWrapper;
beforeAll(() => {
beforeEach(() => {
const alertDataMock: { ecsData: Ecs; nonEcsData: TimelineNonEcsData[] } = {
ecsData: { _id: 'test-id' },
nonEcsData: [{ field: 'file.path', value: ['test/path'] }],
Expand All @@ -140,22 +148,47 @@ describe('When the add exception modal is opened', () => {
it('should render the exception builder', () => {
expect(wrapper.find('[data-test-subj="alert-exception-builder"]').exists()).toBeTruthy();
});
it('has the add exception button enabled', () => {
expect(
wrapper.find('button[data-test-subj="add-exception-confirm-button"]').getDOMNode()
).not.toBeDisabled();
it('should prepopulate endpoint items', () => {
expect(defaultEndpointItems).toHaveBeenCalled();
});
it('should render the close on add exception checkbox', () => {
expect(
wrapper.find('[data-test-subj="close-alert-on-add-add-exception-checkbox"]').exists()
).toBeTruthy();
});
it('has the bulk close on add exception disabled', () => {
});

describe('when there is alert data passed to a detection list exception', () => {
let wrapper: ReactWrapper;
beforeEach(() => {
const alertDataMock: { ecsData: Ecs; nonEcsData: TimelineNonEcsData[] } = {
ecsData: { _id: 'test-id' },
nonEcsData: [{ field: 'file.path', value: ['test/path'] }],
};
wrapper = mount(
<ThemeProvider theme={() => ({ eui: euiLightVars, darkMode: false })}>
<AddExceptionModal
ruleId={'123'}
ruleIndices={['filebeat-*']}
ruleName={ruleName}
exceptionListType={'detection'}
onCancel={() => {}}
onConfirm={() => {}}
alertData={alertDataMock}
/>
</ThemeProvider>
);
});
it('should render the exception builder', () => {
expect(wrapper.find('[data-test-subj="alert-exception-builder"]').exists()).toBeTruthy();
});
it('should not prepopulate endpoint items', () => {
expect(defaultEndpointItems).not.toHaveBeenCalled();
});
it('should render the close on add exception checkbox', () => {
expect(
wrapper
.find('input[data-test-subj="bulk-close-alert-on-add-add-exception-checkbox"]')
.getDOMNode()
).toBeDisabled();
wrapper.find('[data-test-subj="close-alert-on-add-add-exception-checkbox"]').exists()
).toBeTruthy();
});
});

Expand Down Expand Up @@ -199,22 +232,13 @@ describe('When the add exception modal is opened', () => {
it('should render the exception builder', () => {
expect(wrapper.find('[data-test-subj="alert-exception-builder"]').exists()).toBeTruthy();
});
it('has the add exception button enabled', () => {
expect(
wrapper.find('button[data-test-subj="add-exception-confirm-button"]').getDOMNode()
).not.toBeDisabled();
it('should prepopulate endpoint items', () => {
expect(defaultEndpointItems).toHaveBeenCalled();
});
it('should render the close on add exception checkbox', () => {
expect(
wrapper.find('[data-test-subj="close-alert-on-add-add-exception-checkbox"]').exists()
).toBeTruthy();
});
it('has the bulk close on add exception enabled', () => {
expect(
wrapper
.find('input[data-test-subj="bulk-close-alert-on-add-add-exception-checkbox"]')
.getDOMNode()
).not.toBeDisabled();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ import { useSignalIndex } from '../../../../detections/containers/detection_engi
import { createUseKibanaMock } from '../../../mock/kibana_react';
import { getExceptionListItemSchemaMock } from '../../../../../../lists/common/schemas/response/exception_list_item_schema.mock';
import { EntriesArray } from '../../../../../../lists/common/schemas/types';
import { ExceptionBuilderComponent } from '../builder';

jest.mock('../../../../common/lib/kibana');
jest.mock('../../../../detections/containers/detection_engine/rules');
jest.mock('../use_add_exception');
jest.mock('../use_fetch_or_create_rule_exception_list');
jest.mock('../../../../detections/containers/detection_engine/alerts/use_signal_index');
jest.mock('../builder');

const useKibanaMock = useKibana as jest.Mock;

Expand All @@ -53,6 +55,8 @@ describe('When the edit exception modal is opened', () => {
},
]);
(useCurrentUser as jest.Mock).mockReturnValue({ username: 'test-username' });
// Unsafe casting for mocking of internal component
((ExceptionBuilderComponent as unknown) as jest.Mock).mockReturnValue(null);
});

describe('when the modal is loading', () => {
Expand Down Expand Up @@ -109,24 +113,12 @@ describe('When the edit exception modal is opened', () => {
expect(
wrapper.find('[data-test-subj="edit-exception-modal-builder"]').exists()
).toBeTruthy();
expect(
wrapper.find('button[data-test-subj="edit-exception-confirm-button"]').getDOMNode()
).not.toBeDisabled();
});
it('should contain the endpoint specific documentation text', () => {
expect(
wrapper.find('[data-test-subj="edit-exception-endpoint-text"]').exists()
).toBeTruthy();
expect(
wrapper
.find('input[data-test-subj="close-alert-on-add-edit-exception-checkbox"]')
.getDOMNode()
).not.toBeDisabled();
});
// it('should have the confirm edit button be enabled', () => {
// });
// it('should contain the endpoint specific documentation text', () => {
// });
// it('should have the bulk close checkbox enabled', () => {
// });
});

describe('without data that matches index patterns', () => {
Expand All @@ -149,24 +141,12 @@ describe('When the edit exception modal is opened', () => {
expect(
wrapper.find('[data-test-subj="edit-exception-modal-builder"]').exists()
).toBeTruthy();
expect(
wrapper.find('button[data-test-subj="edit-exception-confirm-button"]').getDOMNode()
).not.toBeDisabled();
});
it('should contain the endpoint specific documentation text', () => {
expect(
wrapper.find('[data-test-subj="edit-exception-endpoint-text"]').exists()
).toBeTruthy();
expect(
wrapper
.find('input[data-test-subj="close-alert-on-add-edit-exception-checkbox"]')
.getDOMNode()
).toBeDisabled();
});
// it('should have the confirm edit button be enabled', () => {
// });
// it('should contain the endpoint specific documentation text', () => {
// });
// it('should have the bulk close checkbox disabled', () => {
// });
});
});

Expand All @@ -188,41 +168,9 @@ describe('When the edit exception modal is opened', () => {
});
it('renders the exceptions builder', () => {
expect(wrapper.find('[data-test-subj="edit-exception-modal-builder"]').exists()).toBeTruthy();
expect(
wrapper.find('button[data-test-subj="edit-exception-confirm-button"]').getDOMNode()
).not.toBeDisabled();
expect(wrapper.find('[data-test-subj="edit-exception-endpoint-text"]').exists()).toBeFalsy();
});
// it('should have the confirm edit button be enabled', () => {
// });
// it('should contain the endpoint specific documentation text', () => {
// });
});

describe('when an exception is passed with no exception data', () => {
let wrapper: ReactWrapper;
beforeEach(() => {
const exceptionItemMock = { ...getExceptionListItemSchemaMock(), entries: [] };
wrapper = mount(
<ThemeProvider theme={() => ({ eui: euiLightVars, darkMode: false })}>
<EditExceptionModal
ruleIndices={['filebeat-*']}
ruleName={ruleName}
exceptionListType={'detection'}
onCancel={() => {}}
onConfirm={() => {}}
exceptionItem={exceptionItemMock}
/>
</ThemeProvider>
);
});
it('renders the exceptions builder', () => {
expect(wrapper.find('[data-test-subj="edit-exception-modal-builder"]').exists()).toBeTruthy();
expect(
wrapper.find('button[data-test-subj="edit-exception-confirm-button"]').getDOMNode()
).toBeDisabled();
it('should not contain the endpoint specific documentation text', () => {
expect(wrapper.find('[data-test-subj="edit-exception-endpoint-text"]').exists()).toBeFalsy();
});
// it('should have the confirm edit button be enabled', () => {
// });
});
});

0 comments on commit 3a57767

Please sign in to comment.