Skip to content

Commit

Permalink
[React18] Migrated test suites to accommodate changes to testing libr…
Browse files Browse the repository at this point in the history
…ary owned by kibana-cloud-security-posture
  • Loading branch information
eokoneyo committed Nov 21, 2024
1 parent 51a84eb commit 17260c7
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { renderHook, act } from '@testing-library/react-hooks/dom';
import { renderHook, act } from '@testing-library/react';
import { useNavigateVulnerabilities, useNavigateFindings } from './use_navigate_findings';
import { useHistory } from 'react-router-dom';

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

import { useBenchmarkDynamicValues } from './use_benchmark_dynamic_values';
import { renderHook } from '@testing-library/react-hooks/dom';
import { renderHook } from '@testing-library/react';
import type { BenchmarksCisId } from '@kbn/cloud-security-posture-common';
import { useCspIntegrationLink } from '../navigation/use_csp_integration_link';

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

import { renderHook, act } from '@testing-library/react-hooks/dom';
import { renderHook, act } from '@testing-library/react';
import { useUrlQuery } from './use_url_query';
import { useLocation, useHistory } from 'react-router-dom';
import { encodeQuery } from '@kbn/cloud-security-posture';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

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

import { SetupTechnology } from '@kbn/fleet-plugin/public';
import { AgentPolicy, NewPackagePolicyInput } from '@kbn/fleet-plugin/common';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/
import React from 'react';
import { act, renderHook } from '@testing-library/react-hooks';
import { act, waitFor, renderHook } from '@testing-library/react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { useKibana } from '@kbn/kibana-react-plugin/public';
import type { RuleStateAttributes } from '@kbn/cloud-security-posture-common/schema/rules/v4';
Expand Down Expand Up @@ -87,7 +87,7 @@ describe('use_change_csp_rule_state', () => {
const appMockRender = testWrapper();
const httpPostSpy = jest.spyOn(useKibana().services.http!, 'post');

const { result, waitForNextUpdate } = await renderHook(() => useChangeCspRuleState(), {
const { result } = await renderHook(() => useChangeCspRuleState(), {
wrapper: appMockRender.wrapper,
});

Expand All @@ -107,22 +107,22 @@ describe('use_change_csp_rule_state', () => {
result.current.mutate(mockRuleStateUpdateRequest);
});

await waitForNextUpdate();

expect(httpPostSpy).toHaveBeenCalledWith(CSP_BENCHMARK_RULES_BULK_ACTION_ROUTE_PATH, {
version: '1',
body: JSON.stringify({
action: 'mute',
rules: [
{
benchmark_id: 'benchmark_id',
benchmark_version: 'benchmark_version',
rule_number: '1',
rule_id: 'rule_1',
},
],
}),
});
await waitFor(() =>
expect(httpPostSpy).toHaveBeenCalledWith(CSP_BENCHMARK_RULES_BULK_ACTION_ROUTE_PATH, {
version: '1',
body: JSON.stringify({
action: 'mute',
rules: [
{
benchmark_id: 'benchmark_id',
benchmark_version: 'benchmark_version',
rule_number: '1',
rule_id: 'rule_1',
},
],
}),
})
);
});

it('should cancel queries and update query data onMutate', async () => {
Expand All @@ -131,7 +131,7 @@ describe('use_change_csp_rule_state', () => {
const queryClientGetSpy = jest.spyOn(appMockRender.queryClient, 'getQueryData');
const mockSetQueryDataSpy = jest.spyOn(appMockRender.queryClient, 'setQueryData');

const { result, waitForNextUpdate } = await renderHook(() => useChangeCspRuleState(), {
const { result } = await renderHook(() => useChangeCspRuleState(), {
wrapper: appMockRender.wrapper,
});

Expand All @@ -151,24 +151,24 @@ describe('use_change_csp_rule_state', () => {
result.current.mutate(mockRuleStateUpdateRequest);
});

await waitForNextUpdate();

const expectedMutatedRules = {
...initialRules,
rule_1: { ...initialRules.rule_1, muted: true },
};
await waitFor(() => {
const expectedMutatedRules = {
...initialRules,
rule_1: { ...initialRules.rule_1, muted: true },
};

expect(queryClientSpy).toHaveBeenCalled();
expect(queryClientGetSpy).toHaveBeenCalled();
expect(mockSetQueryDataSpy).toHaveBeenCalled();
expect(mockSetQueryDataSpy).toHaveReturnedWith(expectedMutatedRules);
expect(queryClientSpy).toHaveBeenCalled();
expect(queryClientGetSpy).toHaveBeenCalled();
expect(mockSetQueryDataSpy).toHaveBeenCalled();
expect(mockSetQueryDataSpy).toHaveReturnedWith(expectedMutatedRules);
});
});

it('should invalidate queries onSettled', async () => {
const appMockRender = testWrapper();
const mockInvalidateQueriesSpy = jest.spyOn(appMockRender.queryClient, 'invalidateQueries');

const { result, waitForNextUpdate } = await renderHook(() => useChangeCspRuleState(), {
const { result } = await renderHook(() => useChangeCspRuleState(), {
wrapper: appMockRender.wrapper,
});

Expand All @@ -188,19 +188,19 @@ describe('use_change_csp_rule_state', () => {
result.current.mutate(mockRuleStateUpdateRequest);
});

await waitForNextUpdate();

expect(mockInvalidateQueriesSpy).toHaveBeenCalledWith(BENCHMARK_INTEGRATION_QUERY_KEY_V2);
expect(mockInvalidateQueriesSpy).toHaveBeenCalledWith(CSPM_STATS_QUERY_KEY);
expect(mockInvalidateQueriesSpy).toHaveBeenCalledWith(KSPM_STATS_QUERY_KEY);
expect(mockInvalidateQueriesSpy).toHaveBeenCalledWith(CSP_RULES_STATES_QUERY_KEY);
await waitFor(() => {
expect(mockInvalidateQueriesSpy).toHaveBeenCalledWith(BENCHMARK_INTEGRATION_QUERY_KEY_V2);
expect(mockInvalidateQueriesSpy).toHaveBeenCalledWith(CSPM_STATS_QUERY_KEY);
expect(mockInvalidateQueriesSpy).toHaveBeenCalledWith(KSPM_STATS_QUERY_KEY);
expect(mockInvalidateQueriesSpy).toHaveBeenCalledWith(CSP_RULES_STATES_QUERY_KEY);
});
});

it('should restore previous query data onError', async () => {
const appMockRender = testWrapper();
const mockSetQueryDataSpy = jest.spyOn(appMockRender.queryClient, 'setQueryData');

const { result, waitForNextUpdate } = await renderHook(() => useChangeCspRuleState(), {
const { result } = await renderHook(() => useChangeCspRuleState(), {
wrapper: appMockRender.wrapper,
});

Expand All @@ -221,13 +221,13 @@ describe('use_change_csp_rule_state', () => {
result.current.mutate(mockRuleStateUpdateRequest);
});

await waitForNextUpdate();

expect(mockSetQueryDataSpy).toHaveBeenCalled();
expect(mockSetQueryDataSpy).toHaveReturnedWith(initialRules);
await waitFor(() => {
expect(mockSetQueryDataSpy).toHaveBeenCalled();
expect(mockSetQueryDataSpy).toHaveReturnedWith(initialRules);
});
});

it('creates the new set of cache rules in a muted state when calling createRulesWithUpdatedState', async () => {
it('creates the new set of cache rules in a muted state when calling createRulesWithUpdatedState', () => {
const request: RuleStateUpdateRequest = {
newState: 'mute',
ruleIds: [
Expand Down Expand Up @@ -267,7 +267,7 @@ describe('use_change_csp_rule_state', () => {
expect(newRulesState).toEqual({ ...initialRules, ...updateRules });
});

it('creates the new cache with rules in a unmute state', async () => {
it('creates the new cache with rules in a unmute state', () => {
const initialMutedRules: Record<string, RuleStateAttributes> = {
rule_1: {
benchmark_id: 'benchmark_id',
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, act } from '@testing-library/react-hooks';

import { renderHook, act } from '@testing-library/react';
import { sessionViewIOEventsMock } from '../../../common/mocks/responses/session_view_io_events.mock';
import { useIOLines, useXtermPlayer, XtermPlayerDeps } from './hooks';
import type { ProcessEventsPage } from '../../../common';
Expand All @@ -31,7 +32,7 @@ describe('TTYPlayer/hooks', () => {

// test memoization
let last = result.current.lines;
rerender();
rerender({ pages: initial });
expect(result.current.lines === last).toBeTruthy();
last = result.current.lines;
rerender({ pages: [...initial] });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/
import React from 'react';
import { renderHook } from '@testing-library/react-hooks';
import { renderHook } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { AppContextTestRender, createAppRootMockRenderer } from '../../test';
import { sessionViewIOEventsMock } from '../../../common/mocks/responses/session_view_io_events.mock';
Expand Down

0 comments on commit 17260c7

Please sign in to comment.