Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[React18] Migrate test suites to account for testing library upgrades security-detection-rule-management #201177

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/
import React from 'react';
import { renderHook, act } from '@testing-library/react-hooks';
import { act, waitFor, renderHook } from '@testing-library/react';
import { useEnableDataFeed } from './use_enable_data_feed';
import { TestProviders } from '../../../mock';

Expand Down Expand Up @@ -78,21 +78,18 @@ describe('useSecurityJobsHelpers', () => {
resolvePromiseCb = resolve;
})
);
const { result, waitForNextUpdate } = renderHook(() => useEnableDataFeed(), {
const { result } = renderHook(() => useEnableDataFeed(), {
wrapper,
});
expect(result.current.isLoading).toBe(false);

await act(async () => {
const enableDataFeedPromise = result.current.enableDatafeed(JOB, TIMESTAMP);

await waitForNextUpdate();
expect(result.current.isLoading).toBe(true);

resolvePromiseCb({});
await enableDataFeedPromise;
expect(result.current.isLoading).toBe(false);
});

await waitFor(() => expect(result.current.isLoading).toBe(false));
});

it('does not call setupMlJob if job is already installed', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { renderHook } from '@testing-library/react-hooks';
import { waitFor, renderHook } from '@testing-library/react';
import { hasMlAdminPermissions } from '../../../../../common/machine_learning/has_ml_admin_permissions';
import { hasMlLicense } from '../../../../../common/machine_learning/has_ml_license';
import { useAppToasts } from '../../../hooks/use_app_toasts';
Expand Down Expand Up @@ -71,33 +71,31 @@ describe('useSecurityJobs', () => {
bucketSpanSeconds: 900,
};

const { result, waitForNextUpdate } = renderHook(() => useSecurityJobs(), {
const { result } = renderHook(() => useSecurityJobs(), {
wrapper: TestProviders,
});
await waitForNextUpdate();

expect(result.current.jobs).toHaveLength(6);
await waitFor(() => expect(result.current.jobs).toHaveLength(6));
expect(result.current.jobs).toEqual(expect.arrayContaining([expectedSecurityJob]));
});

it('returns those permissions', async () => {
const { result, waitForNextUpdate } = renderHook(() => useSecurityJobs(), {
const { result } = renderHook(() => useSecurityJobs(), {
wrapper: TestProviders,
});
await waitForNextUpdate();

expect(result.current.isMlAdmin).toEqual(true);
expect(result.current.isLicensed).toEqual(true);
await waitFor(() => {
expect(result.current.isMlAdmin).toEqual(true);
expect(result.current.isLicensed).toEqual(true);
});
});

it('renders a toast error if an ML call fails', async () => {
(getModules as jest.Mock).mockRejectedValue('whoops');
const { waitFor } = renderHook(() => useSecurityJobs(), {
renderHook(() => useSecurityJobs(), {
wrapper: TestProviders,
});

// addError might be called after an arbitrary number of renders, so we
// need to use waitFor here instead of waitForNextUpdate
// need to use waitFor here instead of waitFor
await waitFor(() => {
expect(appToastsMock.addError).toHaveBeenCalledWith('whoops', {
title: 'Security job fetch failure',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
/* eslint-disable no-restricted-imports */

import { renderHook, cleanup } from '@testing-library/react-hooks';
import { renderHook, cleanup } from '@testing-library/react';
// eslint-disable-next-line no-restricted-imports
import type { UseLegacyUrlRedirectParams } from './use_redirect_legacy_url';
// eslint-disable-next-line no-restricted-imports
import { useLegacyUrlRedirect } from './use_redirect_legacy_url';
import type { Rule } from '../../../rule_management/logic';
import type { SpacesApi } from '@kbn/spaces-plugin/public';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { cleanup, renderHook } from '@testing-library/react-hooks';
import { renderHook, cleanup } from '@testing-library/react';
import type { UseRuleDetailsTabsProps } from './use_rule_details_tabs';
import { RuleDetailTabs, useRuleDetailsTabs } from './use_rule_details_tabs';
import type { Rule } from '../../../rule_management/logic';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { renderHook } from '@testing-library/react-hooks';
import { renderHook } from '@testing-library/react';
import { BulkActionTypeEnum } from '../../../../../common/api/detection_engine/rule_management';
import { useAppToasts } from '../../../../common/hooks/use_app_toasts';
import { useRulesTableContextOptional } from '../../../rule_management_ui/components/rules_table/rules_table/rules_table_context';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { renderHook } from '@testing-library/react-hooks';
import { renderHook } from '@testing-library/react';
import { BulkActionTypeEnum } from '../../../../../common/api/detection_engine/rule_management';
import { useAppToasts } from '../../../../common/hooks/use_app_toasts';
import { METRIC_TYPE, TELEMETRY_EVENT, track } from '../../../../common/lib/telemetry';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { renderHook } from '@testing-library/react-hooks';

import { renderHook } from '@testing-library/react';
import type { Type } from '@kbn/securitysolution-io-ts-alerting-types';
import { useAlertSuppression } from './use_alert_suppression';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@
* 2.0.
*/

import { act, renderHook } from '@testing-library/react-hooks';
import { act, waitFor, renderHook } from '@testing-library/react';

import { coreMock } from '@kbn/core/public/mocks';

import * as api from '../api/api';
import { getRulesSchemaMock } from '../../../../common/api/detection_engine/model/rule_schema/mocks';
import type {
ReturnUseDisassociateExceptionList,
UseDisassociateExceptionListProps,
} from './use_disassociate_exception_list';
import { useDisassociateExceptionList } from './use_disassociate_exception_list';

const mockKibanaHttpService = coreMock.createStart().http;
Expand All @@ -33,10 +29,7 @@ describe('useDisassociateExceptionList', () => {

test('initializes hook', async () => {
await act(async () => {
const { result, waitForNextUpdate } = renderHook<
UseDisassociateExceptionListProps,
ReturnUseDisassociateExceptionList
>(() =>
const { result } = renderHook(() =>
useDisassociateExceptionList({
http: mockKibanaHttpService,
ruleRuleId: 'rule_id',
Expand All @@ -45,9 +38,7 @@ describe('useDisassociateExceptionList', () => {
})
);

await waitForNextUpdate();

expect(result.current).toEqual([false, null]);
await waitFor(() => expect(result.current).toEqual([false, null]));
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { renderHook } from '@testing-library/react-hooks';
import { renderHook } from '@testing-library/react';

import { useRuleIndices } from './use_rule_indices';
import { useGetInstalledJob } from '../../../common/components/ml/hooks/use_get_jobs';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import React from 'react';
import type { PropsWithChildren } from 'react';
import { renderHook } from '@testing-library/react-hooks';
import { renderHook } from '@testing-library/react';
import { useUiSetting$ } from '../../../../../common/lib/kibana';
import type { Rule, RulesSnoozeSettingsMap } from '../../../../rule_management/logic';
import { useFindRules } from '../../../../rule_management/logic/use_find_rules';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { renderHook } from '@testing-library/react-hooks';
import { renderHook } from '@testing-library/react';
import { RULES_TABLE_MAX_PAGE_SIZE } from '../../../../../../common/constants';
import type {
RulesTableStorageSavedState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { renderHook } from '@testing-library/react-hooks';
import { renderHook } from '@testing-library/react';
import { useReplaceUrlParams } from '../../../../../common/utils/global_query_string/helpers';
import { useKibana } from '../../../../../common/lib/kibana';
import { URL_PARAM_KEY } from '../../../../../common/hooks/use_url_state';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { renderHook, cleanup } from '@testing-library/react-hooks';
import { cleanup, waitFor, renderHook } from '@testing-library/react';

import {
LogLevelEnum,
Expand Down Expand Up @@ -39,9 +39,9 @@ describe('useExecutionEvents', () => {
it('calls the API via fetchRuleExecutionEvents', async () => {
const fetchRuleExecutionEvents = jest.spyOn(api, 'fetchRuleExecutionEvents');

const { waitForNextUpdate } = render();
render();

await waitForNextUpdate();
await waitFor(() => new Promise((resolve) => resolve(null)));

expect(fetchRuleExecutionEvents).toHaveBeenCalledTimes(1);
expect(fetchRuleExecutionEvents).toHaveBeenLastCalledWith(
Expand All @@ -50,63 +50,63 @@ describe('useExecutionEvents', () => {
});

it('fetches data from the API', async () => {
const { result, waitForNextUpdate } = render();
const { result } = render();

// It starts from a loading state
expect(result.current.isLoading).toEqual(true);
expect(result.current.isSuccess).toEqual(false);
expect(result.current.isError).toEqual(false);

// When fetchRuleExecutionEvents returns
await waitForNextUpdate();

// It switches to a success state
expect(result.current.isLoading).toEqual(false);
expect(result.current.isSuccess).toEqual(true);
expect(result.current.isError).toEqual(false);
expect(result.current.data).toEqual({
events: [
{
timestamp: '2021-12-29T10:42:59.996Z',
sequence: 0,
level: LogLevelEnum.info,
type: RuleExecutionEventTypeEnum['status-change'],
execution_id: 'execution-id-1',
message: 'Rule changed status to "succeeded". Rule execution completed without errors',
await waitFor(() => {
// It switches to a success state
expect(result.current.isLoading).toEqual(false);
expect(result.current.isSuccess).toEqual(true);
expect(result.current.isError).toEqual(false);
expect(result.current.data).toEqual({
events: [
{
timestamp: '2021-12-29T10:42:59.996Z',
sequence: 0,
level: LogLevelEnum.info,
type: RuleExecutionEventTypeEnum['status-change'],
execution_id: 'execution-id-1',
message: 'Rule changed status to "succeeded". Rule execution completed without errors',
},
],
pagination: {
page: 1,
per_page: 20,
total: 1,
},
],
pagination: {
page: 1,
per_page: 20,
total: 1,
},
});
});
});

it('handles exceptions from the API', async () => {
const exception = new Error('Boom!');
jest.spyOn(api, 'fetchRuleExecutionEvents').mockRejectedValue(exception);

const { result, waitForNextUpdate } = render();
const { result } = render();

// It starts from a loading state
expect(result.current.isLoading).toEqual(true);
expect(result.current.isSuccess).toEqual(false);
expect(result.current.isError).toEqual(false);

// When fetchRuleExecutionEvents throws
await waitForNextUpdate();

// It switches to an error state
expect(result.current.isLoading).toEqual(false);
expect(result.current.isSuccess).toEqual(false);
expect(result.current.isError).toEqual(true);
expect(result.current.error).toEqual(exception);

// And shows a toast with the caught exception
expect(useToasts().addError).toHaveBeenCalledTimes(1);
expect(useToasts().addError).toHaveBeenCalledWith(exception, {
title: 'Failed to fetch rule execution events',
await waitFor(() => {
// It switches to an error state
expect(result.current.isLoading).toEqual(false);
expect(result.current.isSuccess).toEqual(false);
expect(result.current.isError).toEqual(true);
expect(result.current.error).toEqual(exception);

// And shows a toast with the caught exception
expect(useToasts().addError).toHaveBeenCalledTimes(1);
expect(useToasts().addError).toHaveBeenCalledWith(exception, {
title: 'Failed to fetch rule execution events',
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { renderHook, cleanup } from '@testing-library/react-hooks';
import { cleanup, waitFor, renderHook } from '@testing-library/react';

import { useExecutionResults } from './use_execution_results';
import { useToasts } from '../../../../common/lib/kibana';
Expand Down Expand Up @@ -48,29 +48,27 @@ describe('useExecutionResults', () => {
it('calls the API via fetchRuleExecutionResults', async () => {
const fetchRuleExecutionResults = jest.spyOn(api, 'fetchRuleExecutionResults');

const { waitForNextUpdate } = render();
render();

await waitForNextUpdate();
await waitFor(() => expect(fetchRuleExecutionResults).toHaveBeenCalledTimes(1));

expect(fetchRuleExecutionResults).toHaveBeenCalledTimes(1);
expect(fetchRuleExecutionResults).toHaveBeenLastCalledWith(
expect.objectContaining({ ruleId: SOME_RULE_ID })
);
});

it('fetches data from the API', async () => {
const { result, waitForNextUpdate } = render();
const { result } = render();

// It starts from a loading state
expect(result.current.isLoading).toEqual(true);
expect(result.current.isSuccess).toEqual(false);
expect(result.current.isError).toEqual(false);

// When fetchRuleExecutionEvents returns
await waitForNextUpdate();
await waitFor(() => expect(result.current.isLoading).toEqual(false));

// It switches to a success state
expect(result.current.isLoading).toEqual(false);
expect(result.current.isSuccess).toEqual(true);
expect(result.current.isError).toEqual(false);
expect(result.current.data).toEqual({
Expand Down Expand Up @@ -107,18 +105,17 @@ describe('useExecutionResults', () => {
const exception = new Error('Boom!');
jest.spyOn(api, 'fetchRuleExecutionResults').mockRejectedValue(exception);

const { result, waitForNextUpdate } = render();
const { result } = render();

// It starts from a loading state
expect(result.current.isLoading).toEqual(true);
expect(result.current.isSuccess).toEqual(false);
expect(result.current.isError).toEqual(false);

// When fetchRuleExecutionEvents throws
await waitForNextUpdate();
await waitFor(() => expect(result.current.isLoading).toEqual(false));

// It switches to an error state
expect(result.current.isLoading).toEqual(false);
expect(result.current.isSuccess).toEqual(false);
expect(result.current.isError).toEqual(true);
expect(result.current.error).toEqual(exception);
Expand Down
Loading