Skip to content

Commit

Permalink
[Alerting] Fixed Jest test suites with unhandled promise rejections (#…
Browse files Browse the repository at this point in the history
…113213)

* [Alerting] Fixed Jest test suites with unhandled promise rejections

* fixed typecheck
  • Loading branch information
YulNaumenko authored Sep 28, 2021
1 parent 25c946c commit 3dcfd44
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 28 deletions.
11 changes: 9 additions & 2 deletions x-pack/plugins/stack_alerts/server/feature.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,20 @@ import { featuresPluginMock } from '../../features/server/mocks';
import { BUILT_IN_ALERTS_FEATURE } from './feature';

describe('Stack Alerts Feature Privileges', () => {
test('feature privilege should contain all built-in rule types', async () => {
test('feature privilege should contain all built-in rule types', () => {
const context = coreMock.createPluginInitializerContext();
const plugin = new AlertingBuiltinsPlugin(context);
const coreSetup = coreMock.createSetup();
coreSetup.getStartServices = jest.fn().mockResolvedValue([
{
application: {},
},
{ triggersActionsUi: {} },
]);

const alertingSetup = alertsMock.createSetup();
const featuresSetup = featuresPluginMock.createSetup();
await plugin.setup(coreSetup, { alerting: alertingSetup, features: featuresSetup });
plugin.setup(coreSetup, { alerting: alertingSetup, features: featuresSetup });

const typesInFeaturePrivilege = BUILT_IN_ALERTS_FEATURE.alerting ?? [];
const typesInFeaturePrivilegeAll =
Expand Down
10 changes: 8 additions & 2 deletions x-pack/plugins/stack_alerts/server/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,18 @@ describe('AlertingBuiltins Plugin', () => {
context = coreMock.createPluginInitializerContext();
plugin = new AlertingBuiltinsPlugin(context);
coreSetup = coreMock.createSetup();
coreSetup.getStartServices = jest.fn().mockResolvedValue([
{
application: {},
},
{ triggersActionsUi: {} },
]);
});

it('should register built-in alert types', async () => {
it('should register built-in alert types', () => {
const alertingSetup = alertsMock.createSetup();
const featuresSetup = featuresPluginMock.createSetup();
await plugin.setup(coreSetup, { alerting: alertingSetup, features: featuresSetup });
plugin.setup(coreSetup, { alerting: alertingSetup, features: featuresSetup });

expect(alertingSetup.registerType).toHaveBeenCalledTimes(3);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,26 @@
*/

import * as React from 'react';
import { mountWithIntl } from '@kbn/test/jest';
import { mountWithIntl, nextTick } from '@kbn/test/jest';
import { act } from 'react-dom/test-utils';
import ConnectorAddModal from './connector_add_modal';
import { actionTypeRegistryMock } from '../../action_type_registry.mock';
import { ActionType, ConnectorValidationResult, GenericValidationResult } from '../../../types';
import { useKibana } from '../../../common/lib/kibana';
import { coreMock } from '../../../../../../../src/core/public/mocks';

jest.mock('../../../common/lib/kibana');
const mocks = coreMock.createSetup();
const actionTypeRegistry = actionTypeRegistryMock.create();
const useKibanaMock = useKibana as jest.Mocked<typeof useKibana>;

describe('connector_add_modal', () => {
beforeAll(async () => {
const mockes = coreMock.createSetup();
const [
{
application: { capabilities },
},
] = await mocks.getStartServices();
] = await mockes.getStartServices();
useKibanaMock().services.application.capabilities = {
...capabilities,
actions: {
Expand All @@ -34,21 +35,22 @@ describe('connector_add_modal', () => {
},
};
});
it('renders connector modal form if addModalVisible is true', () => {

it('renders connector modal form if addModalVisible is true', async () => {
const actionTypeModel = actionTypeRegistryMock.createMockActionTypeModel({
id: 'my-action-type',
iconClass: 'test',
selectMessage: 'test',
validateConnector: (): Promise<ConnectorValidationResult<unknown, unknown>> => {
return Promise.resolve({});
return Promise.resolve({ config: { errors: {} }, secrets: { errors: {} } });
},
validateParams: (): Promise<GenericValidationResult<unknown>> => {
const validationResult = { errors: {} };
return Promise.resolve(validationResult);
},
actionConnectorFields: null,
});
actionTypeRegistry.get.mockReturnValueOnce(actionTypeModel);
actionTypeRegistry.get.mockReturnValue(actionTypeModel);
actionTypeRegistry.has.mockReturnValue(true);

const actionType: ActionType = {
Expand All @@ -67,6 +69,11 @@ describe('connector_add_modal', () => {
actionTypeRegistry={actionTypeRegistry}
/>
);

await act(async () => {
await nextTick();
wrapper.update();
});
expect(wrapper.exists('.euiModalHeader')).toBeTruthy();
expect(wrapper.exists('[data-test-subj="saveActionButtonModal"]')).toBeTruthy();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@

import type { estypes } from '@elastic/elasticsearch';
import { loggingSystemMock } from '../../../../../../src/core/server/mocks';
import {
TimeSeriesQueryParameters,
TimeSeriesQuery,
timeSeriesQuery,
getResultFromEs,
} from './time_series_query';
import { Logger } from '../../../../../../src/core/server';
import { TimeSeriesQuery, timeSeriesQuery, getResultFromEs } from './time_series_query';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { elasticsearchClientMock } from '../../../../../../src/core/server/elasticsearch/client/mocks';

Expand All @@ -34,22 +30,22 @@ const DefaultQueryParams: TimeSeriesQuery = {
};

describe('timeSeriesQuery', () => {
let params: TimeSeriesQueryParameters;
const esClient = elasticsearchClientMock.createClusterClient().asScoped().asCurrentUser;

beforeEach(async () => {
params = {
logger: loggingSystemMock.create().get(),
esClient,
query: DefaultQueryParams,
};
});
const logger = loggingSystemMock.create().get() as jest.Mocked<Logger>;
const params = {
logger,
esClient,
query: DefaultQueryParams,
};

it('fails as expected when the callCluster call fails', async () => {
esClient.search = jest.fn().mockRejectedValue(new Error('woopsie'));
expect(timeSeriesQuery(params)).rejects.toThrowErrorMatchingInlineSnapshot(
`"error running search"`
);
await timeSeriesQuery(params);
expect(logger.warn.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"indexThreshold timeSeriesQuery: callCluster error: woopsie",
]
`);
});

it('fails as expected when the query params are invalid', async () => {
Expand Down

0 comments on commit 3dcfd44

Please sign in to comment.