Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Jan 7, 2021
1 parent 4773519 commit d725669
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 361 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import { createMockSavedObjectsRepository } from '../../routes/api/__fixtures__'
import { createCaseClientWithMockSavedObjectsClient } from '../mocks';

describe('updateAlertsStatus', () => {
beforeEach(async () => {
jest.restoreAllMocks();
});

describe('happy path', () => {
test('it update the status of the alert correctly', async () => {
const savedObjectsClient = createMockSavedObjectsRepository();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ import { CASE_STATUS_URL } from '../../../../../common/constants';

describe('GET status', () => {
let routeHandler: RequestHandler<any, any, any>;
const findArgs = {
fields: [],
page: 1,
perPage: 1,
type: 'cases',
};

beforeAll(async () => {
routeHandler = await createRoute(initGetCasesStatusApi, 'get');
});
Expand All @@ -35,27 +42,18 @@ describe('GET status', () => {

const response = await routeHandler(theContext, request, kibanaResponseFactory);
expect(theContext.core.savedObjects.client.find).toHaveBeenNthCalledWith(1, {
fields: [],
...findArgs,
filter: 'cases.attributes.status: open',
page: 1,
perPage: 1,
type: 'cases',
});

expect(theContext.core.savedObjects.client.find).toHaveBeenNthCalledWith(2, {
fields: [],
...findArgs,
filter: 'cases.attributes.status: in-progress',
page: 1,
perPage: 1,
type: 'cases',
});

expect(theContext.core.savedObjects.client.find).toHaveBeenNthCalledWith(3, {
fields: [],
...findArgs,
filter: 'cases.attributes.status: closed',
page: 1,
perPage: 1,
type: 'cases',
});

expect(response.payload).toEqual({
Expand Down
28 changes: 11 additions & 17 deletions x-pack/plugins/case/server/services/alerts/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ describe('updateAlertsStatus', () => {

describe('happy path', () => {
let alertService: AlertServiceContract;
const args = {
ids: ['alert-id-1'],
index: '.siem-signals',
request: {} as KibanaRequest,
status: CaseStatuses.closed,
};

beforeEach(async () => {
alertService = new AlertService();
Expand All @@ -22,21 +28,16 @@ describe('updateAlertsStatus', () => {

test('it update the status of the alert correctly', async () => {
alertService.initialize(esClientMock);
await alertService.updateAlertsStatus({
ids: ['alert-id-1'],
index: '.siem-signals',
request: {} as KibanaRequest,
status: CaseStatuses.closed,
});
await alertService.updateAlertsStatus(args);

expect(esClientMock.asScoped().asCurrentUser.updateByQuery).toHaveBeenCalledWith({
body: {
query: { ids: { values: ['alert-id-1'] } },
script: { lang: 'painless', source: "ctx._source.signal.status = 'closed'" },
query: { ids: { values: args.ids } },
script: { lang: 'painless', source: `ctx._source.signal.status = '${args.status}'` },
},
conflicts: 'abort',
ignore_unavailable: true,
index: '.siem-signals',
index: args.index,
});
});

Expand All @@ -49,14 +50,7 @@ describe('updateAlertsStatus', () => {
});

test('it throws when service is not initialized and try to update the status', async () => {
await expect(
alertService.updateAlertsStatus({
ids: ['alert-id-1'],
index: '.siem-signals',
request: {} as KibanaRequest,
status: CaseStatuses.closed,
})
).rejects.toThrow();
await expect(alertService.updateAlertsStatus(args)).rejects.toThrow();
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,20 @@ const stats = {

describe('StatusFilter', () => {
const onStatusChanged = jest.fn();
const defaultProps = {
selectedStatus: CaseStatuses.open,
onStatusChanged,
stats,
};

it('should render', () => {
const wrapper = mount(
<StatusFilter
onStatusChanged={onStatusChanged}
selectedStatus={CaseStatuses.open}
stats={stats}
/>
);
const wrapper = mount(<StatusFilter {...defaultProps} />);

expect(wrapper.find('[data-test-subj="case-status-filter"]').exists()).toBeTruthy();
});

it('should call onStatusChanged when changing status to open', async () => {
const wrapper = mount(
<StatusFilter
onStatusChanged={onStatusChanged}
selectedStatus={CaseStatuses.open}
stats={stats}
/>
);
const wrapper = mount(<StatusFilter {...defaultProps} />);

await waitFor(() => {
wrapper.find('button[data-test-subj="case-status-filter"]').simulate('click');
Expand All @@ -49,13 +42,7 @@ describe('StatusFilter', () => {
});

it('should call onStatusChanged when changing status to in-progress', async () => {
const wrapper = mount(
<StatusFilter
onStatusChanged={onStatusChanged}
selectedStatus={CaseStatuses.open}
stats={stats}
/>
);
const wrapper = mount(<StatusFilter {...defaultProps} />);

await waitFor(() => {
wrapper.find('button[data-test-subj="case-status-filter"]').simulate('click');
Expand All @@ -65,13 +52,7 @@ describe('StatusFilter', () => {
});

it('should call onStatusChanged when changing status to closed', async () => {
const wrapper = mount(
<StatusFilter
onStatusChanged={onStatusChanged}
selectedStatus={CaseStatuses.open}
stats={stats}
/>
);
const wrapper = mount(<StatusFilter {...defaultProps} />);

await waitFor(() => {
wrapper.find('button[data-test-subj="case-status-filter"]').simulate('click');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ import { TestProviders } from '../../../common/mock';
describe('CaseActionBar', () => {
const onRefresh = jest.fn();
const onUpdateField = jest.fn();
const defaultProps = {
caseData: basicCase,
isLoading: false,
onRefresh,
onUpdateField,
currentExternalIncident: null,
};

beforeEach(() => {
jest.clearAllMocks();
Expand All @@ -22,13 +29,7 @@ describe('CaseActionBar', () => {
it('it renders', () => {
const wrapper = mount(
<TestProviders>
<CaseActionBar
caseData={basicCase}
isLoading={false}
onRefresh={onRefresh}
onUpdateField={onUpdateField}
currentExternalIncident={null}
/>
<CaseActionBar {...defaultProps} />
</TestProviders>
);

Expand All @@ -43,13 +44,7 @@ describe('CaseActionBar', () => {
it('it should show correct status', () => {
const wrapper = mount(
<TestProviders>
<CaseActionBar
caseData={basicCase}
isLoading={false}
onRefresh={onRefresh}
onUpdateField={onUpdateField}
currentExternalIncident={null}
/>
<CaseActionBar {...defaultProps} />
</TestProviders>
);

Expand All @@ -61,13 +56,7 @@ describe('CaseActionBar', () => {
it('it should show the correct date', () => {
const wrapper = mount(
<TestProviders>
<CaseActionBar
caseData={basicCase}
isLoading={false}
onRefresh={onRefresh}
onUpdateField={onUpdateField}
currentExternalIncident={null}
/>
<CaseActionBar {...defaultProps} />
</TestProviders>
);

Expand All @@ -79,13 +68,7 @@ describe('CaseActionBar', () => {
it('it call onRefresh', () => {
const wrapper = mount(
<TestProviders>
<CaseActionBar
caseData={basicCase}
isLoading={false}
onRefresh={onRefresh}
onUpdateField={onUpdateField}
currentExternalIncident={null}
/>
<CaseActionBar {...defaultProps} />
</TestProviders>
);

Expand All @@ -96,13 +79,7 @@ describe('CaseActionBar', () => {
it('it should call onUpdateField when changing status', () => {
const wrapper = mount(
<TestProviders>
<CaseActionBar
caseData={basicCase}
isLoading={false}
onRefresh={onRefresh}
onUpdateField={onUpdateField}
currentExternalIncident={null}
/>
<CaseActionBar {...defaultProps} />
</TestProviders>
);

Expand All @@ -117,13 +94,7 @@ describe('CaseActionBar', () => {
it('it should call onUpdateField when changing syncAlerts setting', () => {
const wrapper = mount(
<TestProviders>
<CaseActionBar
caseData={basicCase}
isLoading={false}
onRefresh={onRefresh}
onUpdateField={onUpdateField}
currentExternalIncident={null}
/>
<CaseActionBar {...defaultProps} />
</TestProviders>
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import React from 'react';
import { mount } from 'enzyme';
import { act } from '@testing-library/react';
import { waitFor } from '@testing-library/react';

import { SyncAlertsSwitch } from './sync_alerts_switch';

Expand All @@ -20,14 +20,13 @@ describe('SyncAlertsSwitch', () => {
it('it toggles the switch', async () => {
const wrapper = mount(<SyncAlertsSwitch disabled={false} />);

act(() => {
wrapper.find('button[data-test-subj="sync-alerts-switch"]').first().simulate('click');
});
wrapper.find('button[data-test-subj="sync-alerts-switch"]').first().simulate('click');

wrapper.update();
expect(wrapper.find('[data-test-subj="sync-alerts-switch"]').first().prop('checked')).toBe(
false
);
await waitFor(() => {
expect(wrapper.find('[data-test-subj="sync-alerts-switch"]').first().prop('checked')).toBe(
false
);
});
});

it('it disables the switch', async () => {
Expand All @@ -48,13 +47,10 @@ describe('SyncAlertsSwitch', () => {
const wrapper = mount(<SyncAlertsSwitch disabled={false} showLabel={true} />);

expect(wrapper.find('[data-test-subj="sync-alerts-switch"]').first().text()).toBe('On');
wrapper.find('button[data-test-subj="sync-alerts-switch"]').first().simulate('click');

act(() => {
wrapper.find('button[data-test-subj="sync-alerts-switch"]').first().simulate('click');
await waitFor(() => {
expect(wrapper.find(`[data-test-subj="sync-alerts-switch"]`).first().text()).toBe('Off');
});

wrapper.update();

expect(wrapper.find(`[data-test-subj="sync-alerts-switch"]`).first().text()).toBe('Off');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ describe('CaseView ', () => {
useConnectorsMock.mockImplementation(() => ({ connectors: connectorsMock, isLoading: false }));
useQueryAlertsMock.mockImplementation(() => ({
loading: false,
data: { hits: { hits: [...alertsHit] } },
data: { hits: { hits: alertsHit } },
}));
});

Expand Down
Loading

0 comments on commit d725669

Please sign in to comment.