Skip to content

Commit

Permalink
[Security Solution] Fix the flaky useSecurityJobs test (#153576)
Browse files Browse the repository at this point in the history
**Resolves: #153550

## Summary

Fix the flaky `useSecurityJobs` test.
  • Loading branch information
xcrzx authored Mar 24, 2023
1 parent e8944b5 commit aa192f5
Showing 1 changed file with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,20 @@
*/

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

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';
import { useAppToastsMock } from '../../../hooks/use_app_toasts.mock';
import { TestProviders } from '../../../mock';
import { getJobsSummary } from '../../ml/api/get_jobs_summary';
import { checkRecognizer, getModules } from '../api';
import type { SecurityJob } from '../types';
import {
mockJobsSummaryResponse,
mockGetModuleResponse,
checkRecognizerSuccess,
mockGetModuleResponse,
mockJobsSummaryResponse,
} from '../api.mock';
import type { SecurityJob } from '../types';
import { useSecurityJobs } from './use_security_jobs';
import { TestProviders } from '../../../mock';

jest.mock('../../../../../common/machine_learning/has_ml_admin_permissions');
jest.mock('../../../../../common/machine_learning/has_ml_license');
Expand All @@ -30,8 +29,7 @@ jest.mock('../../ml/hooks/use_ml_capabilities');
jest.mock('../../ml/api/get_jobs_summary');
jest.mock('../api');

// FLAKY: https://github.com/elastic/kibana/issues/153550
describe.skip('useSecurityJobs', () => {
describe('useSecurityJobs', () => {
let appToastsMock: jest.Mocked<ReturnType<typeof useAppToastsMock.create>>;

beforeEach(() => {
Expand Down Expand Up @@ -94,13 +92,16 @@ describe.skip('useSecurityJobs', () => {

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

expect(appToastsMock.addError).toHaveBeenCalledWith('whoops', {
title: 'Security job fetch failure',
// addError might be called after an arbitrary number of renders, so we
// need to use waitFor here instead of waitForNextUpdate
await waitFor(() => {
expect(appToastsMock.addError).toHaveBeenCalledWith('whoops', {
title: 'Security job fetch failure',
});
});
});
});
Expand Down

0 comments on commit aa192f5

Please sign in to comment.