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 obs-ux-management-team #201164

Merged
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 @@ -6,13 +6,9 @@
*/
import React from 'react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { act, renderHook } from '@testing-library/react-hooks';
import { waitFor, renderHook } from '@testing-library/react';
import { httpServiceMock } from '@kbn/core-http-browser-mocks';
import {
type UseAlertsHistory,
useAlertsHistory,
type Props as useAlertsHistoryProps,
} from './use_alerts_history';
import { useAlertsHistory } from './use_alerts_history';

const queryClient = new QueryClient({
logger: {
Expand Down Expand Up @@ -44,7 +40,7 @@ describe('useAlertsHistory', () => {

it('returns no data with error when http client is not provided', async () => {
const http = undefined;
const { result, waitFor } = renderHook<useAlertsHistoryProps, UseAlertsHistory>(
const { result } = renderHook(
() =>
useAlertsHistory({
http,
Expand All @@ -56,18 +52,14 @@ describe('useAlertsHistory', () => {
wrapper,
}
);
await act(async () => {
await waitFor(() => result.current.isError);
});
expect(result.current.isError).toBeTruthy();
await waitFor(() => expect(result.current.isError).toBeTruthy());
expect(result.current.isSuccess).toBeFalsy();
expect(result.current.isLoading).toBeFalsy();
});

it('returns no data when API error', async () => {
mockServices.http.post.mockRejectedValueOnce(new Error('ES error'));

const { result, waitFor } = renderHook<useAlertsHistoryProps, UseAlertsHistory>(
const { result } = renderHook(
() =>
useAlertsHistory({
...mockServices,
Expand All @@ -79,10 +71,7 @@ describe('useAlertsHistory', () => {
wrapper,
}
);
await act(async () => {
await waitFor(() => result.current.isError);
});
expect(result.current.isError).toBeTruthy();
await waitFor(() => expect(result.current.isError).toBeTruthy());
expect(result.current.isSuccess).toBeFalsy();
expect(result.current.isLoading).toBeFalsy();
});
Expand Down Expand Up @@ -130,7 +119,7 @@ describe('useAlertsHistory', () => {
},
});

const { result, waitFor } = renderHook<useAlertsHistoryProps, UseAlertsHistory>(
const { result } = renderHook(
() =>
useAlertsHistory({
...mockServices,
Expand All @@ -142,9 +131,7 @@ describe('useAlertsHistory', () => {
wrapper,
}
);
await act(async () => {
await waitFor(() => result.current.isSuccess);
});
await waitFor(() => expect(result.current.isSuccess).toBe(true));
expect(result.current.isLoading).toBeFalsy();
expect(result.current.isError).toBeFalsy();
expect(result.current.data.avgTimeToRecoverUS).toEqual(134959464.2857143);
Expand All @@ -167,7 +154,7 @@ describe('useAlertsHistory', () => {
},
});

const { result, waitFor } = renderHook<useAlertsHistoryProps, UseAlertsHistory>(
const { result } = renderHook(
() =>
useAlertsHistory({
...mockServices,
Expand All @@ -181,11 +168,7 @@ describe('useAlertsHistory', () => {
wrapper,
}
);

await act(async () => {
await waitFor(() => result.current.isSuccess);
});

await waitFor(() => expect(result.current.isSuccess).toBe(true));
expect(mockServices.http.post).toBeCalledWith('/internal/rac/alerts/find', {
body:
'{"size":0,"rule_type_ids":["apm"],"consumers":["foo"],"query":{"bool":{"must":[' +
Expand Down Expand Up @@ -215,7 +198,7 @@ describe('useAlertsHistory', () => {
},
});

const { result, waitFor } = renderHook<useAlertsHistoryProps, UseAlertsHistory>(
const { result } = renderHook(
() =>
useAlertsHistory({
...mockServices,
Expand All @@ -229,10 +212,7 @@ describe('useAlertsHistory', () => {
}
);

await act(async () => {
await waitFor(() => result.current.isSuccess);
});

await waitFor(() => expect(result.current.isSuccess).toBe(true));
expect(mockServices.http.post).toBeCalledWith('/internal/rac/alerts/find', {
body:
'{"size":0,"rule_type_ids":["apm"],"query":{"bool":{"must":[' +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import React from 'react';

import { allSeriesKey, reportTypeKey, UrlStorageContextProvider } from './use_series_storage';
import { renderHook } from '@testing-library/react-hooks';
import { renderHook } from '@testing-library/react';
import { useLensAttributes } from './use_lens_attributes';
import { ReportTypes } from '../configurations/constants';
import { mockDataView } from '../rtl_helpers';
Expand Down Expand Up @@ -66,9 +66,15 @@ describe('useExpViewTimeRange', function () {
jest.spyOn(lensHook, 'useLensFormulaHelper').mockReturnValue(formulaHelper);
});

const lensAttributesSpy = jest.spyOn(lensAttributes, 'LensAttributes');
const lensAttributesSpy = jest
.spyOn(lensAttributes, 'LensAttributes')
.mockImplementation(function (...args) {
return {
getJSON: () => {},
} as lensAttributes.LensAttributes;
});

function Wrapper({ children }: { children: JSX.Element }) {
function Wrapper({ children }: React.PropsWithChildren) {
return (
<ExploratoryViewContextProvider
reportTypes={reportTypesList}
Expand All @@ -78,7 +84,9 @@ describe('useExpViewTimeRange', function () {
theme$={themeServiceMock.createTheme$()}
{...coreMock.createStart()}
>
<UrlStorageContextProvider storage={storage}>{children}</UrlStorageContextProvider>
<UrlStorageContextProvider storage={storage}>
{React.createElement(React.Fragment, {}, children)}
</UrlStorageContextProvider>
</ExploratoryViewContextProvider>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
*/

import React, { useEffect } from 'react';
import { act, renderHook } from '@testing-library/react-hooks';
import { Router, Route } from '@kbn/shared-ux-router';
import { render } from '@testing-library/react';
import { render, renderHook, act } from '@testing-library/react';
import { UrlStorageContextProvider, useSeriesStorage, reportTypeKey } from './use_series_storage';
import { getHistoryFromUrl } from '../rtl_helpers';
import type { AppDataType } from '../types';
Expand Down Expand Up @@ -135,7 +134,7 @@ describe('userSeriesStorage', function () {
});

it('ensures that only one series has a breakdown', () => {
function wrapper({ children }: { children: React.ReactElement }) {
function wrapper({ children }: React.PropsWithChildren) {
return (
<UrlStorageContextProvider
storage={{
Expand All @@ -145,7 +144,7 @@ describe('userSeriesStorage', function () {
set: jest.fn(),
}}
>
{children}
{React.createElement(React.Fragment, {}, children)}
</UrlStorageContextProvider>
);
}
Expand All @@ -166,7 +165,7 @@ describe('userSeriesStorage', function () {

it('sets reportType when calling applyChanges', () => {
const setStorage = jest.fn();
function wrapper({ children }: { children: React.ReactElement }) {
function wrapper({ children }: React.PropsWithChildren) {
return (
<UrlStorageContextProvider
storage={{
Expand All @@ -178,7 +177,7 @@ describe('userSeriesStorage', function () {
set: setStorage,
}}
>
{children}
{React.createElement(React.Fragment, {}, children)}
</UrlStorageContextProvider>
);
}
Expand All @@ -197,7 +196,7 @@ describe('userSeriesStorage', function () {

it('returns reportType in state, not url storage, from hook', () => {
const setStorage = jest.fn();
function wrapper({ children }: { children: React.ReactElement }) {
function wrapper({ children }: React.PropsWithChildren) {
return (
<UrlStorageContextProvider
storage={{
Expand All @@ -209,7 +208,7 @@ describe('userSeriesStorage', function () {
set: setStorage,
}}
>
{children}
{React.createElement(React.Fragment, {}, children)}
</UrlStorageContextProvider>
);
}
Expand All @@ -225,7 +224,7 @@ describe('userSeriesStorage', function () {
it('ensures that telemetry is called', () => {
const trackEvent = jest.fn();
jest.spyOn(useTrackMetric, 'useUiTracker').mockReturnValue(trackEvent);
function wrapper({ children }: { children: React.ReactElement }) {
function wrapper({ children }: React.PropsWithChildren) {
return (
<UrlStorageContextProvider
storage={{
Expand All @@ -237,7 +236,7 @@ describe('userSeriesStorage', function () {
set: jest.fn(),
}}
>
{children}
{React.createElement(React.Fragment, {}, children)}
</UrlStorageContextProvider>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import React from 'react';

import { allSeriesKey, reportTypeKey, UrlStorageContextProvider } from './use_series_storage';
import { renderHook } from '@testing-library/react-hooks';
import { renderHook } from '@testing-library/react';
import { useExpViewTimeRange } from './use_time_range';
import { ReportTypes } from '../configurations/constants';
import { createKbnUrlStateStorage } from '@kbn/kibana-utils-plugin/public';
Expand Down Expand Up @@ -43,9 +43,14 @@ const mockMultipleSeries = [
describe('useExpViewTimeRange', function () {
const storage = createKbnUrlStateStorage({ useHash: false });

function Wrapper({ children }: { children: JSX.Element }) {
return <UrlStorageContextProvider storage={storage}>{children}</UrlStorageContextProvider>;
function Wrapper({ children }: React.PropsWithChildren) {
return (
<UrlStorageContextProvider storage={storage}>
{React.createElement(React.Fragment, {}, children)}
</UrlStorageContextProvider>
);
}

it('should return expected result when there is one series', async function () {
await storage.set(allSeriesKey, mockSingleSeries);
await storage.set(reportTypeKey, ReportTypes.KPI);
Expand Down
Loading
Loading