mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 17:59:23 -04:00
# Backport This will backport the following commits from `main` to `8.x`: - [[React18] Migrate test suites to account for testing library upgrades response-ops (#201141)](https://github.com/elastic/kibana/pull/201141) <!--- Backport version: 9.4.3 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) <!--BACKPORT [{"author":{"name":"Eyo O. Eyo","email":"7893459+eokoneyo@users.noreply.github.com"},"sourceCommit":{"committedDate":"2024-11-22T16:43:23Z","message":"[React18] Migrate test suites to account for testing library upgrades response-ops (#201141)\n\nThis PR migrates test suites that use `renderHook` from the library\r\n`@testing-library/react-hooks` to adopt the equivalent and replacement\r\nof `renderHook` from the export that is now available from\r\n`@testing-library/react`. This work is required for the planned\r\nmigration to react18.\r\n\r\n## Context\r\n\r\nIn this PR, usages of `waitForNextUpdate` that previously could have\r\nbeen destructured from `renderHook` are now been replaced with `waitFor`\r\nexported from `@testing-library/react`, furthermore `waitFor`\r\nthat would also have been destructured from the same renderHook result\r\nis now been replaced with `waitFor` from the export of\r\n`@testing-library/react`.\r\n\r\n***Why is `waitFor` a sufficient enough replacement for\r\n`waitForNextUpdate`, and better for testing values subject to async\r\ncomputations?***\r\n\r\nWaitFor will retry the provided callback if an error is returned, till\r\nthe configured timeout elapses. By default the retry interval is `50ms`\r\nwith a timeout value of `1000ms` that\r\neffectively translates to at least 20 retries for assertions placed\r\nwithin waitFor. See\r\nhttps://testing-library.com/docs/dom-testing-library/api-async/#waitfor\r\nfor more information.\r\nThis however means that for person's writing tests, said person has to\r\nbe explicit about expectations that describe the internal state of the\r\nhook being tested.\r\nThis implies checking for instance when a react query hook is being\r\nrendered, there's an assertion that said hook isn't loading anymore.\r\n\r\nIn this PR you'd notice that this pattern has been adopted, with most\r\nexisting assertions following an invocation of `waitForNextUpdate` being\r\nplaced within a `waitFor`\r\ninvocation. In some cases the replacement is simply a `waitFor(() => new\r\nPromise((resolve) => resolve(null)))` (many thanks to @kapral18, for\r\npoint out exactly why this works),\r\nwhere this suffices the assertions that follow aren't placed within a\r\nwaitFor so this PR doesn't get larger than it needs to be.\r\n\r\nIt's also worth pointing out this PR might also contain changes to test\r\nand application code to improve said existing test.\r\n\r\n### What to do next?\r\n1. Review the changes in this PR.\r\n2. If you think the changes are correct, approve the PR.\r\n\r\n## Any questions?\r\nIf you have any questions or need help with this PR, please leave\r\ncomments in this PR.","sha":"3890bde1ab9510adc7b6b8333861d3de51381ef5","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Team:ResponseOps","v9.0.0","backport:prev-minor","React@18"],"title":"[React18] Migrate test suites to account for testing library upgrades response-ops","number":201141,"url":"https://github.com/elastic/kibana/pull/201141","mergeCommit":{"message":"[React18] Migrate test suites to account for testing library upgrades response-ops (#201141)\n\nThis PR migrates test suites that use `renderHook` from the library\r\n`@testing-library/react-hooks` to adopt the equivalent and replacement\r\nof `renderHook` from the export that is now available from\r\n`@testing-library/react`. This work is required for the planned\r\nmigration to react18.\r\n\r\n## Context\r\n\r\nIn this PR, usages of `waitForNextUpdate` that previously could have\r\nbeen destructured from `renderHook` are now been replaced with `waitFor`\r\nexported from `@testing-library/react`, furthermore `waitFor`\r\nthat would also have been destructured from the same renderHook result\r\nis now been replaced with `waitFor` from the export of\r\n`@testing-library/react`.\r\n\r\n***Why is `waitFor` a sufficient enough replacement for\r\n`waitForNextUpdate`, and better for testing values subject to async\r\ncomputations?***\r\n\r\nWaitFor will retry the provided callback if an error is returned, till\r\nthe configured timeout elapses. By default the retry interval is `50ms`\r\nwith a timeout value of `1000ms` that\r\neffectively translates to at least 20 retries for assertions placed\r\nwithin waitFor. See\r\nhttps://testing-library.com/docs/dom-testing-library/api-async/#waitfor\r\nfor more information.\r\nThis however means that for person's writing tests, said person has to\r\nbe explicit about expectations that describe the internal state of the\r\nhook being tested.\r\nThis implies checking for instance when a react query hook is being\r\nrendered, there's an assertion that said hook isn't loading anymore.\r\n\r\nIn this PR you'd notice that this pattern has been adopted, with most\r\nexisting assertions following an invocation of `waitForNextUpdate` being\r\nplaced within a `waitFor`\r\ninvocation. In some cases the replacement is simply a `waitFor(() => new\r\nPromise((resolve) => resolve(null)))` (many thanks to @kapral18, for\r\npoint out exactly why this works),\r\nwhere this suffices the assertions that follow aren't placed within a\r\nwaitFor so this PR doesn't get larger than it needs to be.\r\n\r\nIt's also worth pointing out this PR might also contain changes to test\r\nand application code to improve said existing test.\r\n\r\n### What to do next?\r\n1. Review the changes in this PR.\r\n2. If you think the changes are correct, approve the PR.\r\n\r\n## Any questions?\r\nIf you have any questions or need help with this PR, please leave\r\ncomments in this PR.","sha":"3890bde1ab9510adc7b6b8333861d3de51381ef5"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/201141","number":201141,"mergeCommit":{"message":"[React18] Migrate test suites to account for testing library upgrades response-ops (#201141)\n\nThis PR migrates test suites that use `renderHook` from the library\r\n`@testing-library/react-hooks` to adopt the equivalent and replacement\r\nof `renderHook` from the export that is now available from\r\n`@testing-library/react`. This work is required for the planned\r\nmigration to react18.\r\n\r\n## Context\r\n\r\nIn this PR, usages of `waitForNextUpdate` that previously could have\r\nbeen destructured from `renderHook` are now been replaced with `waitFor`\r\nexported from `@testing-library/react`, furthermore `waitFor`\r\nthat would also have been destructured from the same renderHook result\r\nis now been replaced with `waitFor` from the export of\r\n`@testing-library/react`.\r\n\r\n***Why is `waitFor` a sufficient enough replacement for\r\n`waitForNextUpdate`, and better for testing values subject to async\r\ncomputations?***\r\n\r\nWaitFor will retry the provided callback if an error is returned, till\r\nthe configured timeout elapses. By default the retry interval is `50ms`\r\nwith a timeout value of `1000ms` that\r\neffectively translates to at least 20 retries for assertions placed\r\nwithin waitFor. See\r\nhttps://testing-library.com/docs/dom-testing-library/api-async/#waitfor\r\nfor more information.\r\nThis however means that for person's writing tests, said person has to\r\nbe explicit about expectations that describe the internal state of the\r\nhook being tested.\r\nThis implies checking for instance when a react query hook is being\r\nrendered, there's an assertion that said hook isn't loading anymore.\r\n\r\nIn this PR you'd notice that this pattern has been adopted, with most\r\nexisting assertions following an invocation of `waitForNextUpdate` being\r\nplaced within a `waitFor`\r\ninvocation. In some cases the replacement is simply a `waitFor(() => new\r\nPromise((resolve) => resolve(null)))` (many thanks to @kapral18, for\r\npoint out exactly why this works),\r\nwhere this suffices the assertions that follow aren't placed within a\r\nwaitFor so this PR doesn't get larger than it needs to be.\r\n\r\nIt's also worth pointing out this PR might also contain changes to test\r\nand application code to improve said existing test.\r\n\r\n### What to do next?\r\n1. Review the changes in this PR.\r\n2. If you think the changes are correct, approve the PR.\r\n\r\n## Any questions?\r\nIf you have any questions or need help with this PR, please leave\r\ncomments in this PR.","sha":"3890bde1ab9510adc7b6b8333861d3de51381ef5"}}]}] BACKPORT--> Co-authored-by: Eyo O. Eyo <7893459+eokoneyo@users.noreply.github.com>
175 lines
4.8 KiB
TypeScript
175 lines
4.8 KiB
TypeScript
/*
|
|
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
* or more contributor license agreements. Licensed under the "Elastic License
|
|
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
|
|
* Public License v 1"; you may not use this file except in compliance with, at
|
|
* your election, the "Elastic License 2.0", the "GNU Affero General Public
|
|
* License v3.0 only", or the "Server Side Public License, v 1".
|
|
*/
|
|
|
|
import React from 'react';
|
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
|
import { waitFor, renderHook } from '@testing-library/react';
|
|
import type { HttpStart } from '@kbn/core-http-browser';
|
|
|
|
import { useHealthCheck } from './use_health_check';
|
|
import { healthCheckErrors } from '../apis';
|
|
|
|
jest.mock('../apis/fetch_ui_health_status/fetch_ui_health_status', () => ({
|
|
fetchUiHealthStatus: jest.fn(),
|
|
}));
|
|
|
|
jest.mock('../apis/fetch_alerting_framework_health/fetch_alerting_framework_health', () => ({
|
|
fetchAlertingFrameworkHealth: jest.fn(),
|
|
}));
|
|
|
|
const { fetchUiHealthStatus } = jest.requireMock(
|
|
'../apis/fetch_ui_health_status/fetch_ui_health_status'
|
|
);
|
|
const { fetchAlertingFrameworkHealth } = jest.requireMock(
|
|
'../apis/fetch_alerting_framework_health/fetch_alerting_framework_health'
|
|
);
|
|
|
|
const queryClient = new QueryClient();
|
|
|
|
const wrapper = ({ children }: { children: React.ReactNode }) => (
|
|
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
|
);
|
|
|
|
const httpMock = jest.fn();
|
|
|
|
describe('useHealthCheck', () => {
|
|
afterEach(() => {
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
test('should return null if there are no errors', async () => {
|
|
fetchUiHealthStatus.mockResolvedValueOnce({
|
|
isRulesAvailable: true,
|
|
});
|
|
|
|
fetchAlertingFrameworkHealth.mockResolvedValueOnce({
|
|
isSufficientlySecure: true,
|
|
hasPermanentEncryptionKey: true,
|
|
});
|
|
|
|
const { result } = renderHook(
|
|
() => {
|
|
return useHealthCheck({
|
|
http: httpMock as unknown as HttpStart,
|
|
});
|
|
},
|
|
{ wrapper }
|
|
);
|
|
|
|
await waitFor(() => {
|
|
return expect(result.current.isLoading).toEqual(false);
|
|
});
|
|
|
|
expect(result.current.error).toBeNull();
|
|
});
|
|
|
|
test('should return alerts error if rules are not available', async () => {
|
|
fetchUiHealthStatus.mockResolvedValueOnce({
|
|
isRulesAvailable: false,
|
|
});
|
|
|
|
fetchAlertingFrameworkHealth.mockResolvedValueOnce({
|
|
isSufficientlySecure: true,
|
|
hasPermanentEncryptionKey: true,
|
|
});
|
|
|
|
const { result } = renderHook(
|
|
() => {
|
|
return useHealthCheck({
|
|
http: httpMock as unknown as HttpStart,
|
|
});
|
|
},
|
|
{ wrapper }
|
|
);
|
|
|
|
await waitFor(() => {
|
|
return expect(result.current.isLoading).toEqual(false);
|
|
});
|
|
|
|
expect(result.current.error).toEqual(healthCheckErrors.ALERTS_ERROR);
|
|
});
|
|
|
|
test('should return API keys encryption error if not secure or has no encryption key', async () => {
|
|
fetchUiHealthStatus.mockResolvedValueOnce({
|
|
isRulesAvailable: true,
|
|
});
|
|
|
|
fetchAlertingFrameworkHealth.mockResolvedValueOnce({
|
|
isSufficientlySecure: false,
|
|
hasPermanentEncryptionKey: false,
|
|
});
|
|
|
|
const { result } = renderHook(
|
|
() => {
|
|
return useHealthCheck({
|
|
http: httpMock as unknown as HttpStart,
|
|
});
|
|
},
|
|
{ wrapper }
|
|
);
|
|
|
|
await waitFor(() => {
|
|
return expect(result.current.isLoading).toEqual(false);
|
|
});
|
|
|
|
expect(result.current.error).toEqual(healthCheckErrors.API_KEYS_AND_ENCRYPTION_ERROR);
|
|
});
|
|
|
|
test('should return encryption error if has no encryption key', async () => {
|
|
fetchUiHealthStatus.mockResolvedValueOnce({
|
|
isRulesAvailable: true,
|
|
});
|
|
|
|
fetchAlertingFrameworkHealth.mockResolvedValueOnce({
|
|
isSufficientlySecure: true,
|
|
hasPermanentEncryptionKey: false,
|
|
});
|
|
|
|
const { result } = renderHook(
|
|
() => {
|
|
return useHealthCheck({
|
|
http: httpMock as unknown as HttpStart,
|
|
});
|
|
},
|
|
{ wrapper }
|
|
);
|
|
|
|
await waitFor(() => {
|
|
return expect(result.current.isLoading).toEqual(false);
|
|
});
|
|
|
|
expect(result.current.error).toEqual(healthCheckErrors.ENCRYPTION_ERROR);
|
|
});
|
|
|
|
test('should return API keys disabled error is API keys are disabled', async () => {
|
|
fetchUiHealthStatus.mockResolvedValueOnce({
|
|
isRulesAvailable: true,
|
|
});
|
|
|
|
fetchAlertingFrameworkHealth.mockResolvedValueOnce({
|
|
isSufficientlySecure: false,
|
|
hasPermanentEncryptionKey: true,
|
|
});
|
|
|
|
const { result } = renderHook(
|
|
() => {
|
|
return useHealthCheck({
|
|
http: httpMock as unknown as HttpStart,
|
|
});
|
|
},
|
|
{ wrapper }
|
|
);
|
|
|
|
await waitFor(() => {
|
|
return expect(result.current.isLoading).toEqual(false);
|
|
});
|
|
|
|
expect(result.current.error).toEqual(healthCheckErrors.API_KEYS_DISABLED_ERROR);
|
|
});
|
|
});
|