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

chore: fix failed tests for threat-hunting team #1

Open
wants to merge 18 commits into
base: chore/switch-waitForNextUpdate-for-waitFor
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,17 @@ module.exports = {
message:
'lodash.template is unsafe, and not compatible with our content security policy.',
},
{
object: 'renderHook',
property: 'waitFor',
message:
"use waitFor exported from @testing-library/react instead. waitFor is not available on the returned object from renderHook in it's next version",
},
{
property: 'waitForNextUpdate',
message:
"use waitFor exported from @testing-library/react instead. waitForNextUpdate is not available on the returned object from renderHook in it's next version.",
},
],
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import type { ControlGroupRuntimeState } from '@kbn/controls-plugin/public';
import { renderHook } from '@testing-library/react-hooks';
import { waitFor } from '@testing-library/react';
import { useControlGroupSyncToLocalStorage } from './use_control_group_sync_to_local_storage';
import { Storage } from '@kbn/kibana-utils-plugin/public';

Expand Down Expand Up @@ -39,30 +40,34 @@ describe('Filters Sync to Local Storage', () => {
});
it('should not be undefined if localStorage has initial value', () => {
global.localStorage.setItem(TEST_STORAGE_KEY, JSON.stringify(DEFAULT_STORED_VALUE));
const { result, waitForNextUpdate } = renderHook(() =>
const { result } = renderHook(() =>
useControlGroupSyncToLocalStorage({
Storage,
storageKey: TEST_STORAGE_KEY,
shouldSync: true,
})
);
waitForNextUpdate();
expect(result.current.controlGroupState).toMatchObject(DEFAULT_STORED_VALUE);
waitFor(() => expect(result.current.controlGroupState).toMatchObject(DEFAULT_STORED_VALUE));
});
it('should be undefined if localstorage as NO initial value', () => {
const { result, waitForNextUpdate } = renderHook(() =>
const { result } = renderHook(() =>
useControlGroupSyncToLocalStorage({
Storage,
storageKey: TEST_STORAGE_KEY,
shouldSync: true,
})
);
waitForNextUpdate();
expect(result.current.controlGroupState).toBeUndefined();
expect(result.current.setControlGroupState).toBeTruthy();
waitFor(() =>
expect(result.current).toBe(
expect.objectContaining({
controlGroupState: undefined,
setControlGroupState: expect.any(Function),
})
)
);
});
it('should be update values to local storage when sync is ON', () => {
const { result, waitFor } = renderHook(() =>
const { result } = renderHook(() =>
useControlGroupSyncToLocalStorage({
Storage,
storageKey: TEST_STORAGE_KEY,
Expand All @@ -82,7 +87,7 @@ describe('Filters Sync to Local Storage', () => {
});
});
it('should not update values to local storage when sync is OFF', () => {
const { waitFor, result, rerender } = renderHook(() =>
const { result, rerender } = renderHook(() =>
useControlGroupSyncToLocalStorage({
Storage,
storageKey: TEST_STORAGE_KEY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import React, { FC } from 'react';
import { AlertConsumers } from '@kbn/rule-data-utils';
import * as ReactQuery from '@tanstack/react-query';
import { renderHook } from '@testing-library/react-hooks';
import { waitFor } from '@testing-library/react';
import { testQueryClientConfig } from '../test_utils/test_query_client_config';
import { useFetchAlertsFieldsQuery } from './use_fetch_alerts_fields_query';
import { httpServiceMock } from '@kbn/core-http-browser-mocks';
Expand Down Expand Up @@ -88,35 +89,37 @@ describe('useFetchAlertsFieldsQuery', () => {
});

it('should call the api only once', async () => {
const { result, rerender, waitForValueToChange } = renderHook(
const { result, rerender } = renderHook(
() => useFetchAlertsFieldsQuery({ http: mockHttpClient, featureIds: ['apm'] }),
{
wrapper,
}
);

await waitForValueToChange(() => result.current.data);

expect(mockHttpGet).toHaveBeenCalledTimes(1);
expect(result.current.data).toEqual({
browserFields: { fakeCategory: {} },
fields: [
{
name: 'fakeCategory',
},
],
await waitFor(() => {
expect(mockHttpGet).toHaveBeenCalledTimes(1);
expect(result.current.data).toEqual({
browserFields: { fakeCategory: {} },
fields: [
{
name: 'fakeCategory',
},
],
});
});

rerender();

expect(mockHttpGet).toHaveBeenCalledTimes(1);
expect(result.current.data).toEqual({
browserFields: { fakeCategory: {} },
fields: [
{
name: 'fakeCategory',
},
],
await waitFor(() => {
expect(mockHttpGet).toHaveBeenCalledTimes(1);
expect(result.current.data).toEqual({
browserFields: { fakeCategory: {} },
fields: [
{
name: 'fakeCategory',
},
],
});
});
});

Expand All @@ -132,8 +135,10 @@ describe('useFetchAlertsFieldsQuery', () => {
}
);

expect(mockHttpGet).toHaveBeenCalledTimes(0);
expect(result.current.data).toEqual(emptyData);
await waitFor(() => {
expect(mockHttpGet).toHaveBeenCalledTimes(0);
expect(result.current.data).toEqual(emptyData);
});
});

it('should not fetch if all featureId are not valid', async () => {
Expand All @@ -148,8 +153,10 @@ describe('useFetchAlertsFieldsQuery', () => {
}
);

expect(mockHttpGet).toHaveBeenCalledTimes(0);
expect(result.current.data).toEqual(emptyData);
await waitFor(() => {
expect(mockHttpGet).toHaveBeenCalledTimes(0);
expect(result.current.data).toEqual(emptyData);
});
});

it('should filter out the non valid feature id', async () => {
Expand All @@ -164,9 +171,11 @@ describe('useFetchAlertsFieldsQuery', () => {
}
);

expect(mockHttpGet).toHaveBeenCalledTimes(1);
expect(mockHttpGet).toHaveBeenCalledWith('/internal/rac/alerts/browser_fields', {
query: { featureIds: ['apm', 'logs'] },
await waitFor(() => {
expect(mockHttpGet).toHaveBeenCalledTimes(1);
expect(mockHttpGet).toHaveBeenCalledWith('/internal/rac/alerts/browser_fields', {
query: { featureIds: ['apm', 'logs'] },
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import React, { FunctionComponent } from 'react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { renderHook } from '@testing-library/react-hooks';
import { waitFor } from '@testing-library/react';
import { testQueryClientConfig } from '../test_utils/test_query_client_config';
import { useFetchAlertsIndexNamesQuery } from './use_fetch_alerts_index_names_query';
import { fetchAlertsIndexNames } from '../apis/fetch_alerts_index_names';
Expand Down Expand Up @@ -56,14 +57,14 @@ describe('useFetchAlertsIndexNamesQuery', () => {
});

it('correctly caches the index names', async () => {
const { result, rerender, waitForValueToChange } = renderHook(
const { result, rerender } = renderHook(
() => useFetchAlertsIndexNamesQuery({ http: mockHttpClient, featureIds: ['apm'] }),
{
wrapper,
}
);

await waitForValueToChange(() => result.current.data);
await waitFor(() => result.current.data);

expect(mockFetchAlertsIndexNames).toHaveBeenCalledTimes(1);

Expand Down
Loading