From 9a7a426ff811540ffb0f802693acadac19158687 Mon Sep 17 00:00:00 2001 From: Aleh Zasypkin Date: Tue, 8 Mar 2022 10:49:19 +0100 Subject: [PATCH] Get rid of `axios` dependency in the Upgrade Assistant tests. --- .../client_integration/app/app.helpers.tsx | 11 +- .../app/cluster_upgrade.test.tsx | 22 +- .../es_deprecation_logs.helpers.ts | 6 +- .../es_deprecation_logs.test.tsx | 98 +++--- .../default_deprecation_flyout.test.ts | 20 +- .../es_deprecations/deprecations_list.test.ts | 64 ++-- .../es_deprecations/error_handling.test.ts | 23 +- .../es_deprecations.helpers.ts | 6 +- .../index_settings_deprecation_flyout.test.ts | 79 ++--- .../ml_snapshots_deprecation_flyout.test.ts | 92 +++--- .../es_deprecations/mocked_responses.ts | 2 +- .../reindex_deprecation_flyout.test.ts | 179 ++++++----- .../helpers/http_requests.ts | 297 +++++++----------- .../helpers/setup_environment.tsx | 17 +- .../deprecation_details_flyout.test.ts | 8 +- .../deprecations_table.test.ts | 11 +- .../deprecations_table/error_handling.test.ts | 10 +- .../kibana_deprecations.helpers.ts | 4 +- .../overview/backup_step/backup_step.test.tsx | 35 +-- .../elasticsearch_deprecation_issues.test.tsx | 35 ++- .../fix_issues_step/fix_issues_step.test.tsx | 17 +- .../kibana_deprecation_issues.test.tsx | 13 +- .../migrate_system_indices/flyout.test.ts | 14 +- .../migrate_system_indices.test.tsx | 38 +-- .../step_completion.test.ts | 21 +- .../overview/overview.helpers.ts | 9 +- .../overview/overview.test.tsx | 8 +- .../upgrade_step/upgrade_step.test.tsx | 42 +-- 28 files changed, 562 insertions(+), 619 deletions(-) diff --git a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/app/app.helpers.tsx b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/app/app.helpers.tsx index 25e38b4111e50..ba09db95ee883 100644 --- a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/app/app.helpers.tsx +++ b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/app/app.helpers.tsx @@ -8,6 +8,7 @@ import { act } from 'react-dom/test-utils'; import { registerTestBed, TestBed, AsyncTestBedConfig } from '@kbn/test-jest-helpers'; +import { HttpSetup } from 'src/core/public'; import { App } from '../../../public/application/app'; import { WithAppDependencies } from '../helpers'; @@ -39,8 +40,14 @@ const createActions = (testBed: TestBed) => { }; }; -export const setupAppPage = async (overrides?: Record): Promise => { - const initTestBed = registerTestBed(WithAppDependencies(App, overrides), testBedConfig); +export const setupAppPage = async ( + httpSetup: HttpSetup, + overrides?: Record +): Promise => { + const initTestBed = registerTestBed( + WithAppDependencies(App, httpSetup, overrides), + testBedConfig + ); const testBed = await initTestBed(); return { diff --git a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/app/cluster_upgrade.test.tsx b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/app/cluster_upgrade.test.tsx index 7276d005844c2..d3d16d1844ffc 100644 --- a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/app/cluster_upgrade.test.tsx +++ b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/app/cluster_upgrade.test.tsx @@ -12,20 +12,14 @@ import { AppTestBed, setupAppPage } from './app.helpers'; describe('Cluster upgrade', () => { let testBed: AppTestBed; - let server: ReturnType['server']; - let httpRequestsMockHelpers: ReturnType['httpRequestsMockHelpers']; - - beforeEach(() => { - ({ server, httpRequestsMockHelpers } = setupEnvironment()); - }); - - afterEach(() => { - server.restore(); + let mockEnvironment: ReturnType; + beforeEach(async () => { + mockEnvironment = setupEnvironment(); }); describe('when user is still preparing for upgrade', () => { beforeEach(async () => { - testBed = await setupAppPage(); + testBed = await setupAppPage(mockEnvironment.httpSetup); }); test('renders overview', () => { @@ -43,7 +37,7 @@ describe('Cluster upgrade', () => { // `es deprecations` response. describe('when cluster is in the process of a rolling upgrade', () => { beforeEach(async () => { - httpRequestsMockHelpers.setLoadEsDeprecationsResponse(undefined, { + mockEnvironment.httpRequestsMockHelpers.setLoadEsDeprecationsResponse(undefined, { statusCode: 426, message: '', attributes: { @@ -52,7 +46,7 @@ describe('Cluster upgrade', () => { }); await act(async () => { - testBed = await setupAppPage(); + testBed = await setupAppPage(mockEnvironment.httpSetup); }); }); @@ -67,7 +61,7 @@ describe('Cluster upgrade', () => { describe('when cluster has been upgraded', () => { beforeEach(async () => { - httpRequestsMockHelpers.setLoadEsDeprecationsResponse(undefined, { + mockEnvironment.httpRequestsMockHelpers.setLoadEsDeprecationsResponse(undefined, { statusCode: 426, message: '', attributes: { @@ -76,7 +70,7 @@ describe('Cluster upgrade', () => { }); await act(async () => { - testBed = await setupAppPage(); + testBed = await setupAppPage(mockEnvironment.httpSetup); }); }); diff --git a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecation_logs/es_deprecation_logs.helpers.ts b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecation_logs/es_deprecation_logs.helpers.ts index 31bbcd01a7320..813a269bf5a97 100644 --- a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecation_logs/es_deprecation_logs.helpers.ts +++ b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecation_logs/es_deprecation_logs.helpers.ts @@ -7,7 +7,8 @@ import { act } from 'react-dom/test-utils'; import { registerTestBed, TestBed, AsyncTestBedConfig } from '@kbn/test-jest-helpers'; -import { EsDeprecationLogs } from '../../../public/application/components/es_deprecation_logs'; +import { HttpSetup } from 'src/core/public'; +import { EsDeprecationLogs } from '../../../public/application/components'; import { WithAppDependencies } from '../helpers'; const testBedConfig: AsyncTestBedConfig = { @@ -65,10 +66,11 @@ const createActions = (testBed: TestBed) => { }; export const setupESDeprecationLogsPage = async ( + httpSetup: HttpSetup, overrides?: Record ): Promise => { const initTestBed = registerTestBed( - WithAppDependencies(EsDeprecationLogs, overrides), + WithAppDependencies(EsDeprecationLogs, httpSetup, overrides), testBedConfig ); const testBed = await initTestBed(); diff --git a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecation_logs/es_deprecation_logs.test.tsx b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecation_logs/es_deprecation_logs.test.tsx index 679a9175f5c50..e6450336d8650 100644 --- a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecation_logs/es_deprecation_logs.test.tsx +++ b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecation_logs/es_deprecation_logs.test.tsx @@ -41,18 +41,16 @@ const getLoggingResponse = (toggle: boolean): DeprecationLoggingStatus => ({ describe('ES deprecation logs', () => { let testBed: EsDeprecationLogsTestBed; - const { server, httpRequestsMockHelpers } = setupEnvironment(); - + let mockEnvironment: ReturnType; beforeEach(async () => { - httpRequestsMockHelpers.setLoadDeprecationLoggingResponse(getLoggingResponse(true)); - testBed = await setupESDeprecationLogsPage(); + mockEnvironment = setupEnvironment(); + mockEnvironment.httpRequestsMockHelpers.setLoadDeprecationLoggingResponse( + getLoggingResponse(true) + ); + testBed = await setupESDeprecationLogsPage(mockEnvironment.httpSetup); testBed.component.update(); }); - afterAll(() => { - server.restore(); - }); - describe('Documentation link', () => { test('Has a link for migration info api docs in page header', () => { const { exists } = testBed; @@ -65,25 +63,30 @@ describe('ES deprecation logs', () => { test('toggles deprecation logging', async () => { const { find, actions } = testBed; - httpRequestsMockHelpers.setUpdateDeprecationLoggingResponse(getLoggingResponse(false)); + mockEnvironment.httpRequestsMockHelpers.setUpdateDeprecationLoggingResponse( + getLoggingResponse(false) + ); expect(find('deprecationLoggingToggle').props()['aria-checked']).toBe(true); await actions.clickDeprecationToggle(); - const latestRequest = server.requests[server.requests.length - 1]; - expect(JSON.parse(JSON.parse(latestRequest.requestBody).body)).toEqual({ isEnabled: false }); + expect(mockEnvironment.httpSetup.put).toHaveBeenLastCalledWith( + `/api/upgrade_assistant/deprecation_logging`, + expect.objectContaining({ body: JSON.stringify({ isEnabled: false }) }) + ); + expect(find('deprecationLoggingToggle').props()['aria-checked']).toBe(false); }); test('shows callout when only loggerDeprecation is enabled', async () => { - httpRequestsMockHelpers.setLoadDeprecationLoggingResponse({ + mockEnvironment.httpRequestsMockHelpers.setLoadDeprecationLoggingResponse({ isDeprecationLogIndexingEnabled: false, isDeprecationLoggingEnabled: true, }); await act(async () => { - testBed = await setupESDeprecationLogsPage(); + testBed = await setupESDeprecationLogsPage(mockEnvironment.httpSetup); }); const { exists, component } = testBed; @@ -102,7 +105,7 @@ describe('ES deprecation logs', () => { const { actions, exists } = testBed; - httpRequestsMockHelpers.setUpdateDeprecationLoggingResponse(undefined, error); + mockEnvironment.httpRequestsMockHelpers.setUpdateDeprecationLoggingResponse(undefined, error); await actions.clickDeprecationToggle(); @@ -116,10 +119,10 @@ describe('ES deprecation logs', () => { message: 'Internal server error', }; - httpRequestsMockHelpers.setLoadDeprecationLoggingResponse(undefined, error); + mockEnvironment.httpRequestsMockHelpers.setLoadDeprecationLoggingResponse(undefined, error); await act(async () => { - testBed = await setupESDeprecationLogsPage(); + testBed = await setupESDeprecationLogsPage(mockEnvironment.httpSetup); }); const { component, exists } = testBed; @@ -130,13 +133,13 @@ describe('ES deprecation logs', () => { }); test('It doesnt show external links and deprecations count when toggle is disabled', async () => { - httpRequestsMockHelpers.setLoadDeprecationLoggingResponse({ + mockEnvironment.httpRequestsMockHelpers.setLoadDeprecationLoggingResponse({ isDeprecationLogIndexingEnabled: false, isDeprecationLoggingEnabled: false, }); await act(async () => { - testBed = await setupESDeprecationLogsPage(); + testBed = await setupESDeprecationLogsPage(mockEnvironment.httpSetup); }); const { exists, component } = testBed; @@ -151,12 +154,14 @@ describe('ES deprecation logs', () => { describe('Step 2 - Analyze logs', () => { beforeEach(async () => { - httpRequestsMockHelpers.setLoadDeprecationLoggingResponse(getLoggingResponse(true)); + mockEnvironment.httpRequestsMockHelpers.setLoadDeprecationLoggingResponse( + getLoggingResponse(true) + ); }); test('Has a link to see logs in observability app', async () => { await act(async () => { - testBed = await setupESDeprecationLogsPage({ + testBed = await setupESDeprecationLogsPage(mockEnvironment.httpSetup, { http: { basePath: { prepend: (url: string) => url, @@ -194,7 +199,7 @@ describe('ES deprecation logs', () => { test('Has a link to see logs in discover app', async () => { await act(async () => { - testBed = await setupESDeprecationLogsPage(); + testBed = await setupESDeprecationLogsPage(mockEnvironment.httpSetup); }); const { exists, component, find } = testBed; @@ -223,17 +228,19 @@ describe('ES deprecation logs', () => { describe('Step 3 - Resolve log issues', () => { beforeEach(async () => { - httpRequestsMockHelpers.setLoadDeprecationLoggingResponse(getLoggingResponse(true)); - httpRequestsMockHelpers.setDeleteLogsCacheResponse('ok'); + mockEnvironment.httpRequestsMockHelpers.setLoadDeprecationLoggingResponse( + getLoggingResponse(true) + ); + mockEnvironment.httpRequestsMockHelpers.setDeleteLogsCacheResponse('ok'); }); test('With deprecation warnings', async () => { - httpRequestsMockHelpers.setLoadDeprecationLogsCountResponse({ + mockEnvironment.httpRequestsMockHelpers.setLoadDeprecationLogsCountResponse({ count: 10, }); await act(async () => { - testBed = await setupESDeprecationLogsPage(); + testBed = await setupESDeprecationLogsPage(mockEnvironment.httpSetup); }); const { find, exists, component } = testBed; @@ -245,12 +252,12 @@ describe('ES deprecation logs', () => { }); test('No deprecation issues', async () => { - httpRequestsMockHelpers.setLoadDeprecationLogsCountResponse({ + mockEnvironment.httpRequestsMockHelpers.setLoadDeprecationLogsCountResponse({ count: 0, }); await act(async () => { - testBed = await setupESDeprecationLogsPage(); + testBed = await setupESDeprecationLogsPage(mockEnvironment.httpSetup); }); const { find, exists, component } = testBed; @@ -268,10 +275,10 @@ describe('ES deprecation logs', () => { message: 'Internal server error', }; - httpRequestsMockHelpers.setLoadDeprecationLogsCountResponse(undefined, error); + mockEnvironment.httpRequestsMockHelpers.setLoadDeprecationLogsCountResponse(undefined, error); await act(async () => { - testBed = await setupESDeprecationLogsPage(); + testBed = await setupESDeprecationLogsPage(mockEnvironment.httpSetup); }); const { exists, actions, component } = testBed; @@ -280,7 +287,7 @@ describe('ES deprecation logs', () => { expect(exists('errorCallout')).toBe(true); - httpRequestsMockHelpers.setLoadDeprecationLogsCountResponse({ + mockEnvironment.httpRequestsMockHelpers.setLoadDeprecationLogsCountResponse({ count: 0, }); @@ -290,12 +297,12 @@ describe('ES deprecation logs', () => { }); test('Allows user to reset last stored date', async () => { - httpRequestsMockHelpers.setLoadDeprecationLogsCountResponse({ + mockEnvironment.httpRequestsMockHelpers.setLoadDeprecationLogsCountResponse({ count: 10, }); await act(async () => { - testBed = await setupESDeprecationLogsPage(); + testBed = await setupESDeprecationLogsPage(mockEnvironment.httpSetup); }); const { exists, actions, component } = testBed; @@ -305,7 +312,7 @@ describe('ES deprecation logs', () => { expect(exists('hasWarningsCallout')).toBe(true); expect(exists('resetLastStoredDate')).toBe(true); - httpRequestsMockHelpers.setLoadDeprecationLogsCountResponse({ + mockEnvironment.httpRequestsMockHelpers.setLoadDeprecationLogsCountResponse({ count: 0, }); @@ -321,13 +328,13 @@ describe('ES deprecation logs', () => { message: 'Internal server error', }; - httpRequestsMockHelpers.setDeleteLogsCacheResponse(undefined, error); + mockEnvironment.httpRequestsMockHelpers.setDeleteLogsCacheResponse(undefined, error); // Initially we want to have the callout to have a warning state - httpRequestsMockHelpers.setLoadDeprecationLogsCountResponse({ count: 10 }); + mockEnvironment.httpRequestsMockHelpers.setLoadDeprecationLogsCountResponse({ count: 10 }); const addDanger = jest.fn(); await act(async () => { - testBed = await setupESDeprecationLogsPage({ + testBed = await setupESDeprecationLogsPage(mockEnvironment.httpSetup, { services: { core: { notifications: { @@ -344,7 +351,7 @@ describe('ES deprecation logs', () => { component.update(); - httpRequestsMockHelpers.setLoadDeprecationLogsCountResponse({ count: 0 }); + mockEnvironment.httpRequestsMockHelpers.setLoadDeprecationLogsCountResponse({ count: 0 }); await actions.clickResetButton(); @@ -361,11 +368,11 @@ describe('ES deprecation logs', () => { jest.useFakeTimers(); // First request should make the step be complete - httpRequestsMockHelpers.setLoadDeprecationLogsCountResponse({ + mockEnvironment.httpRequestsMockHelpers.setLoadDeprecationLogsCountResponse({ count: 0, }); - testBed = await setupESDeprecationLogsPage(); + testBed = await setupESDeprecationLogsPage(mockEnvironment.httpSetup); }); afterEach(() => { @@ -383,7 +390,10 @@ describe('ES deprecation logs', () => { error: 'Internal server error', message: 'Internal server error', }; - httpRequestsMockHelpers.setLoadDeprecationLogsCountResponse(undefined, error); + mockEnvironment.httpRequestsMockHelpers.setLoadDeprecationLogsCountResponse( + undefined, + error + ); // Resolve the polling timeout. await advanceTime(DEPRECATION_LOGS_COUNT_POLL_INTERVAL_MS); @@ -396,12 +406,14 @@ describe('ES deprecation logs', () => { describe('Step 4 - API compatibility header', () => { beforeEach(async () => { - httpRequestsMockHelpers.setLoadDeprecationLoggingResponse(getLoggingResponse(true)); + mockEnvironment.httpRequestsMockHelpers.setLoadDeprecationLoggingResponse( + getLoggingResponse(true) + ); }); test('It shows copy with compatibility api header advice', async () => { await act(async () => { - testBed = await setupESDeprecationLogsPage(); + testBed = await setupESDeprecationLogsPage(mockEnvironment.httpSetup); }); const { exists, component } = testBed; @@ -425,7 +437,7 @@ describe('ES deprecation logs', () => { test(`doesn't show analyze and resolve logs if it doesn't have the right privileges`, async () => { await act(async () => { - testBed = await setupESDeprecationLogsPage({ + testBed = await setupESDeprecationLogsPage(mockEnvironment.httpSetup, { privileges: { hasAllPrivileges: false, missingPrivileges: { diff --git a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecations/default_deprecation_flyout.test.ts b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecations/default_deprecation_flyout.test.ts index 5566ec1d17e2b..77e17d1ad4d09 100644 --- a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecations/default_deprecation_flyout.test.ts +++ b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecations/default_deprecation_flyout.test.ts @@ -13,21 +13,19 @@ import { esDeprecationsMockResponse, MOCK_SNAPSHOT_ID, MOCK_JOB_ID } from './moc describe('Default deprecation flyout', () => { let testBed: ElasticsearchTestBed; - const { server, httpRequestsMockHelpers } = setupEnvironment(); - - afterAll(() => { - server.restore(); - }); - + let mockEnvironment: ReturnType; beforeEach(async () => { - httpRequestsMockHelpers.setLoadEsDeprecationsResponse(esDeprecationsMockResponse); - httpRequestsMockHelpers.setUpgradeMlSnapshotStatusResponse({ + mockEnvironment = setupEnvironment(); + mockEnvironment.httpRequestsMockHelpers.setLoadEsDeprecationsResponse( + esDeprecationsMockResponse + ); + mockEnvironment.httpRequestsMockHelpers.setUpgradeMlSnapshotStatusResponse({ nodeId: 'my_node', snapshotId: MOCK_SNAPSHOT_ID, jobId: MOCK_JOB_ID, status: 'idle', }); - httpRequestsMockHelpers.setReindexStatusResponse({ + mockEnvironment.httpRequestsMockHelpers.setReindexStatusResponse('reindex_index', { reindexOp: null, warnings: [], hasRequiredPrivileges: true, @@ -39,7 +37,9 @@ describe('Default deprecation flyout', () => { }); await act(async () => { - testBed = await setupElasticsearchPage({ isReadOnlyMode: false }); + testBed = await setupElasticsearchPage(mockEnvironment.httpSetup, { + isReadOnlyMode: false, + }); }); testBed.component.update(); diff --git a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecations/deprecations_list.test.ts b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecations/deprecations_list.test.ts index ca393e165e335..ad33c676ba434 100644 --- a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecations/deprecations_list.test.ts +++ b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecations/deprecations_list.test.ts @@ -20,21 +20,19 @@ import { describe('ES deprecations table', () => { let testBed: ElasticsearchTestBed; - const { server, httpRequestsMockHelpers } = setupEnvironment(); - - afterAll(() => { - server.restore(); - }); - + let mockEnvironment: ReturnType; beforeEach(async () => { - httpRequestsMockHelpers.setLoadEsDeprecationsResponse(esDeprecationsMockResponse); - httpRequestsMockHelpers.setUpgradeMlSnapshotStatusResponse({ + mockEnvironment = setupEnvironment(); + mockEnvironment.httpRequestsMockHelpers.setLoadEsDeprecationsResponse( + esDeprecationsMockResponse + ); + mockEnvironment.httpRequestsMockHelpers.setUpgradeMlSnapshotStatusResponse({ nodeId: 'my_node', snapshotId: MOCK_SNAPSHOT_ID, jobId: MOCK_JOB_ID, status: 'idle', }); - httpRequestsMockHelpers.setReindexStatusResponse({ + mockEnvironment.httpRequestsMockHelpers.setReindexStatusResponse('reindex_index', { reindexOp: null, warnings: [], hasRequiredPrivileges: true, @@ -44,10 +42,10 @@ describe('ES deprecations table', () => { aliases: [], }, }); - httpRequestsMockHelpers.setLoadRemoteClustersResponse([]); + mockEnvironment.httpRequestsMockHelpers.setLoadRemoteClustersResponse([]); await act(async () => { - testBed = await setupElasticsearchPage({ isReadOnlyMode: false }); + testBed = await setupElasticsearchPage(mockEnvironment.httpSetup, { isReadOnlyMode: false }); }); testBed.component.update(); @@ -66,7 +64,6 @@ describe('ES deprecations table', () => { it('refreshes deprecation data', async () => { const { actions } = testBed; - const totalRequests = server.requests.length; await actions.table.clickRefreshButton(); @@ -74,21 +71,24 @@ describe('ES deprecations table', () => { const reindexDeprecation = esDeprecationsMockResponse.deprecations[3]; // Since upgradeStatusMockResponse includes ML and reindex actions (which require fetching status), there will be 4 requests made - expect(server.requests.length).toBe(totalRequests + 4); - expect(server.requests[server.requests.length - 4].url).toBe( - `${API_BASE_PATH}/es_deprecations` + expect(mockEnvironment.httpSetup.get).toHaveBeenCalledWith( + `${API_BASE_PATH}/es_deprecations`, + expect.anything() ); - expect(server.requests[server.requests.length - 3].url).toBe( + expect(mockEnvironment.httpSetup.get).toHaveBeenCalledWith( `${API_BASE_PATH}/ml_snapshots/${(mlDeprecation.correctiveAction as MlAction).jobId}/${ (mlDeprecation.correctiveAction as MlAction).snapshotId - }` + }`, + expect.anything() ); - expect(server.requests[server.requests.length - 2].url).toBe( - `${API_BASE_PATH}/reindex/${reindexDeprecation.index}` + expect(mockEnvironment.httpSetup.get).toHaveBeenCalledWith( + `${API_BASE_PATH}/reindex/${reindexDeprecation.index}`, + expect.anything() ); - expect(server.requests[server.requests.length - 1].url).toBe( - `${API_BASE_PATH}/ml_upgrade_mode` + expect(mockEnvironment.httpSetup.get).toHaveBeenCalledWith( + `${API_BASE_PATH}/ml_upgrade_mode`, + expect.anything() ); }); @@ -108,10 +108,14 @@ describe('ES deprecations table', () => { describe('remote clusters callout', () => { beforeEach(async () => { - httpRequestsMockHelpers.setLoadRemoteClustersResponse(['test_remote_cluster']); + mockEnvironment.httpRequestsMockHelpers.setLoadRemoteClustersResponse([ + 'test_remote_cluster', + ]); await act(async () => { - testBed = await setupElasticsearchPage({ isReadOnlyMode: false }); + testBed = await setupElasticsearchPage(mockEnvironment.httpSetup, { + isReadOnlyMode: false, + }); }); testBed.component.update(); @@ -206,10 +210,10 @@ describe('ES deprecations table', () => { const { deprecations } = esDeprecationsMockResponseWithManyDeprecations; beforeEach(async () => { - httpRequestsMockHelpers.setLoadEsDeprecationsResponse( + mockEnvironment.httpRequestsMockHelpers.setLoadEsDeprecationsResponse( esDeprecationsMockResponseWithManyDeprecations ); - httpRequestsMockHelpers.setUpgradeMlSnapshotStatusResponse({ + mockEnvironment.httpRequestsMockHelpers.setUpgradeMlSnapshotStatusResponse({ nodeId: 'my_node', snapshotId: MOCK_SNAPSHOT_ID, jobId: MOCK_JOB_ID, @@ -217,7 +221,9 @@ describe('ES deprecations table', () => { }); await act(async () => { - testBed = await setupElasticsearchPage({ isReadOnlyMode: false }); + testBed = await setupElasticsearchPage(mockEnvironment.httpSetup, { + isReadOnlyMode: false, + }); }); testBed.component.update(); @@ -296,10 +302,12 @@ describe('ES deprecations table', () => { deprecations: [], }; - httpRequestsMockHelpers.setLoadEsDeprecationsResponse(noDeprecationsResponse); + mockEnvironment.httpRequestsMockHelpers.setLoadEsDeprecationsResponse(noDeprecationsResponse); await act(async () => { - testBed = await setupElasticsearchPage({ isReadOnlyMode: false }); + testBed = await setupElasticsearchPage(mockEnvironment.httpSetup, { + isReadOnlyMode: false, + }); }); testBed.component.update(); diff --git a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecations/error_handling.test.ts b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecations/error_handling.test.ts index 2f0c8f0597ec3..646df47835e42 100644 --- a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecations/error_handling.test.ts +++ b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecations/error_handling.test.ts @@ -12,10 +12,9 @@ import { ElasticsearchTestBed, setupElasticsearchPage } from './es_deprecations. describe('Error handling', () => { let testBed: ElasticsearchTestBed; - const { server, httpRequestsMockHelpers } = setupEnvironment(); - - afterAll(() => { - server.restore(); + let mockEnvironment: ReturnType; + beforeEach(async () => { + mockEnvironment = setupEnvironment(); }); it('handles 403', async () => { @@ -25,10 +24,10 @@ describe('Error handling', () => { message: 'Forbidden', }; - httpRequestsMockHelpers.setLoadEsDeprecationsResponse(undefined, error); + mockEnvironment.httpRequestsMockHelpers.setLoadEsDeprecationsResponse(undefined, error); await act(async () => { - testBed = await setupElasticsearchPage({ isReadOnlyMode: false }); + testBed = await setupElasticsearchPage(mockEnvironment.httpSetup, { isReadOnlyMode: false }); }); const { component, find } = testBed; @@ -50,10 +49,10 @@ describe('Error handling', () => { }, }; - httpRequestsMockHelpers.setLoadEsDeprecationsResponse(undefined, error); + mockEnvironment.httpRequestsMockHelpers.setLoadEsDeprecationsResponse(undefined, error); await act(async () => { - testBed = await setupElasticsearchPage({ isReadOnlyMode: false }); + testBed = await setupElasticsearchPage(mockEnvironment.httpSetup, { isReadOnlyMode: false }); }); const { component, find } = testBed; @@ -73,10 +72,10 @@ describe('Error handling', () => { }, }; - httpRequestsMockHelpers.setLoadEsDeprecationsResponse(undefined, error); + mockEnvironment.httpRequestsMockHelpers.setLoadEsDeprecationsResponse(undefined, error); await act(async () => { - testBed = await setupElasticsearchPage({ isReadOnlyMode: false }); + testBed = await setupElasticsearchPage(mockEnvironment.httpSetup, { isReadOnlyMode: false }); }); const { component, find } = testBed; @@ -93,10 +92,10 @@ describe('Error handling', () => { message: 'Internal server error', }; - httpRequestsMockHelpers.setLoadEsDeprecationsResponse(undefined, error); + mockEnvironment.httpRequestsMockHelpers.setLoadEsDeprecationsResponse(undefined, error); await act(async () => { - testBed = await setupElasticsearchPage({ isReadOnlyMode: false }); + testBed = await setupElasticsearchPage(mockEnvironment.httpSetup, { isReadOnlyMode: false }); }); const { component, find } = testBed; diff --git a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecations/es_deprecations.helpers.ts b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecations/es_deprecations.helpers.ts index 08d53d1eaca7e..02fe72883c34f 100644 --- a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecations/es_deprecations.helpers.ts +++ b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecations/es_deprecations.helpers.ts @@ -7,7 +7,8 @@ import { act } from 'react-dom/test-utils'; import { registerTestBed, TestBed, AsyncTestBedConfig } from '@kbn/test-jest-helpers'; -import { EsDeprecations } from '../../../public/application/components/es_deprecations'; +import { HttpSetup } from 'src/core/public'; +import { EsDeprecations } from '../../../public/application/components'; import { WithAppDependencies } from '../helpers'; const testBedConfig: AsyncTestBedConfig = { @@ -146,10 +147,11 @@ const createActions = (testBed: TestBed) => { }; export const setupElasticsearchPage = async ( + httpSetup: HttpSetup, overrides?: Record ): Promise => { const initTestBed = registerTestBed( - WithAppDependencies(EsDeprecations, overrides), + WithAppDependencies(EsDeprecations, httpSetup, overrides), testBedConfig ); const testBed = await initTestBed(); diff --git a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecations/index_settings_deprecation_flyout.test.ts b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecations/index_settings_deprecation_flyout.test.ts index f032c34040bfe..1a022529a33bd 100644 --- a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecations/index_settings_deprecation_flyout.test.ts +++ b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecations/index_settings_deprecation_flyout.test.ts @@ -9,38 +9,44 @@ import { act } from 'react-dom/test-utils'; import { setupEnvironment } from '../helpers'; import { ElasticsearchTestBed, setupElasticsearchPage } from './es_deprecations.helpers'; -import { esDeprecationsMockResponse, MOCK_SNAPSHOT_ID, MOCK_JOB_ID } from './mocked_responses'; +import { + esDeprecationsMockResponse, + MOCK_SNAPSHOT_ID, + MOCK_JOB_ID, + MOCK_REINDEX_DEPRECATION, +} from './mocked_responses'; describe('Index settings deprecation flyout', () => { let testBed: ElasticsearchTestBed; - const { server, httpRequestsMockHelpers } = setupEnvironment(); + let mockEnvironment: ReturnType; const indexSettingDeprecation = esDeprecationsMockResponse.deprecations[1]; - - afterAll(() => { - server.restore(); - }); - beforeEach(async () => { - httpRequestsMockHelpers.setLoadEsDeprecationsResponse(esDeprecationsMockResponse); - httpRequestsMockHelpers.setUpgradeMlSnapshotStatusResponse({ + mockEnvironment = setupEnvironment(); + mockEnvironment.httpRequestsMockHelpers.setLoadEsDeprecationsResponse( + esDeprecationsMockResponse + ); + mockEnvironment.httpRequestsMockHelpers.setUpgradeMlSnapshotStatusResponse({ nodeId: 'my_node', snapshotId: MOCK_SNAPSHOT_ID, jobId: MOCK_JOB_ID, status: 'idle', }); - httpRequestsMockHelpers.setReindexStatusResponse({ - reindexOp: null, - warnings: [], - hasRequiredPrivileges: true, - meta: { - indexName: 'foo', - reindexName: 'reindexed-foo', - aliases: [], - }, - }); + mockEnvironment.httpRequestsMockHelpers.setReindexStatusResponse( + MOCK_REINDEX_DEPRECATION.index!, + { + reindexOp: null, + warnings: [], + hasRequiredPrivileges: true, + meta: { + indexName: 'foo', + reindexName: 'reindexed-foo', + aliases: [], + }, + } + ); await act(async () => { - testBed = await setupElasticsearchPage({ isReadOnlyMode: false }); + testBed = await setupElasticsearchPage(mockEnvironment.httpSetup, { isReadOnlyMode: false }); }); const { actions, component } = testBed; @@ -48,7 +54,7 @@ describe('Index settings deprecation flyout', () => { await actions.table.clickDeprecationRowAt('indexSetting', 0); }); - test('renders a flyout with deprecation details', async () => { + it('renders a flyout with deprecation details', async () => { const { find, exists } = testBed; expect(exists('indexSettingsDetails')).toBe(true); @@ -64,21 +70,19 @@ describe('Index settings deprecation flyout', () => { it('removes deprecated index settings', async () => { const { find, actions, exists } = testBed; - httpRequestsMockHelpers.setUpdateIndexSettingsResponse({ - acknowledged: true, - }); + mockEnvironment.httpRequestsMockHelpers.setUpdateIndexSettingsResponse( + indexSettingDeprecation.index!, + { acknowledged: true } + ); expect(exists('indexSettingsDetails.warningDeprecationBadge')).toBe(true); await actions.indexSettingsDeprecationFlyout.clickDeleteSettingsButton(); - const request = server.requests[server.requests.length - 1]; - - expect(request.method).toBe('POST'); - expect(request.url).toBe( - `/api/upgrade_assistant/${indexSettingDeprecation.index!}/index_settings` + expect(mockEnvironment.httpSetup.post).toHaveBeenLastCalledWith( + `/api/upgrade_assistant/${indexSettingDeprecation.index!}/index_settings`, + expect.anything() ); - expect(request.status).toEqual(200); // Verify the "Resolution" column of the table is updated expect(find('indexSettingsResolutionStatusCell').at(0).text()).toEqual( @@ -104,17 +108,18 @@ describe('Index settings deprecation flyout', () => { message: 'Remove index settings error', }; - httpRequestsMockHelpers.setUpdateIndexSettingsResponse(undefined, error); + mockEnvironment.httpRequestsMockHelpers.setUpdateIndexSettingsResponse( + indexSettingDeprecation.index!, + undefined, + error + ); await actions.indexSettingsDeprecationFlyout.clickDeleteSettingsButton(); - const request = server.requests[server.requests.length - 1]; - - expect(request.method).toBe('POST'); - expect(request.url).toBe( - `/api/upgrade_assistant/${indexSettingDeprecation.index!}/index_settings` + expect(mockEnvironment.httpSetup.post).toHaveBeenLastCalledWith( + `/api/upgrade_assistant/${indexSettingDeprecation.index!}/index_settings`, + expect.anything() ); - expect(request.status).toEqual(500); // Verify the "Resolution" column of the table is updated expect(find('indexSettingsResolutionStatusCell').at(0).text()).toEqual( diff --git a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecations/ml_snapshots_deprecation_flyout.test.ts b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecations/ml_snapshots_deprecation_flyout.test.ts index 11bb27bb8b331..877bf267713db 100644 --- a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecations/ml_snapshots_deprecation_flyout.test.ts +++ b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecations/ml_snapshots_deprecation_flyout.test.ts @@ -14,23 +14,23 @@ import { esDeprecationsMockResponse, MOCK_SNAPSHOT_ID, MOCK_JOB_ID } from './moc describe('Machine learning deprecation flyout', () => { let testBed: ElasticsearchTestBed; - const { server, httpRequestsMockHelpers } = setupEnvironment(); const mlDeprecation = esDeprecationsMockResponse.deprecations[0]; - - afterAll(() => { - server.restore(); - }); - + let mockEnvironment: ReturnType; beforeEach(async () => { - httpRequestsMockHelpers.setLoadEsDeprecationsResponse(esDeprecationsMockResponse); - httpRequestsMockHelpers.setLoadMlUpgradeModeResponse({ mlUpgradeModeEnabled: false }); - httpRequestsMockHelpers.setUpgradeMlSnapshotStatusResponse({ + mockEnvironment = setupEnvironment(); + mockEnvironment.httpRequestsMockHelpers.setLoadEsDeprecationsResponse( + esDeprecationsMockResponse + ); + mockEnvironment.httpRequestsMockHelpers.setLoadMlUpgradeModeResponse({ + mlUpgradeModeEnabled: false, + }); + mockEnvironment.httpRequestsMockHelpers.setUpgradeMlSnapshotStatusResponse({ nodeId: 'my_node', snapshotId: MOCK_SNAPSHOT_ID, jobId: MOCK_JOB_ID, status: 'idle', }); - httpRequestsMockHelpers.setReindexStatusResponse({ + mockEnvironment.httpRequestsMockHelpers.setReindexStatusResponse('reindex_index', { reindexOp: null, warnings: [], hasRequiredPrivileges: true, @@ -42,7 +42,7 @@ describe('Machine learning deprecation flyout', () => { }); await act(async () => { - testBed = await setupElasticsearchPage({ isReadOnlyMode: false }); + testBed = await setupElasticsearchPage(mockEnvironment.httpSetup, { isReadOnlyMode: false }); }); const { actions, component } = testBed; @@ -64,14 +64,14 @@ describe('Machine learning deprecation flyout', () => { it('successfully upgrades snapshots', async () => { const { find, actions, exists } = testBed; - httpRequestsMockHelpers.setUpgradeMlSnapshotResponse({ + mockEnvironment.httpRequestsMockHelpers.setUpgradeMlSnapshotResponse({ nodeId: 'my_node', snapshotId: MOCK_SNAPSHOT_ID, jobId: MOCK_JOB_ID, status: 'in_progress', }); - httpRequestsMockHelpers.setUpgradeMlSnapshotStatusResponse({ + mockEnvironment.httpRequestsMockHelpers.setUpgradeMlSnapshotStatusResponse({ nodeId: 'my_node', snapshotId: MOCK_SNAPSHOT_ID, jobId: MOCK_JOB_ID, @@ -84,15 +84,15 @@ describe('Machine learning deprecation flyout', () => { await actions.mlDeprecationFlyout.clickUpgradeSnapshot(); // First, we expect a POST request to upgrade the snapshot - const upgradeRequest = server.requests[server.requests.length - 2]; - expect(upgradeRequest.method).toBe('POST'); - expect(upgradeRequest.url).toBe('/api/upgrade_assistant/ml_snapshots'); + expect(mockEnvironment.httpSetup.post).toHaveBeenLastCalledWith( + '/api/upgrade_assistant/ml_snapshots', + expect.anything() + ); // Next, we expect a GET request to check the status of the upgrade - const statusRequest = server.requests[server.requests.length - 1]; - expect(statusRequest.method).toBe('GET'); - expect(statusRequest.url).toBe( - `/api/upgrade_assistant/ml_snapshots/${MOCK_JOB_ID}/${MOCK_SNAPSHOT_ID}` + expect(mockEnvironment.httpSetup.get).toHaveBeenLastCalledWith( + `/api/upgrade_assistant/ml_snapshots/${MOCK_JOB_ID}/${MOCK_SNAPSHOT_ID}`, + expect.anything() ); // Verify the "Resolution" column of the table is updated @@ -117,8 +117,8 @@ describe('Machine learning deprecation flyout', () => { message: 'Upgrade snapshot error', }; - httpRequestsMockHelpers.setUpgradeMlSnapshotResponse(undefined, error); - httpRequestsMockHelpers.setUpgradeMlSnapshotStatusResponse({ + mockEnvironment.httpRequestsMockHelpers.setUpgradeMlSnapshotResponse(undefined, error); + mockEnvironment.httpRequestsMockHelpers.setUpgradeMlSnapshotStatusResponse({ nodeId: 'my_node', snapshotId: MOCK_SNAPSHOT_ID, jobId: MOCK_JOB_ID, @@ -128,9 +128,10 @@ describe('Machine learning deprecation flyout', () => { await actions.mlDeprecationFlyout.clickUpgradeSnapshot(); - const upgradeRequest = server.requests[server.requests.length - 1]; - expect(upgradeRequest.method).toBe('POST'); - expect(upgradeRequest.url).toBe('/api/upgrade_assistant/ml_snapshots'); + expect(mockEnvironment.httpSetup.post).toHaveBeenLastCalledWith( + '/api/upgrade_assistant/ml_snapshots', + expect.anything() + ); // Verify the "Resolution" column of the table is updated expect(find('mlActionResolutionCell').text()).toContain('Upgrade failed'); @@ -147,10 +148,14 @@ describe('Machine learning deprecation flyout', () => { }); it('Disables actions if ml_upgrade_mode is enabled', async () => { - httpRequestsMockHelpers.setLoadMlUpgradeModeResponse({ mlUpgradeModeEnabled: true }); + mockEnvironment.httpRequestsMockHelpers.setLoadMlUpgradeModeResponse({ + mlUpgradeModeEnabled: true, + }); await act(async () => { - testBed = await setupElasticsearchPage({ isReadOnlyMode: false }); + testBed = await setupElasticsearchPage(mockEnvironment.httpSetup, { + isReadOnlyMode: false, + }); }); const { actions, exists, component } = testBed; @@ -172,7 +177,9 @@ describe('Machine learning deprecation flyout', () => { it('successfully deletes snapshots', async () => { const { find, actions, exists } = testBed; - httpRequestsMockHelpers.setDeleteMlSnapshotResponse({ + const jobId = (mlDeprecation.correctiveAction! as MlAction).jobId; + const snapshotId = (mlDeprecation.correctiveAction! as MlAction).snapshotId; + mockEnvironment.httpRequestsMockHelpers.setDeleteMlSnapshotResponse(jobId, snapshotId, { acknowledged: true, }); @@ -181,13 +188,9 @@ describe('Machine learning deprecation flyout', () => { await actions.mlDeprecationFlyout.clickDeleteSnapshot(); - const request = server.requests[server.requests.length - 1]; - - expect(request.method).toBe('DELETE'); - expect(request.url).toBe( - `/api/upgrade_assistant/ml_snapshots/${ - (mlDeprecation.correctiveAction! as MlAction).jobId - }/${(mlDeprecation.correctiveAction! as MlAction).snapshotId}` + expect(mockEnvironment.httpSetup.delete).toHaveBeenLastCalledWith( + `/api/upgrade_assistant/ml_snapshots/${jobId}/${snapshotId}`, + expect.anything() ); // Verify the "Resolution" column of the table is updated @@ -212,17 +215,20 @@ describe('Machine learning deprecation flyout', () => { message: 'Upgrade snapshot error', }; - httpRequestsMockHelpers.setDeleteMlSnapshotResponse(undefined, error); + const jobId = (mlDeprecation.correctiveAction! as MlAction).jobId; + const snapshotId = (mlDeprecation.correctiveAction! as MlAction).snapshotId; + mockEnvironment.httpRequestsMockHelpers.setDeleteMlSnapshotResponse( + jobId, + snapshotId, + undefined, + error + ); await actions.mlDeprecationFlyout.clickDeleteSnapshot(); - const request = server.requests[server.requests.length - 1]; - - expect(request.method).toBe('DELETE'); - expect(request.url).toBe( - `/api/upgrade_assistant/ml_snapshots/${ - (mlDeprecation.correctiveAction! as MlAction).jobId - }/${(mlDeprecation.correctiveAction! as MlAction).snapshotId}` + expect(mockEnvironment.httpSetup.delete).toHaveBeenLastCalledWith( + `/api/upgrade_assistant/ml_snapshots/${jobId}/${snapshotId}`, + expect.anything() ); // Verify the "Resolution" column of the table is updated diff --git a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecations/mocked_responses.ts b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecations/mocked_responses.ts index ddf477195063c..09f60a8dd3074 100644 --- a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecations/mocked_responses.ts +++ b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecations/mocked_responses.ts @@ -26,7 +26,7 @@ export const MOCK_ML_DEPRECATION: EnrichedDeprecationInfo = { }, }; -const MOCK_REINDEX_DEPRECATION: EnrichedDeprecationInfo = { +export const MOCK_REINDEX_DEPRECATION: EnrichedDeprecationInfo = { isCritical: true, resolveDuringUpgrade: false, type: 'index_settings', diff --git a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecations/reindex_deprecation_flyout.test.ts b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecations/reindex_deprecation_flyout.test.ts index 25742958aa243..01258b5ae4cf6 100644 --- a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecations/reindex_deprecation_flyout.test.ts +++ b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecations/reindex_deprecation_flyout.test.ts @@ -10,7 +10,12 @@ import { act } from 'react-dom/test-utils'; import { ReindexStatus, ReindexStep, ReindexStatusResponse } from '../../../common/types'; import { setupEnvironment } from '../helpers'; import { ElasticsearchTestBed, setupElasticsearchPage } from './es_deprecations.helpers'; -import { esDeprecationsMockResponse, MOCK_SNAPSHOT_ID, MOCK_JOB_ID } from './mocked_responses'; +import { + esDeprecationsMockResponse, + MOCK_SNAPSHOT_ID, + MOCK_JOB_ID, + MOCK_REINDEX_DEPRECATION, +} from './mocked_responses'; const defaultReindexStatusMeta: ReindexStatusResponse['meta'] = { indexName: 'foo', @@ -21,7 +26,6 @@ const defaultReindexStatusMeta: ReindexStatusResponse['meta'] = { // Note: The reindexing flyout UX is subject to change; more tests should be added here once functionality is built out describe('Reindex deprecation flyout', () => { let testBed: ElasticsearchTestBed; - const { server, httpRequestsMockHelpers } = setupEnvironment(); beforeAll(() => { jest.useFakeTimers(); @@ -29,30 +33,36 @@ describe('Reindex deprecation flyout', () => { afterAll(() => { jest.useRealTimers(); - server.restore(); }); + let mockEnvironment: ReturnType; beforeEach(async () => { - httpRequestsMockHelpers.setLoadEsDeprecationsResponse(esDeprecationsMockResponse); - httpRequestsMockHelpers.setUpgradeMlSnapshotStatusResponse({ + mockEnvironment = setupEnvironment(); + mockEnvironment.httpRequestsMockHelpers.setLoadEsDeprecationsResponse( + esDeprecationsMockResponse + ); + mockEnvironment.httpRequestsMockHelpers.setUpgradeMlSnapshotStatusResponse({ nodeId: 'my_node', snapshotId: MOCK_SNAPSHOT_ID, jobId: MOCK_JOB_ID, status: 'idle', }); - httpRequestsMockHelpers.setReindexStatusResponse({ - reindexOp: null, - warnings: [], - hasRequiredPrivileges: true, - meta: { - indexName: 'foo', - reindexName: 'reindexed-foo', - aliases: [], - }, - }); + mockEnvironment.httpRequestsMockHelpers.setReindexStatusResponse( + MOCK_REINDEX_DEPRECATION.index!, + { + reindexOp: null, + warnings: [], + hasRequiredPrivileges: true, + meta: { + indexName: 'foo', + reindexName: 'reindexed-foo', + aliases: [], + }, + } + ); await act(async () => { - testBed = await setupElasticsearchPage({ isReadOnlyMode: false }); + testBed = await setupElasticsearchPage(mockEnvironment.httpSetup, { isReadOnlyMode: false }); }); testBed.component.update(); @@ -71,15 +81,8 @@ describe('Reindex deprecation flyout', () => { }); it('renders error callout when reindex fails', async () => { - httpRequestsMockHelpers.setReindexStatusResponse({ - reindexOp: null, - warnings: [], - hasRequiredPrivileges: true, - meta: defaultReindexStatusMeta, - }); - await act(async () => { - testBed = await setupElasticsearchPage({ isReadOnlyMode: false }); + testBed = await setupElasticsearchPage(mockEnvironment.httpSetup, { isReadOnlyMode: false }); }); testBed.component.update(); @@ -88,10 +91,11 @@ describe('Reindex deprecation flyout', () => { await actions.table.clickDeprecationRowAt('reindex', 0); - httpRequestsMockHelpers.setStartReindexingResponse(undefined, { - statusCode: 404, - message: 'no such index [test]', - }); + mockEnvironment.httpRequestsMockHelpers.setStartReindexingResponse( + MOCK_REINDEX_DEPRECATION.index!, + undefined, + { statusCode: 404, message: 'no such index [test]' } + ); await actions.reindexDeprecationFlyout.clickReindexButton(); @@ -99,13 +103,14 @@ describe('Reindex deprecation flyout', () => { }); it('renders error callout when fetch status fails', async () => { - httpRequestsMockHelpers.setReindexStatusResponse(undefined, { - statusCode: 404, - message: 'no such index [test]', - }); + mockEnvironment.httpRequestsMockHelpers.setReindexStatusResponse( + MOCK_REINDEX_DEPRECATION.index!, + undefined, + { statusCode: 404, message: 'no such index [test]' } + ); await act(async () => { - testBed = await setupElasticsearchPage({ isReadOnlyMode: false }); + testBed = await setupElasticsearchPage(mockEnvironment.httpSetup, { isReadOnlyMode: false }); }); testBed.component.update(); @@ -127,19 +132,24 @@ describe('Reindex deprecation flyout', () => { }); it('has started but not yet reindexing documents', async () => { - httpRequestsMockHelpers.setReindexStatusResponse({ - reindexOp: { - status: ReindexStatus.inProgress, - lastCompletedStep: ReindexStep.readonly, - reindexTaskPercComplete: null, - }, - warnings: [], - hasRequiredPrivileges: true, - meta: defaultReindexStatusMeta, - }); + mockEnvironment.httpRequestsMockHelpers.setReindexStatusResponse( + MOCK_REINDEX_DEPRECATION.index!, + { + reindexOp: { + status: ReindexStatus.inProgress, + lastCompletedStep: ReindexStep.readonly, + reindexTaskPercComplete: null, + }, + warnings: [], + hasRequiredPrivileges: true, + meta: defaultReindexStatusMeta, + } + ); await act(async () => { - testBed = await setupElasticsearchPage({ isReadOnlyMode: false }); + testBed = await setupElasticsearchPage(mockEnvironment.httpSetup, { + isReadOnlyMode: false, + }); }); testBed.component.update(); @@ -152,19 +162,24 @@ describe('Reindex deprecation flyout', () => { }); it('has started reindexing documents', async () => { - httpRequestsMockHelpers.setReindexStatusResponse({ - reindexOp: { - status: ReindexStatus.inProgress, - lastCompletedStep: ReindexStep.reindexStarted, - reindexTaskPercComplete: 0.25, - }, - warnings: [], - hasRequiredPrivileges: true, - meta: defaultReindexStatusMeta, - }); + mockEnvironment.httpRequestsMockHelpers.setReindexStatusResponse( + MOCK_REINDEX_DEPRECATION.index!, + { + reindexOp: { + status: ReindexStatus.inProgress, + lastCompletedStep: ReindexStep.reindexStarted, + reindexTaskPercComplete: 0.25, + }, + warnings: [], + hasRequiredPrivileges: true, + meta: defaultReindexStatusMeta, + } + ); await act(async () => { - testBed = await setupElasticsearchPage({ isReadOnlyMode: false }); + testBed = await setupElasticsearchPage(mockEnvironment.httpSetup, { + isReadOnlyMode: false, + }); }); testBed.component.update(); @@ -177,19 +192,24 @@ describe('Reindex deprecation flyout', () => { }); it('has completed reindexing documents', async () => { - httpRequestsMockHelpers.setReindexStatusResponse({ - reindexOp: { - status: ReindexStatus.inProgress, - lastCompletedStep: ReindexStep.reindexCompleted, - reindexTaskPercComplete: 1, - }, - warnings: [], - hasRequiredPrivileges: true, - meta: defaultReindexStatusMeta, - }); + mockEnvironment.httpRequestsMockHelpers.setReindexStatusResponse( + MOCK_REINDEX_DEPRECATION.index!, + { + reindexOp: { + status: ReindexStatus.inProgress, + lastCompletedStep: ReindexStep.reindexCompleted, + reindexTaskPercComplete: 1, + }, + warnings: [], + hasRequiredPrivileges: true, + meta: defaultReindexStatusMeta, + } + ); await act(async () => { - testBed = await setupElasticsearchPage({ isReadOnlyMode: false }); + testBed = await setupElasticsearchPage(mockEnvironment.httpSetup, { + isReadOnlyMode: false, + }); }); testBed.component.update(); @@ -202,19 +222,24 @@ describe('Reindex deprecation flyout', () => { }); it('has completed', async () => { - httpRequestsMockHelpers.setReindexStatusResponse({ - reindexOp: { - status: ReindexStatus.completed, - lastCompletedStep: ReindexStep.aliasCreated, - reindexTaskPercComplete: 1, - }, - warnings: [], - hasRequiredPrivileges: true, - meta: defaultReindexStatusMeta, - }); + mockEnvironment.httpRequestsMockHelpers.setReindexStatusResponse( + MOCK_REINDEX_DEPRECATION.index!, + { + reindexOp: { + status: ReindexStatus.completed, + lastCompletedStep: ReindexStep.aliasCreated, + reindexTaskPercComplete: 1, + }, + warnings: [], + hasRequiredPrivileges: true, + meta: defaultReindexStatusMeta, + } + ); await act(async () => { - testBed = await setupElasticsearchPage({ isReadOnlyMode: false }); + testBed = await setupElasticsearchPage(mockEnvironment.httpSetup, { + isReadOnlyMode: false, + }); }); const { actions, find, exists, component } = testBed; diff --git a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/helpers/http_requests.ts b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/helpers/http_requests.ts index 774dfba4c1b9f..70a7efecd7183 100644 --- a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/helpers/http_requests.ts +++ b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/helpers/http_requests.ts @@ -5,7 +5,7 @@ * 2.0. */ -import sinon, { SinonFakeServer } from 'sinon'; +import { httpServiceMock } from '../../../../../../src/core/public/mocks'; import { API_BASE_PATH } from '../../../common/constants'; import { @@ -15,204 +15,132 @@ import { ResponseError, } from '../../../common/types'; +type HttpMethod = 'GET' | 'PUT' | 'DELETE' | 'POST'; + // Register helpers to mock HTTP Requests -const registerHttpRequestMockHelpers = (server: SinonFakeServer) => { - const setLoadCloudBackupStatusResponse = ( - response?: CloudBackupStatus, - error?: ResponseError - ) => { - const status = error ? error.statusCode || 400 : 200; - const body = error ? error : response; +const registerHttpRequestMockHelpers = ( + httpSetup: ReturnType, + shouldDelayResponse: () => boolean +) => { + const mockResponses = new Map>>( + ['GET', 'PUT', 'DELETE', 'POST'].map( + (method) => [method, new Map()] as [HttpMethod, Map>] + ) + ); + + const mockMethodImplementation = (method: HttpMethod, path: string) => { + const responsePromise = mockResponses.get(method)?.get(path) ?? Promise.resolve({}); + if (shouldDelayResponse()) { + return new Promise((resolve) => { + setTimeout(() => resolve(responsePromise), 1000); + }); + } - server.respondWith('GET', `${API_BASE_PATH}/cloud_backup_status`, [ - status, - { 'Content-Type': 'application/json' }, - JSON.stringify(body), - ]); + return responsePromise; }; - const setLoadEsDeprecationsResponse = (response?: ESUpgradeStatus, error?: ResponseError) => { - const status = error ? error.statusCode || 400 : 200; - const body = error ? error : response; + httpSetup.get.mockImplementation((path) => + mockMethodImplementation('GET', path as unknown as string) + ); + httpSetup.delete.mockImplementation((path) => + mockMethodImplementation('DELETE', path as unknown as string) + ); + httpSetup.post.mockImplementation((path) => + mockMethodImplementation('POST', path as unknown as string) + ); + httpSetup.put.mockImplementation((path) => + mockMethodImplementation('PUT', path as unknown as string) + ); - server.respondWith('GET', `${API_BASE_PATH}/es_deprecations`, [ - status, - { 'Content-Type': 'application/json' }, - JSON.stringify(body), - ]); + const mockResponse = (method: HttpMethod, path: string, response?: unknown, error?: unknown) => { + const defuse = (promise: Promise) => { + promise.catch(() => {}); + return promise; + }; + + return mockResponses + .get(method)! + .set(path, error ? defuse(Promise.reject({ body: error })) : Promise.resolve(response)); }; + const setLoadCloudBackupStatusResponse = (response?: CloudBackupStatus, error?: ResponseError) => + mockResponse('GET', `${API_BASE_PATH}/cloud_backup_status`, response, error); + + const setLoadEsDeprecationsResponse = (response?: ESUpgradeStatus, error?: ResponseError) => + mockResponse('GET', `${API_BASE_PATH}/es_deprecations`, response, error); + const setLoadDeprecationLoggingResponse = ( response?: DeprecationLoggingStatus, error?: ResponseError - ) => { - const status = error ? error.statusCode || 400 : 200; - const body = error ? error : response; - - server.respondWith('GET', `${API_BASE_PATH}/deprecation_logging`, [ - status, - { 'Content-Type': 'application/json' }, - JSON.stringify(body), - ]); - }; + ) => mockResponse('GET', `${API_BASE_PATH}/deprecation_logging`, response, error); const setLoadDeprecationLogsCountResponse = ( response?: { count: number }, error?: ResponseError - ) => { - const status = error ? error.statusCode || 400 : 200; - const body = error ? error : response; - - server.respondWith('GET', `${API_BASE_PATH}/deprecation_logging/count`, [ - status, - { 'Content-Type': 'application/json' }, - JSON.stringify(body), - ]); - }; + ) => mockResponse('GET', `${API_BASE_PATH}/deprecation_logging/count`, response, error); - const setDeleteLogsCacheResponse = (response?: string, error?: ResponseError) => { - const status = error ? error.statusCode || 400 : 200; - const body = error ? error : response; - server.respondWith('DELETE', `${API_BASE_PATH}/deprecation_logging/cache`, [ - status, - { 'Content-Type': 'application/json' }, - JSON.stringify(body), - ]); - }; + const setDeleteLogsCacheResponse = (response?: string, error?: ResponseError) => + mockResponse('DELETE', `${API_BASE_PATH}/deprecation_logging/cache`, response, error); const setUpdateDeprecationLoggingResponse = ( response?: DeprecationLoggingStatus, error?: ResponseError - ) => { - const status = error ? error.statusCode || 400 : 200; - const body = error ? error : response; + ) => mockResponse('PUT', `${API_BASE_PATH}/deprecation_logging`, response, error); - server.respondWith('PUT', `${API_BASE_PATH}/deprecation_logging`, [ - status, - { 'Content-Type': 'application/json' }, - JSON.stringify(body), - ]); - }; - - const setUpdateIndexSettingsResponse = (response?: object, error?: ResponseError) => { - const status = error ? error.statusCode || 400 : 200; - const body = error ? error : response; - server.respondWith('POST', `${API_BASE_PATH}/:indexName/index_settings`, [ - status, - { 'Content-Type': 'application/json' }, - JSON.stringify(body), - ]); - }; - - const setUpgradeMlSnapshotResponse = (response?: object, error?: ResponseError) => { - const status = error ? error.statusCode || 400 : 200; - const body = error ? error : response; - - server.respondWith('POST', `${API_BASE_PATH}/ml_snapshots`, [ - status, - { 'Content-Type': 'application/json' }, - JSON.stringify(body), - ]); - }; - - const setUpgradeMlSnapshotStatusResponse = (response?: object, error?: ResponseError) => { - const status = error ? error.statusCode || 400 : 200; - const body = error ? error : response; - - server.respondWith('GET', `${API_BASE_PATH}/ml_snapshots/:jobId/:snapshotId`, [ - status, - { 'Content-Type': 'application/json' }, - JSON.stringify(body), - ]); - }; - - const setReindexStatusResponse = (response?: object, error?: ResponseError) => { - const status = error ? error.statusCode || 400 : 200; - const body = error ? error : response; - - server.respondWith('GET', `${API_BASE_PATH}/reindex/:indexName`, [ - status, - { 'Content-Type': 'application/json' }, - JSON.stringify(body), - ]); - }; - - const setStartReindexingResponse = (response?: object, error?: ResponseError) => { - const status = error ? error.statusCode || 400 : 200; - const body = error ? error : response; - - server.respondWith('POST', `${API_BASE_PATH}/reindex/:indexName`, [ - status, - { 'Content-Type': 'application/json' }, - JSON.stringify(body), - ]); - }; - - const setDeleteMlSnapshotResponse = (response?: object, error?: ResponseError) => { - const status = error ? error.statusCode || 400 : 200; - const body = error ? error : response; - - server.respondWith('DELETE', `${API_BASE_PATH}/ml_snapshots/:jobId/:snapshotId`, [ - status, - { 'Content-Type': 'application/json' }, - JSON.stringify(body), - ]); - }; - - const setLoadSystemIndicesMigrationStatus = (response?: object, error?: ResponseError) => { - const status = error ? error.statusCode || 400 : 200; - const body = error ? error : response; + const setUpdateIndexSettingsResponse = ( + indexName: string, + response?: object, + error?: ResponseError + ) => mockResponse('POST', `${API_BASE_PATH}/${indexName}/index_settings`, response, error); - server.respondWith('GET', `${API_BASE_PATH}/system_indices_migration`, [ - status, - { 'Content-Type': 'application/json' }, - JSON.stringify(body), - ]); - }; + const setUpgradeMlSnapshotResponse = (response?: object, error?: ResponseError) => + mockResponse('POST', `${API_BASE_PATH}/ml_snapshots`, response, error); - const setLoadMlUpgradeModeResponse = (response?: object, error?: ResponseError) => { - const status = error ? error.statusCode || 400 : 200; - const body = error ? error : response; + const setUpgradeMlSnapshotStatusResponse = ( + response?: Record, + error?: ResponseError + ) => + mockResponse( + 'GET', + `${API_BASE_PATH}/ml_snapshots/${response?.jobId}/${response?.snapshotId}`, + response, + error + ); + + const setReindexStatusResponse = ( + indexName: string, + response?: Record, + error?: ResponseError + ) => mockResponse('GET', `${API_BASE_PATH}/reindex/${indexName}`, response, error); - server.respondWith('GET', `${API_BASE_PATH}/ml_upgrade_mode`, [ - status, - { 'Content-Type': 'application/json' }, - JSON.stringify(body), - ]); - }; + const setStartReindexingResponse = ( + indexName: string, + response?: object, + error?: ResponseError + ) => mockResponse('POST', `${API_BASE_PATH}/reindex/${indexName}`, response, error); - const setSystemIndicesMigrationResponse = (response?: object, error?: ResponseError) => { - const status = error ? error.statusCode || 400 : 200; - const body = error ? error : response; + const setDeleteMlSnapshotResponse = ( + jobId: string, + snapshotId: string, + response?: object, + error?: ResponseError + ) => + mockResponse('DELETE', `${API_BASE_PATH}/ml_snapshots/${jobId}/${snapshotId}`, response, error); - server.respondWith('POST', `${API_BASE_PATH}/system_indices_migration`, [ - status, - { 'Content-Type': 'application/json' }, - JSON.stringify(body), - ]); - }; + const setLoadSystemIndicesMigrationStatus = (response?: object, error?: ResponseError) => + mockResponse('GET', `${API_BASE_PATH}/system_indices_migration`, response, error); - const setGetUpgradeStatusResponse = (response?: object, error?: ResponseError) => { - const status = error ? error.statusCode || 400 : 200; - const body = error ? error : response; + const setLoadMlUpgradeModeResponse = (response?: object, error?: ResponseError) => + mockResponse('GET', `${API_BASE_PATH}/ml_upgrade_mode`, response, error); - server.respondWith('GET', `${API_BASE_PATH}/status`, [ - status, - { 'Content-Type': 'application/json' }, - JSON.stringify(body), - ]); - }; + const setSystemIndicesMigrationResponse = (response?: object, error?: ResponseError) => + mockResponse('POST', `${API_BASE_PATH}/system_indices_migration`, response, error); - const setLoadRemoteClustersResponse = (response?: object, error?: ResponseError) => { - const status = error ? error.statusCode || 400 : 200; - const body = error ? error : response; + const setGetUpgradeStatusResponse = (response?: object, error?: ResponseError) => + mockResponse('GET', `${API_BASE_PATH}/status`, response, error); - server.respondWith('GET', `${API_BASE_PATH}/remote_clusters`, [ - status, - { 'Content-Type': 'application/json' }, - JSON.stringify(body), - ]); - }; + const setLoadRemoteClustersResponse = (response?: object, error?: ResponseError) => + mockResponse('GET', `${API_BASE_PATH}/remote_clusters`, response, error); return { setLoadCloudBackupStatusResponse, @@ -236,29 +164,18 @@ const registerHttpRequestMockHelpers = (server: SinonFakeServer) => { }; export const init = () => { - const server = sinon.fakeServer.create(); - server.respondImmediately = true; - - // Define default response for unhandled requests. - // We make requests to APIs which don't impact the component under test, e.g. UI metric telemetry, - // and we can mock them all with a 200 instead of mocking each one individually. - server.respondWith([200, {}, 'DefaultMockedResponse']); - - const httpRequestsMockHelpers = registerHttpRequestMockHelpers(server); - - const setServerAsync = (isAsync: boolean, timeout: number = 200) => { - if (isAsync) { - server.autoRespond = true; - server.autoRespondAfter = 1000; - server.respondImmediately = false; - } else { - server.respondImmediately = true; - } + let isResponseDelayed = false; + const getDelayResponse = () => isResponseDelayed; + const setDelayResponse = (shouldDelayResponse: boolean) => { + isResponseDelayed = shouldDelayResponse; }; + const httpSetup = httpServiceMock.createSetupContract(); + const httpRequestsMockHelpers = registerHttpRequestMockHelpers(httpSetup, getDelayResponse); + return { - server, - setServerAsync, + setDelayResponse, + httpSetup, httpRequestsMockHelpers, }; }; diff --git a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/helpers/setup_environment.tsx b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/helpers/setup_environment.tsx index 0e4af0b697a49..7bc6931550a3b 100644 --- a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/helpers/setup_environment.tsx +++ b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/helpers/setup_environment.tsx @@ -6,11 +6,8 @@ */ import React from 'react'; -import axios from 'axios'; import SemVer from 'semver/classes/semver'; import { merge } from 'lodash'; -// @ts-ignore -import axiosXhrAdapter from 'axios/lib/adapters/xhr'; import { HttpSetup } from 'src/core/public'; import { MAJOR_VERSION } from '../../../common/constants'; @@ -26,8 +23,6 @@ import { init as initHttpRequests } from './http_requests'; const { GlobalFlyoutProvider } = GlobalFlyout; -const mockHttpClient = axios.create({ adapter: axiosXhrAdapter }); - export const kibanaVersion = new SemVer(MAJOR_VERSION); const createAuthorizationContextValue = (privileges: Privileges) => { @@ -38,9 +33,9 @@ const createAuthorizationContextValue = (privileges: Privileges) => { }; export const WithAppDependencies = - (Comp: any, { privileges, ...overrides }: Record = {}) => + (Comp: any, httpSetup: HttpSetup, { privileges, ...overrides }: Record = {}) => (props: Record) => { - apiService.setup(mockHttpClient as unknown as HttpSetup); + apiService.setup(httpSetup); breadcrumbService.setup(() => ''); const appContextMock = getAppContextMock(kibanaVersion) as unknown as AppDependencies; @@ -59,11 +54,5 @@ export const WithAppDependencies = }; export const setupEnvironment = () => { - const { server, setServerAsync, httpRequestsMockHelpers } = initHttpRequests(); - - return { - server, - setServerAsync, - httpRequestsMockHelpers, - }; + return initHttpRequests(); }; diff --git a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/kibana_deprecations/deprecation_details_flyout/deprecation_details_flyout.test.ts b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/kibana_deprecations/deprecation_details_flyout/deprecation_details_flyout.test.ts index 9677104a6e558..e235bdc6f4543 100644 --- a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/kibana_deprecations/deprecation_details_flyout/deprecation_details_flyout.test.ts +++ b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/kibana_deprecations/deprecation_details_flyout/deprecation_details_flyout.test.ts @@ -14,21 +14,15 @@ import { KibanaTestBed, setupKibanaPage } from '../kibana_deprecations.helpers'; describe('Kibana deprecations - Deprecation details flyout', () => { let testBed: KibanaTestBed; - const { server } = setupEnvironment(); const { defaultMockedResponses: { mockedKibanaDeprecations }, } = kibanaDeprecationsServiceHelpers; const deprecationService = deprecationsServiceMock.createStartContract(); - - afterAll(() => { - server.restore(); - }); - beforeEach(async () => { await act(async () => { kibanaDeprecationsServiceHelpers.setLoadDeprecations({ deprecationService }); - testBed = await setupKibanaPage({ + testBed = await setupKibanaPage(setupEnvironment().httpSetup, { services: { core: { deprecations: deprecationService, diff --git a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/kibana_deprecations/deprecations_table/deprecations_table.test.ts b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/kibana_deprecations/deprecations_table/deprecations_table.test.ts index a14d6e087b017..99714db08720a 100644 --- a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/kibana_deprecations/deprecations_table/deprecations_table.test.ts +++ b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/kibana_deprecations/deprecations_table/deprecations_table.test.ts @@ -17,7 +17,6 @@ describe('Kibana deprecations - Deprecations table', () => { let testBed: KibanaTestBed; let deprecationService: jest.Mocked; - const { server } = setupEnvironment(); const { mockedKibanaDeprecations, mockedCriticalKibanaDeprecations, @@ -25,17 +24,15 @@ describe('Kibana deprecations - Deprecations table', () => { mockedConfigKibanaDeprecations, } = kibanaDeprecationsServiceHelpers.defaultMockedResponses; - afterAll(() => { - server.restore(); - }); - + let mockEnvironment: ReturnType; beforeEach(async () => { + mockEnvironment = setupEnvironment(); deprecationService = deprecationsServiceMock.createStartContract(); await act(async () => { kibanaDeprecationsServiceHelpers.setLoadDeprecations({ deprecationService }); - testBed = await setupKibanaPage({ + testBed = await setupKibanaPage(mockEnvironment.httpSetup, { services: { core: { deprecations: deprecationService, @@ -108,7 +105,7 @@ describe('Kibana deprecations - Deprecations table', () => { describe('No deprecations', () => { beforeEach(async () => { await act(async () => { - testBed = await setupKibanaPage({ isReadOnlyMode: false }); + testBed = await setupKibanaPage(mockEnvironment.httpSetup, { isReadOnlyMode: false }); }); const { component } = testBed; diff --git a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/kibana_deprecations/deprecations_table/error_handling.test.ts b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/kibana_deprecations/deprecations_table/error_handling.test.ts index 918ee759a0f45..ff0d788415e06 100644 --- a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/kibana_deprecations/deprecations_table/error_handling.test.ts +++ b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/kibana_deprecations/deprecations_table/error_handling.test.ts @@ -14,11 +14,11 @@ import { KibanaTestBed, setupKibanaPage } from '../kibana_deprecations.helpers'; describe('Kibana deprecations - Deprecations table - Error handling', () => { let testBed: KibanaTestBed; - const { server } = setupEnvironment(); const deprecationService = deprecationsServiceMock.createStartContract(); - afterAll(() => { - server.restore(); + let mockEnvironment: ReturnType; + beforeEach(async () => { + mockEnvironment = setupEnvironment(); }); test('handles plugin errors', async () => { @@ -57,7 +57,7 @@ describe('Kibana deprecations - Deprecations table - Error handling', () => { ], }); - testBed = await setupKibanaPage({ + testBed = await setupKibanaPage(mockEnvironment.httpSetup, { services: { core: { deprecations: deprecationService, @@ -83,7 +83,7 @@ describe('Kibana deprecations - Deprecations table - Error handling', () => { mockRequestErrorMessage: 'Internal Server Error', }); - testBed = await setupKibanaPage({ + testBed = await setupKibanaPage(mockEnvironment.httpSetup, { services: { core: { deprecations: deprecationService, diff --git a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/kibana_deprecations/kibana_deprecations.helpers.ts b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/kibana_deprecations/kibana_deprecations.helpers.ts index dba077fc303d2..bde2a78b13302 100644 --- a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/kibana_deprecations/kibana_deprecations.helpers.ts +++ b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/kibana_deprecations/kibana_deprecations.helpers.ts @@ -11,6 +11,7 @@ import { AsyncTestBedConfig, findTestSubject, } from '@kbn/test-jest-helpers'; +import { HttpSetup } from 'src/core/public'; import { KibanaDeprecations } from '../../../public/application/components'; import { WithAppDependencies } from '../helpers'; @@ -118,10 +119,11 @@ const createActions = (testBed: TestBed) => { }; export const setupKibanaPage = async ( + httpSetup: HttpSetup, overrides?: Record ): Promise => { const initTestBed = registerTestBed( - WithAppDependencies(KibanaDeprecations, overrides), + WithAppDependencies(KibanaDeprecations, httpSetup, overrides), testBedConfig ); const testBed = await initTestBed(); diff --git a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/backup_step/backup_step.test.tsx b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/backup_step/backup_step.test.tsx index 4cd4bf3f76629..3f382a671cb16 100644 --- a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/backup_step/backup_step.test.tsx +++ b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/backup_step/backup_step.test.tsx @@ -14,21 +14,14 @@ import { OverviewTestBed, setupOverviewPage } from '../overview.helpers'; describe('Overview - Backup Step', () => { let testBed: OverviewTestBed; - let server: ReturnType['server']; - let setServerAsync: ReturnType['setServerAsync']; - let httpRequestsMockHelpers: ReturnType['httpRequestsMockHelpers']; - - beforeEach(() => { - ({ server, setServerAsync, httpRequestsMockHelpers } = setupEnvironment()); - }); - - afterEach(() => { - server.restore(); + let mockEnvironment: ReturnType; + beforeEach(async () => { + mockEnvironment = setupEnvironment(); }); describe('On-prem', () => { beforeEach(async () => { - testBed = await setupOverviewPage(); + testBed = await setupOverviewPage(mockEnvironment.httpSetup); }); test('Shows link to Snapshot and Restore', () => { @@ -45,7 +38,7 @@ describe('Overview - Backup Step', () => { describe('On Cloud', () => { const setupCloudOverviewPage = async () => - setupOverviewPage({ + setupOverviewPage(mockEnvironment.httpSetup, { plugins: { cloud: { isCloudEnabled: true, @@ -57,14 +50,10 @@ describe('Overview - Backup Step', () => { describe('initial loading state', () => { beforeEach(async () => { // We don't want the request to load backup status to resolve immediately. - setServerAsync(true); + mockEnvironment.setDelayResponse(true); testBed = await setupCloudOverviewPage(); }); - afterEach(() => { - setServerAsync(false); - }); - test('is rendered', () => { const { exists } = testBed; expect(exists('cloudBackupLoading')).toBe(true); @@ -73,7 +62,7 @@ describe('Overview - Backup Step', () => { describe('error state', () => { beforeEach(async () => { - httpRequestsMockHelpers.setLoadCloudBackupStatusResponse(undefined, { + mockEnvironment.httpRequestsMockHelpers.setLoadCloudBackupStatusResponse(undefined, { statusCode: 400, message: 'error', }); @@ -94,7 +83,7 @@ describe('Overview - Backup Step', () => { }); test('loads on prem if missing found-snapshots repository', async () => { - httpRequestsMockHelpers.setLoadCloudBackupStatusResponse(undefined, { + mockEnvironment.httpRequestsMockHelpers.setLoadCloudBackupStatusResponse(undefined, { statusCode: 404, message: `[${CLOUD_SNAPSHOT_REPOSITORY}] missing`, }); @@ -113,7 +102,7 @@ describe('Overview - Backup Step', () => { describe('success state', () => { describe('when data is backed up', () => { beforeEach(async () => { - httpRequestsMockHelpers.setLoadCloudBackupStatusResponse({ + mockEnvironment.httpRequestsMockHelpers.setLoadCloudBackupStatusResponse({ isBackedUp: true, lastBackupTime: '2021-08-25T19:59:59.863Z', }); @@ -136,7 +125,7 @@ describe('Overview - Backup Step', () => { describe(`when data isn't backed up`, () => { beforeEach(async () => { - httpRequestsMockHelpers.setLoadCloudBackupStatusResponse({ + mockEnvironment.httpRequestsMockHelpers.setLoadCloudBackupStatusResponse({ isBackedUp: false, lastBackupTime: undefined, }); @@ -162,7 +151,7 @@ describe('Overview - Backup Step', () => { jest.useFakeTimers(); // First request will succeed. - httpRequestsMockHelpers.setLoadCloudBackupStatusResponse({ + mockEnvironment.httpRequestsMockHelpers.setLoadCloudBackupStatusResponse({ isBackedUp: true, lastBackupTime: '2021-08-25T19:59:59.863Z', }); @@ -179,7 +168,7 @@ describe('Overview - Backup Step', () => { expect(exists('backupStep-complete')).toBe(true); // Second request will error. - httpRequestsMockHelpers.setLoadCloudBackupStatusResponse(undefined, { + mockEnvironment.httpRequestsMockHelpers.setLoadCloudBackupStatusResponse(undefined, { statusCode: 400, message: 'error', }); diff --git a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/fix_issues_step/elasticsearch_deprecation_issues.test.tsx b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/fix_issues_step/elasticsearch_deprecation_issues.test.tsx index e1cef64dfb20c..13c0bf40d4438 100644 --- a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/fix_issues_step/elasticsearch_deprecation_issues.test.tsx +++ b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/fix_issues_step/elasticsearch_deprecation_issues.test.tsx @@ -19,10 +19,9 @@ import { describe('Overview - Fix deprecation issues step - Elasticsearch deprecations', () => { let testBed: OverviewTestBed; - const { server, httpRequestsMockHelpers } = setupEnvironment(); - - afterAll(() => { - server.restore(); + let mockEnvironment: ReturnType; + beforeEach(async () => { + mockEnvironment = setupEnvironment(); }); describe('When load succeeds', () => { @@ -32,7 +31,7 @@ describe('Overview - Fix deprecation issues step - Elasticsearch deprecations', const deprecationService = deprecationsServiceMock.createStartContract(); kibanaDeprecationsServiceHelpers.setLoadDeprecations({ deprecationService, response: [] }); - testBed = await setupOverviewPage({ + testBed = await setupOverviewPage(mockEnvironment.httpSetup, { services: { core: { deprecations: deprecationService, @@ -47,7 +46,9 @@ describe('Overview - Fix deprecation issues step - Elasticsearch deprecations', describe('when there are critical and warning issues', () => { beforeEach(async () => { - httpRequestsMockHelpers.setLoadEsDeprecationsResponse(esCriticalAndWarningDeprecations); + mockEnvironment.httpRequestsMockHelpers.setLoadEsDeprecationsResponse( + esCriticalAndWarningDeprecations + ); await setup(); }); @@ -67,7 +68,9 @@ describe('Overview - Fix deprecation issues step - Elasticsearch deprecations', describe('when there are critical but no warning issues', () => { beforeEach(async () => { - httpRequestsMockHelpers.setLoadEsDeprecationsResponse(esCriticalOnlyDeprecations); + mockEnvironment.httpRequestsMockHelpers.setLoadEsDeprecationsResponse( + esCriticalOnlyDeprecations + ); await setup(); }); @@ -87,7 +90,7 @@ describe('Overview - Fix deprecation issues step - Elasticsearch deprecations', describe('when there no critical or warning issues', () => { beforeEach(async () => { - httpRequestsMockHelpers.setLoadEsDeprecationsResponse(esNoDeprecations); + mockEnvironment.httpRequestsMockHelpers.setLoadEsDeprecationsResponse(esNoDeprecations); await setup(); }); @@ -113,10 +116,10 @@ describe('Overview - Fix deprecation issues step - Elasticsearch deprecations', message: 'Internal server error', }; - httpRequestsMockHelpers.setLoadEsDeprecationsResponse(undefined, error); + mockEnvironment.httpRequestsMockHelpers.setLoadEsDeprecationsResponse(undefined, error); await act(async () => { - testBed = await setupOverviewPage(); + testBed = await setupOverviewPage(mockEnvironment.httpSetup); }); const { component, find } = testBed; @@ -133,10 +136,10 @@ describe('Overview - Fix deprecation issues step - Elasticsearch deprecations', message: 'Forbidden', }; - httpRequestsMockHelpers.setLoadEsDeprecationsResponse(undefined, error); + mockEnvironment.httpRequestsMockHelpers.setLoadEsDeprecationsResponse(undefined, error); await act(async () => { - testBed = await setupOverviewPage(); + testBed = await setupOverviewPage(mockEnvironment.httpSetup); }); const { component, find } = testBed; @@ -156,10 +159,10 @@ describe('Overview - Fix deprecation issues step - Elasticsearch deprecations', }, }; - httpRequestsMockHelpers.setLoadEsDeprecationsResponse(undefined, error); + mockEnvironment.httpRequestsMockHelpers.setLoadEsDeprecationsResponse(undefined, error); await act(async () => { - testBed = await setupOverviewPage({ isReadOnlyMode: false }); + testBed = await setupOverviewPage(mockEnvironment.httpSetup, { isReadOnlyMode: false }); }); const { component, find } = testBed; @@ -179,10 +182,10 @@ describe('Overview - Fix deprecation issues step - Elasticsearch deprecations', }, }; - httpRequestsMockHelpers.setLoadEsDeprecationsResponse(undefined, error); + mockEnvironment.httpRequestsMockHelpers.setLoadEsDeprecationsResponse(undefined, error); await act(async () => { - testBed = await setupOverviewPage({ isReadOnlyMode: false }); + testBed = await setupOverviewPage(mockEnvironment.httpSetup, { isReadOnlyMode: false }); }); const { component, find } = testBed; diff --git a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/fix_issues_step/fix_issues_step.test.tsx b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/fix_issues_step/fix_issues_step.test.tsx index b7c417fbfcb8d..1e4e9cf5a21d1 100644 --- a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/fix_issues_step/fix_issues_step.test.tsx +++ b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/fix_issues_step/fix_issues_step.test.tsx @@ -15,21 +15,22 @@ import { esCriticalAndWarningDeprecations, esNoDeprecations } from './mock_es_is describe('Overview - Fix deprecation issues step', () => { let testBed: OverviewTestBed; - const { server, httpRequestsMockHelpers } = setupEnvironment(); - - afterAll(() => { - server.restore(); + let mockEnvironment: ReturnType; + beforeEach(async () => { + mockEnvironment = setupEnvironment(); }); describe('when there are critical issues in one panel', () => { beforeEach(async () => { - httpRequestsMockHelpers.setLoadEsDeprecationsResponse(esCriticalAndWarningDeprecations); + mockEnvironment.httpRequestsMockHelpers.setLoadEsDeprecationsResponse( + esCriticalAndWarningDeprecations + ); await act(async () => { const deprecationService = deprecationsServiceMock.createStartContract(); kibanaDeprecationsServiceHelpers.setLoadDeprecations({ deprecationService, response: [] }); - testBed = await setupOverviewPage({ + testBed = await setupOverviewPage(mockEnvironment.httpSetup, { services: { core: { deprecations: deprecationService, @@ -49,13 +50,13 @@ describe('Overview - Fix deprecation issues step', () => { describe('when there are no critical issues for either panel', () => { beforeEach(async () => { - httpRequestsMockHelpers.setLoadEsDeprecationsResponse(esNoDeprecations); + mockEnvironment.httpRequestsMockHelpers.setLoadEsDeprecationsResponse(esNoDeprecations); await act(async () => { const deprecationService = deprecationsServiceMock.createStartContract(); kibanaDeprecationsServiceHelpers.setLoadDeprecations({ deprecationService, response: [] }); - testBed = await setupOverviewPage({ + testBed = await setupOverviewPage(mockEnvironment.httpSetup, { services: { core: { deprecations: deprecationService, diff --git a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/fix_issues_step/kibana_deprecation_issues.test.tsx b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/fix_issues_step/kibana_deprecation_issues.test.tsx index c11a1481b68b5..82d632afc5d4d 100644 --- a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/fix_issues_step/kibana_deprecation_issues.test.tsx +++ b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/fix_issues_step/kibana_deprecation_issues.test.tsx @@ -16,24 +16,23 @@ import { esNoDeprecations } from './mock_es_issues'; describe('Overview - Fix deprecation issues step - Kibana deprecations', () => { let testBed: OverviewTestBed; - const { server, httpRequestsMockHelpers } = setupEnvironment(); const { mockedKibanaDeprecations, mockedCriticalKibanaDeprecations } = kibanaDeprecationsServiceHelpers.defaultMockedResponses; - - afterAll(() => { - server.restore(); + let mockEnvironment: ReturnType; + beforeEach(async () => { + mockEnvironment = setupEnvironment(); }); describe('When load succeeds', () => { const setup = async (response: DomainDeprecationDetails[]) => { // Set up with no ES deprecations. - httpRequestsMockHelpers.setLoadEsDeprecationsResponse(esNoDeprecations); + mockEnvironment.httpRequestsMockHelpers.setLoadEsDeprecationsResponse(esNoDeprecations); await act(async () => { const deprecationService = deprecationsServiceMock.createStartContract(); kibanaDeprecationsServiceHelpers.setLoadDeprecations({ deprecationService, response }); - testBed = await setupOverviewPage({ + testBed = await setupOverviewPage(mockEnvironment.httpSetup, { services: { core: { deprecations: deprecationService, @@ -114,7 +113,7 @@ describe('Overview - Fix deprecation issues step - Kibana deprecations', () => { mockRequestErrorMessage: 'Internal Server Error', }); - testBed = await setupOverviewPage({ + testBed = await setupOverviewPage(mockEnvironment.httpSetup, { services: { core: { deprecations: deprecationService, diff --git a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/migrate_system_indices/flyout.test.ts b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/migrate_system_indices/flyout.test.ts index 1e74a966b3933..55c466973939a 100644 --- a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/migrate_system_indices/flyout.test.ts +++ b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/migrate_system_indices/flyout.test.ts @@ -13,22 +13,20 @@ import { systemIndicesMigrationStatus } from './mocks'; describe('Overview - Migrate system indices - Flyout', () => { let testBed: OverviewTestBed; - const { server, httpRequestsMockHelpers } = setupEnvironment(); - + let mockEnvironment: ReturnType; beforeEach(async () => { - httpRequestsMockHelpers.setLoadSystemIndicesMigrationStatus(systemIndicesMigrationStatus); + mockEnvironment = setupEnvironment(); + mockEnvironment.httpRequestsMockHelpers.setLoadSystemIndicesMigrationStatus( + systemIndicesMigrationStatus + ); await act(async () => { - testBed = await setupOverviewPage(); + testBed = await setupOverviewPage(mockEnvironment.httpSetup); }); testBed.component.update(); }); - afterAll(() => { - server.restore(); - }); - test('shows correct features in flyout table', async () => { const { actions, table } = testBed; diff --git a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/migrate_system_indices/migrate_system_indices.test.tsx b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/migrate_system_indices/migrate_system_indices.test.tsx index e3f6d747deaed..140f7f3d1af67 100644 --- a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/migrate_system_indices/migrate_system_indices.test.tsx +++ b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/migrate_system_indices/migrate_system_indices.test.tsx @@ -12,25 +12,21 @@ import { OverviewTestBed, setupOverviewPage } from '../overview.helpers'; describe('Overview - Migrate system indices', () => { let testBed: OverviewTestBed; - const { server, httpRequestsMockHelpers } = setupEnvironment(); - + let mockEnvironment: ReturnType; beforeEach(async () => { - testBed = await setupOverviewPage(); + mockEnvironment = setupEnvironment(); + testBed = await setupOverviewPage(mockEnvironment.httpSetup); testBed.component.update(); }); - afterAll(() => { - server.restore(); - }); - describe('Error state', () => { beforeEach(async () => { - httpRequestsMockHelpers.setLoadSystemIndicesMigrationStatus(undefined, { + mockEnvironment.httpRequestsMockHelpers.setLoadSystemIndicesMigrationStatus(undefined, { statusCode: 400, message: 'error', }); - testBed = await setupOverviewPage(); + testBed = await setupOverviewPage(mockEnvironment.httpSetup); }); test('Is rendered', () => { @@ -44,7 +40,7 @@ describe('Overview - Migrate system indices', () => { const { exists, component, actions } = testBed; component.update(); - httpRequestsMockHelpers.setLoadSystemIndicesMigrationStatus({ + mockEnvironment.httpRequestsMockHelpers.setLoadSystemIndicesMigrationStatus({ migration_status: 'NO_MIGRATION_NEEDED', }); @@ -55,11 +51,11 @@ describe('Overview - Migrate system indices', () => { }); test('No migration needed', async () => { - httpRequestsMockHelpers.setLoadSystemIndicesMigrationStatus({ + mockEnvironment.httpRequestsMockHelpers.setLoadSystemIndicesMigrationStatus({ migration_status: 'NO_MIGRATION_NEEDED', }); - testBed = await setupOverviewPage(); + testBed = await setupOverviewPage(mockEnvironment.httpSetup); const { exists, component } = testBed; @@ -71,11 +67,11 @@ describe('Overview - Migrate system indices', () => { }); test('Migration in progress', async () => { - httpRequestsMockHelpers.setLoadSystemIndicesMigrationStatus({ + mockEnvironment.httpRequestsMockHelpers.setLoadSystemIndicesMigrationStatus({ migration_status: 'IN_PROGRESS', }); - testBed = await setupOverviewPage(); + testBed = await setupOverviewPage(mockEnvironment.httpSetup); const { exists, component, find } = testBed; @@ -90,11 +86,11 @@ describe('Overview - Migrate system indices', () => { describe('Migration needed', () => { test('Initial state', async () => { - httpRequestsMockHelpers.setLoadSystemIndicesMigrationStatus({ + mockEnvironment.httpRequestsMockHelpers.setLoadSystemIndicesMigrationStatus({ migration_status: 'MIGRATION_NEEDED', }); - testBed = await setupOverviewPage(); + testBed = await setupOverviewPage(mockEnvironment.httpSetup); const { exists, component, find } = testBed; @@ -108,15 +104,15 @@ describe('Overview - Migrate system indices', () => { }); test('Handles errors when migrating', async () => { - httpRequestsMockHelpers.setLoadSystemIndicesMigrationStatus({ + mockEnvironment.httpRequestsMockHelpers.setLoadSystemIndicesMigrationStatus({ migration_status: 'MIGRATION_NEEDED', }); - httpRequestsMockHelpers.setSystemIndicesMigrationResponse(undefined, { + mockEnvironment.httpRequestsMockHelpers.setSystemIndicesMigrationResponse(undefined, { statusCode: 400, message: 'error', }); - testBed = await setupOverviewPage(); + testBed = await setupOverviewPage(mockEnvironment.httpSetup); const { exists, component, find } = testBed; @@ -134,7 +130,7 @@ describe('Overview - Migrate system indices', () => { }); test('Handles errors from migration', async () => { - httpRequestsMockHelpers.setLoadSystemIndicesMigrationStatus({ + mockEnvironment.httpRequestsMockHelpers.setLoadSystemIndicesMigrationStatus({ migration_status: 'ERROR', features: [ { @@ -154,7 +150,7 @@ describe('Overview - Migrate system indices', () => { ], }); - testBed = await setupOverviewPage(); + testBed = await setupOverviewPage(mockEnvironment.httpSetup); const { exists } = testBed; diff --git a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/migrate_system_indices/step_completion.test.ts b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/migrate_system_indices/step_completion.test.ts index 9eb0831c3c7a0..f5aa4006d79a1 100644 --- a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/migrate_system_indices/step_completion.test.ts +++ b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/migrate_system_indices/step_completion.test.ts @@ -13,19 +13,18 @@ import { SYSTEM_INDICES_MIGRATION_POLL_INTERVAL_MS } from '../../../../common/co describe('Overview - Migrate system indices - Step completion', () => { let testBed: OverviewTestBed; - const { server, httpRequestsMockHelpers } = setupEnvironment(); - - afterAll(() => { - server.restore(); + let mockEnvironment: ReturnType; + beforeEach(async () => { + mockEnvironment = setupEnvironment(); }); test(`It's complete when no upgrade is needed`, async () => { - httpRequestsMockHelpers.setLoadSystemIndicesMigrationStatus({ + mockEnvironment.httpRequestsMockHelpers.setLoadSystemIndicesMigrationStatus({ migration_status: 'NO_MIGRATION_NEEDED', }); await act(async () => { - testBed = await setupOverviewPage(); + testBed = await setupOverviewPage(mockEnvironment.httpSetup); }); const { exists, component } = testBed; @@ -36,12 +35,12 @@ describe('Overview - Migrate system indices - Step completion', () => { }); test(`It's incomplete when migration is needed`, async () => { - httpRequestsMockHelpers.setLoadSystemIndicesMigrationStatus({ + mockEnvironment.httpRequestsMockHelpers.setLoadSystemIndicesMigrationStatus({ migration_status: 'MIGRATION_NEEDED', }); await act(async () => { - testBed = await setupOverviewPage(); + testBed = await setupOverviewPage(mockEnvironment.httpSetup); }); const { exists, component } = testBed; @@ -56,11 +55,11 @@ describe('Overview - Migrate system indices - Step completion', () => { jest.useFakeTimers(); // First request should make the step be incomplete - httpRequestsMockHelpers.setLoadSystemIndicesMigrationStatus({ + mockEnvironment.httpRequestsMockHelpers.setLoadSystemIndicesMigrationStatus({ migration_status: 'IN_PROGRESS', }); - testBed = await setupOverviewPage(); + testBed = await setupOverviewPage(mockEnvironment.httpSetup); }); afterEach(() => { @@ -72,7 +71,7 @@ describe('Overview - Migrate system indices - Step completion', () => { expect(exists('migrateSystemIndicesStep-incomplete')).toBe(true); - httpRequestsMockHelpers.setLoadSystemIndicesMigrationStatus({ + mockEnvironment.httpRequestsMockHelpers.setLoadSystemIndicesMigrationStatus({ migration_status: 'NO_MIGRATION_NEEDED', }); diff --git a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/overview.helpers.ts b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/overview.helpers.ts index 30a531091f166..3f56a971b6bf9 100644 --- a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/overview.helpers.ts +++ b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/overview.helpers.ts @@ -7,7 +7,8 @@ import { act } from 'react-dom/test-utils'; import { registerTestBed, TestBed, AsyncTestBedConfig } from '@kbn/test-jest-helpers'; -import { Overview } from '../../../public/application/components/overview'; +import { HttpSetup } from 'src/core/public'; +import { Overview } from '../../../public/application/components'; import { WithAppDependencies } from '../helpers'; const testBedConfig: AsyncTestBedConfig = { @@ -54,9 +55,13 @@ const createActions = (testBed: TestBed) => { }; export const setupOverviewPage = async ( + httpSetup: HttpSetup, overrides?: Record ): Promise => { - const initTestBed = registerTestBed(WithAppDependencies(Overview, overrides), testBedConfig); + const initTestBed = registerTestBed( + WithAppDependencies(Overview, httpSetup, overrides), + testBedConfig + ); const testBed = await initTestBed(); return { diff --git a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/overview.test.tsx b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/overview.test.tsx index 2d318f60149d0..5e49fd510686f 100644 --- a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/overview.test.tsx +++ b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/overview.test.tsx @@ -10,17 +10,11 @@ import { OverviewTestBed, setupOverviewPage } from './overview.helpers'; describe('Overview Page', () => { let testBed: OverviewTestBed; - const { server } = setupEnvironment(); - beforeEach(async () => { - testBed = await setupOverviewPage(); + testBed = await setupOverviewPage(setupEnvironment().httpSetup); testBed.component.update(); }); - afterAll(() => { - server.restore(); - }); - describe('Documentation links', () => { test('Has a whatsNew link and it references target version', () => { const { exists, find } = testBed; diff --git a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/upgrade_step/upgrade_step.test.tsx b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/upgrade_step/upgrade_step.test.tsx index 9a4655d9d8ddb..22913a9b07639 100644 --- a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/upgrade_step/upgrade_step.test.tsx +++ b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/upgrade_step/upgrade_step.test.tsx @@ -9,28 +9,28 @@ import { setupEnvironment } from '../../helpers'; import { OverviewTestBed, setupOverviewPage } from '../overview.helpers'; const DEPLOYMENT_URL = 'https://cloud.elastic.co./deployments/bfdad4ef99a24212a06d387593686d63'; -const setupCloudOverviewPage = () => { - return setupOverviewPage({ - plugins: { - cloud: { - isCloudEnabled: true, - deploymentUrl: DEPLOYMENT_URL, - }, - }, - }); -}; describe('Overview - Upgrade Step', () => { let testBed: OverviewTestBed; - const { server, httpRequestsMockHelpers, setServerAsync } = setupEnvironment(); + let mockEnvironment: ReturnType; + const setupCloudOverviewPage = () => { + return setupOverviewPage(mockEnvironment.httpSetup, { + plugins: { + cloud: { + isCloudEnabled: true, + deploymentUrl: DEPLOYMENT_URL, + }, + }, + }); + }; - afterAll(() => { - server.restore(); + beforeEach(async () => { + mockEnvironment = setupEnvironment(); }); describe('On-prem', () => { test('Shows link to setup upgrade docs', async () => { - testBed = await setupOverviewPage(); + testBed = await setupOverviewPage(mockEnvironment.httpSetup); const { exists } = testBed; expect(exists('upgradeSetupDocsLink')).toBe(true); @@ -40,7 +40,7 @@ describe('Overview - Upgrade Step', () => { describe('On Cloud', () => { test('When ready for upgrade, shows upgrade CTA and link to docs', async () => { - httpRequestsMockHelpers.setGetUpgradeStatusResponse({ + mockEnvironment.httpRequestsMockHelpers.setGetUpgradeStatusResponse({ readyForUpgrade: true, details: 'Ready for upgrade', }); @@ -59,7 +59,7 @@ describe('Overview - Upgrade Step', () => { }); test('When not ready for upgrade, the CTA button is disabled', async () => { - httpRequestsMockHelpers.setGetUpgradeStatusResponse({ + mockEnvironment.httpRequestsMockHelpers.setGetUpgradeStatusResponse({ readyForUpgrade: false, details: 'Resolve critical deprecations first', }); @@ -75,10 +75,10 @@ describe('Overview - Upgrade Step', () => { }); test('An error callout is displayed, if status check failed', async () => { - httpRequestsMockHelpers.setGetUpgradeStatusResponse( - {}, - { statusCode: 500, message: 'Status check failed' } - ); + mockEnvironment.httpRequestsMockHelpers.setGetUpgradeStatusResponse(undefined, { + statusCode: 500, + message: 'Status check failed', + }); testBed = await setupCloudOverviewPage(); const { exists, component } = testBed; @@ -90,7 +90,7 @@ describe('Overview - Upgrade Step', () => { }); test('The CTA button displays loading indicator', async () => { - setServerAsync(true); + mockEnvironment.setDelayResponse(true); testBed = await setupCloudOverviewPage(); const { exists, find } = testBed;