Skip to content

Commit

Permalink
[RAM] Fix useAlertDataView flaky tests (elastic#177799)
Browse files Browse the repository at this point in the history
## Summary

Removes unnecessary `act` and `waitForNextUpdate` calls from
`useAlertDataView` tests. Ensures only one assertion is performed in
`waitFor` callbacks.

Fixes elastic#177250

---------

Co-authored-by: Xavier Mouligneau <[email protected]>
  • Loading branch information
umbopepato and XavierM authored Mar 12, 2024
1 parent b91962e commit a088a98
Showing 1 changed file with 40 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
import { AlertConsumers } from '@kbn/rule-data-utils';
import { createStartServicesMock } from '../../common/lib/kibana/kibana_react.mock';
import type { ValidFeatureId } from '@kbn/rule-data-utils';
import { act, renderHook } from '@testing-library/react-hooks';
import { renderHook } from '@testing-library/react-hooks/dom';
import { useAlertDataViews } from './use_alert_data_view';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import React from 'react';
import { waitFor } from '@testing-library/react';

const mockUseKibanaReturnValue = createStartServicesMock();

Expand Down Expand Up @@ -45,8 +46,7 @@ const wrapper = ({ children }: { children: Node }) => (
<QueryClientProvider client={queryClient}> {children} </QueryClientProvider>
);

// FLAKY: https://github.com/elastic/kibana/issues/177250
describe.skip('useAlertDataView', () => {
describe('useAlertDataView', () => {
const observabilityAlertFeatureIds: ValidFeatureId[] = [
AlertConsumers.APM,
AlertConsumers.INFRASTRUCTURE,
Expand All @@ -70,94 +70,66 @@ describe.skip('useAlertDataView', () => {
});

it('initially is loading and does not have data', async () => {
await act(async () => {
const mockedAsyncDataView = {
loading: true,
dataview: undefined,
};

const { result, waitForNextUpdate } = renderHook(
() => useAlertDataViews(observabilityAlertFeatureIds),
{
wrapper,
}
);
const mockedAsyncDataView = {
loading: true,
dataview: undefined,
};

await waitForNextUpdate();

expect(result.current).toEqual(mockedAsyncDataView);
const { result } = renderHook(() => useAlertDataViews(observabilityAlertFeatureIds), {
wrapper,
});

await waitFor(() => expect(result.current).toEqual(mockedAsyncDataView));
});

it('fetch index names + fields for the provided o11y featureIds', async () => {
await act(async () => {
const { waitForNextUpdate } = renderHook(
() => useAlertDataViews(observabilityAlertFeatureIds),
{
wrapper,
}
);

await waitForNextUpdate();
await waitForNextUpdate();

expect(fetchAlertIndexNames).toHaveBeenCalledTimes(1);
expect(fetchAlertFields).toHaveBeenCalledTimes(1);
renderHook(() => useAlertDataViews(observabilityAlertFeatureIds), {
wrapper,
});

await waitFor(() => expect(fetchAlertIndexNames).toHaveBeenCalledTimes(1));
expect(fetchAlertFields).toHaveBeenCalledTimes(1);
});

it('only fetch index names for security featureId', async () => {
await act(async () => {
const { waitForNextUpdate } = renderHook(() => useAlertDataViews([AlertConsumers.SIEM]), {
wrapper,
});

await waitForNextUpdate();
await waitForNextUpdate();

expect(fetchAlertIndexNames).toHaveBeenCalledTimes(1);
expect(fetchAlertFields).toHaveBeenCalledTimes(0);
renderHook(() => useAlertDataViews([AlertConsumers.SIEM]), {
wrapper,
});
});

it('Do not fetch anything if security and o11y featureIds are mix together', async () => {
await act(async () => {
const { result, waitForNextUpdate } = renderHook(
() => useAlertDataViews([AlertConsumers.SIEM, AlertConsumers.LOGS]),
{
wrapper,
}
);
await waitFor(() => expect(fetchAlertIndexNames).toHaveBeenCalledTimes(1));
expect(fetchAlertFields).toHaveBeenCalledTimes(0);
});

await waitForNextUpdate();
it('Do not fetch anything if security and o11y featureIds are mixed together', async () => {
const { result } = renderHook(
() => useAlertDataViews([AlertConsumers.SIEM, AlertConsumers.LOGS]),
{
wrapper,
}
);

expect(fetchAlertIndexNames).toHaveBeenCalledTimes(0);
expect(fetchAlertFields).toHaveBeenCalledTimes(0);
await waitFor(() =>
expect(result.current).toEqual({
loading: false,
dataview: undefined,
});
});
})
);
expect(fetchAlertIndexNames).toHaveBeenCalledTimes(0);
expect(fetchAlertFields).toHaveBeenCalledTimes(0);
});

it('if fetch throw error return no data', async () => {
it('if fetch throws error return no data', async () => {
fetchAlertIndexNames.mockRejectedValue('error');

await act(async () => {
const { result, waitForNextUpdate } = renderHook(
() => useAlertDataViews(observabilityAlertFeatureIds),
{
wrapper,
}
);

await waitForNextUpdate();
await waitForNextUpdate();
const { result } = renderHook(() => useAlertDataViews(observabilityAlertFeatureIds), {
wrapper,
});

await waitFor(() =>
expect(result.current).toEqual({
loading: false,
dataview: undefined,
});
});
})
);
});
});

0 comments on commit a088a98

Please sign in to comment.