From 806a66340b10574e48151a2dbe01abf28bcc24dd Mon Sep 17 00:00:00 2001 From: Alison Goryachev Date: Tue, 7 Sep 2021 18:06:21 -0400 Subject: [PATCH] [Upgrade Assistant] Refactor kibana deprecation service mocks (#111168) --- .../client_integration/helpers/index.ts | 1 + .../kibana_deprecations_service.mock.ts | 94 +++++++++++++++++++ .../deprecation_details_flyout.test.ts | 26 +++-- .../deprecations_list.test.ts | 45 ++++----- .../error_handling.test.ts | 35 +++---- .../kibana_deprecations/mocked_responses.ts | 34 ------- .../fix_issues_step/fix_issues_step.test.tsx | 37 +++++--- .../fix_issues_step/mocked_responses.ts | 18 ---- 8 files changed, 172 insertions(+), 118 deletions(-) create mode 100644 x-pack/plugins/upgrade_assistant/__jest__/client_integration/helpers/kibana_deprecations_service.mock.ts delete mode 100644 x-pack/plugins/upgrade_assistant/__jest__/client_integration/kibana_deprecations/mocked_responses.ts diff --git a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/helpers/index.ts b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/helpers/index.ts index 0c30e173ba9941..7b20b8f7504a2e 100644 --- a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/helpers/index.ts +++ b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/helpers/index.ts @@ -6,3 +6,4 @@ */ export { setupEnvironment, WithAppDependencies, kibanaVersion } from './setup_environment'; +export { kibanaDeprecationsServiceHelpers } from './kibana_deprecations_service.mock'; diff --git a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/helpers/kibana_deprecations_service.mock.ts b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/helpers/kibana_deprecations_service.mock.ts new file mode 100644 index 00000000000000..761f950bd7daf5 --- /dev/null +++ b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/helpers/kibana_deprecations_service.mock.ts @@ -0,0 +1,94 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ +import type { DeprecationsServiceStart, DomainDeprecationDetails } from 'kibana/public'; + +const kibanaDeprecations: DomainDeprecationDetails[] = [ + { + correctiveActions: { + manualSteps: ['Step 1', 'Step 2', 'Step 3'], + api: { + method: 'POST', + path: '/test', + }, + }, + domainId: 'test_domain_1', + level: 'critical', + title: 'Test deprecation title 1', + message: 'Test deprecation message 1', + deprecationType: 'config', + }, + { + correctiveActions: { + manualSteps: ['Step 1', 'Step 2', 'Step 3'], + }, + domainId: 'test_domain_2', + level: 'warning', + title: 'Test deprecation title 1', + message: 'Test deprecation message 2', + deprecationType: 'feature', + }, +]; + +const setLoadDeprecations = ({ + deprecationService, + response, + mockRequestErrorMessage, +}: { + deprecationService: jest.Mocked; + response?: DomainDeprecationDetails[]; + mockRequestErrorMessage?: string; +}) => { + const mockResponse = response ? response : kibanaDeprecations; + + if (mockRequestErrorMessage) { + return deprecationService.getAllDeprecations.mockRejectedValue( + new Error(mockRequestErrorMessage) + ); + } + + return deprecationService.getAllDeprecations.mockReturnValue(Promise.resolve(mockResponse)); +}; + +const setResolveDeprecations = ({ + deprecationService, + status, +}: { + deprecationService: jest.Mocked; + status: 'ok' | 'fail'; +}) => { + if (status === 'fail') { + return deprecationService.resolveDeprecation.mockReturnValue( + Promise.resolve({ + status, + reason: 'resolve failed', + }) + ); + } + + return deprecationService.resolveDeprecation.mockReturnValue( + Promise.resolve({ + status, + }) + ); +}; + +export const kibanaDeprecationsServiceHelpers = { + setLoadDeprecations, + setResolveDeprecations, + defaultMockedResponses: { + mockedKibanaDeprecations: kibanaDeprecations, + mockedCriticalKibanaDeprecations: kibanaDeprecations.filter( + (deprecation) => deprecation.level === 'critical' + ), + mockedWarningKibanaDeprecations: kibanaDeprecations.filter( + (deprecation) => deprecation.level === 'warning' + ), + mockedConfigKibanaDeprecations: kibanaDeprecations.filter( + (deprecation) => deprecation.deprecationType === 'config' + ), + }, +}; diff --git a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/kibana_deprecations/deprecation_details_flyout.test.ts b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/kibana_deprecations/deprecation_details_flyout.test.ts index c65f15fda2e3c5..f4f501221e5958 100644 --- a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/kibana_deprecations/deprecation_details_flyout.test.ts +++ b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/kibana_deprecations/deprecation_details_flyout.test.ts @@ -8,13 +8,15 @@ import { act } from 'react-dom/test-utils'; import { deprecationsServiceMock } from 'src/core/public/mocks'; -import { setupEnvironment } from '../helpers'; +import { setupEnvironment, kibanaDeprecationsServiceHelpers } from '../helpers'; import { KibanaTestBed, setupKibanaPage } from './kibana_deprecations.helpers'; -import { kibanaDeprecationsMockResponse } from './mocked_responses'; describe('Kibana deprecation details flyout', () => { let testBed: KibanaTestBed; const { server } = setupEnvironment(); + const { + defaultMockedResponses: { mockedKibanaDeprecations }, + } = kibanaDeprecationsServiceHelpers; const deprecationService = deprecationsServiceMock.createStartContract(); afterAll(() => { @@ -23,9 +25,7 @@ describe('Kibana deprecation details flyout', () => { beforeEach(async () => { await act(async () => { - deprecationService.getAllDeprecations = jest - .fn() - .mockReturnValue(kibanaDeprecationsMockResponse); + kibanaDeprecationsServiceHelpers.setLoadDeprecations({ deprecationService }); testBed = await setupKibanaPage({ services: { @@ -42,7 +42,7 @@ describe('Kibana deprecation details flyout', () => { describe('Deprecation with manual steps', () => { test('renders flyout with manual steps only', async () => { const { find, exists, actions } = testBed; - const manualDeprecation = kibanaDeprecationsMockResponse[1]; + const manualDeprecation = mockedKibanaDeprecations[1]; await actions.table.clickDeprecationAt(1); @@ -61,7 +61,7 @@ describe('Kibana deprecation details flyout', () => { describe('Deprecation with automatic resolution', () => { test('resolves deprecation successfully', async () => { const { find, exists, actions } = testBed; - const quickResolveDeprecation = kibanaDeprecationsMockResponse[0]; + const quickResolveDeprecation = mockedKibanaDeprecations[0]; await actions.table.clickDeprecationAt(0); @@ -93,14 +93,12 @@ describe('Kibana deprecation details flyout', () => { test('handles resolve failure', async () => { const { find, exists, actions } = testBed; - const quickResolveDeprecation = kibanaDeprecationsMockResponse[0]; + const quickResolveDeprecation = mockedKibanaDeprecations[0]; - deprecationService.resolveDeprecation.mockReturnValue( - Promise.resolve({ - status: 'fail', - reason: 'resolve failed', - }) - ); + kibanaDeprecationsServiceHelpers.setResolveDeprecations({ + deprecationService, + status: 'fail', + }); await actions.table.clickDeprecationAt(0); diff --git a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/kibana_deprecations/deprecations_list.test.ts b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/kibana_deprecations/deprecations_list.test.ts index c3e35814099471..9a3b1d1882cc77 100644 --- a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/kibana_deprecations/deprecations_list.test.ts +++ b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/kibana_deprecations/deprecations_list.test.ts @@ -7,35 +7,32 @@ import { act } from 'react-dom/test-utils'; import { deprecationsServiceMock } from 'src/core/public/mocks'; +import type { DeprecationsServiceStart } from 'kibana/public'; -import { setupEnvironment } from '../helpers'; +import { setupEnvironment, kibanaDeprecationsServiceHelpers } from '../helpers'; import { KibanaTestBed, setupKibanaPage } from './kibana_deprecations.helpers'; -import { kibanaDeprecationsMockResponse } from './mocked_responses'; - -const criticalDeprecations = kibanaDeprecationsMockResponse.filter( - (deprecation) => deprecation.level === 'critical' -); -const warningDeprecations = kibanaDeprecationsMockResponse.filter( - (deprecation) => deprecation.level === 'warning' -); -const configDeprecations = kibanaDeprecationsMockResponse.filter( - (deprecation) => deprecation.deprecationType === 'config' -); describe('Kibana deprecations table', () => { let testBed: KibanaTestBed; + let deprecationService: jest.Mocked; + const { server } = setupEnvironment(); - const deprecationService = deprecationsServiceMock.createStartContract(); + const { + mockedKibanaDeprecations, + mockedCriticalKibanaDeprecations, + mockedWarningKibanaDeprecations, + mockedConfigKibanaDeprecations, + } = kibanaDeprecationsServiceHelpers.defaultMockedResponses; afterAll(() => { server.restore(); }); beforeEach(async () => { + deprecationService = deprecationsServiceMock.createStartContract(); + await act(async () => { - deprecationService.getAllDeprecations = jest - .fn() - .mockReturnValue(kibanaDeprecationsMockResponse); + kibanaDeprecationsServiceHelpers.setLoadDeprecations({ deprecationService }); testBed = await setupKibanaPage({ services: { @@ -56,7 +53,7 @@ describe('Kibana deprecations table', () => { const { tableCellsValues } = table.getMetaData('kibanaDeprecationsTable'); - expect(tableCellsValues.length).toEqual(kibanaDeprecationsMockResponse.length); + expect(tableCellsValues.length).toEqual(mockedKibanaDeprecations.length); }); it('refreshes deprecation data', async () => { @@ -72,8 +69,12 @@ describe('Kibana deprecations table', () => { it('shows critical and warning deprecations count', () => { const { find } = testBed; - expect(find('criticalDeprecationsCount').text()).toContain(criticalDeprecations.length); - expect(find('warningDeprecationsCount').text()).toContain(warningDeprecations.length); + expect(find('criticalDeprecationsCount').text()).toContain( + mockedCriticalKibanaDeprecations.length + ); + expect(find('warningDeprecationsCount').text()).toContain( + mockedWarningKibanaDeprecations.length + ); }); describe('Search bar', () => { @@ -83,12 +84,12 @@ describe('Kibana deprecations table', () => { // Show only critical deprecations await actions.searchBar.clickCriticalFilterButton(); const { rows: criticalRows } = table.getMetaData('kibanaDeprecationsTable'); - expect(criticalRows.length).toEqual(criticalDeprecations.length); + expect(criticalRows.length).toEqual(mockedCriticalKibanaDeprecations.length); // Show all deprecations await actions.searchBar.clickCriticalFilterButton(); const { rows: allRows } = table.getMetaData('kibanaDeprecationsTable'); - expect(allRows.length).toEqual(kibanaDeprecationsMockResponse.length); + expect(allRows.length).toEqual(mockedKibanaDeprecations.length); }); it('filters by type', async () => { @@ -99,7 +100,7 @@ describe('Kibana deprecations table', () => { const { rows: configRows } = table.getMetaData('kibanaDeprecationsTable'); - expect(configRows.length).toEqual(configDeprecations.length); + expect(configRows.length).toEqual(mockedConfigKibanaDeprecations.length); }); }); diff --git a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/kibana_deprecations/error_handling.test.ts b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/kibana_deprecations/error_handling.test.ts index 6de1fd339f3cc4..b45f0b0527ac09 100644 --- a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/kibana_deprecations/error_handling.test.ts +++ b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/kibana_deprecations/error_handling.test.ts @@ -8,9 +8,8 @@ import { act } from 'react-dom/test-utils'; import { deprecationsServiceMock } from 'src/core/public/mocks'; -import { setupEnvironment } from '../helpers'; +import { setupEnvironment, kibanaDeprecationsServiceHelpers } from '../helpers'; import { KibanaTestBed, setupKibanaPage } from './kibana_deprecations.helpers'; -import { kibanaDeprecationsMockResponse } from './mocked_responses'; describe('Error handling', () => { let testBed: KibanaTestBed; @@ -23,18 +22,21 @@ describe('Error handling', () => { test('handles plugin error', async () => { await act(async () => { - deprecationService.getAllDeprecations = jest.fn().mockReturnValue([ - ...kibanaDeprecationsMockResponse, - { - domainId: 'failed_plugin_id', - title: 'Failed to fetch deprecations for "failed_plugin_id"', - message: `Failed to get deprecations info for plugin "failed_plugin_id".`, - level: 'fetch_error', - correctiveActions: { - manualSteps: ['Check Kibana server logs for error message.'], + kibanaDeprecationsServiceHelpers.setLoadDeprecations({ + deprecationService, + response: [ + ...kibanaDeprecationsServiceHelpers.defaultMockedResponses.mockedKibanaDeprecations, + { + domainId: 'failed_plugin_id', + title: 'Failed to fetch deprecations for "failed_plugin_id"', + message: `Failed to get deprecations info for plugin "failed_plugin_id".`, + level: 'fetch_error', + correctiveActions: { + manualSteps: ['Check Kibana server logs for error message.'], + }, }, - }, - ]); + ], + }); testBed = await setupKibanaPage({ services: { @@ -57,9 +59,10 @@ describe('Error handling', () => { test('handles request error', async () => { await act(async () => { - deprecationService.getAllDeprecations = jest - .fn() - .mockRejectedValue(new Error('Internal Server Error')); + kibanaDeprecationsServiceHelpers.setLoadDeprecations({ + deprecationService, + mockRequestErrorMessage: 'Internal Server Error', + }); testBed = await setupKibanaPage({ services: { diff --git a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/kibana_deprecations/mocked_responses.ts b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/kibana_deprecations/mocked_responses.ts deleted file mode 100644 index 261fe719bf5bfe..00000000000000 --- a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/kibana_deprecations/mocked_responses.ts +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ -import type { DomainDeprecationDetails } from 'kibana/public'; - -export const kibanaDeprecationsMockResponse: DomainDeprecationDetails[] = [ - { - correctiveActions: { - manualSteps: ['Step 1', 'Step 2', 'Step 3'], - api: { - method: 'POST', - path: '/test', - }, - }, - domainId: 'test_domain_1', - level: 'critical', - title: 'Test deprecation title 1', - message: 'Test deprecation message 1', - deprecationType: 'config', - }, - { - correctiveActions: { - manualSteps: ['Step 1', 'Step 2', 'Step 3'], - }, - domainId: 'test_domain_2', - level: 'warning', - title: 'Test deprecation title 1', - message: 'Test deprecation message 2', - deprecationType: 'feature', - }, -]; 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 e39f0da488205b..77d3862b9e9628 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 @@ -8,22 +8,24 @@ import { act } from 'react-dom/test-utils'; import { deprecationsServiceMock } from 'src/core/public/mocks'; -import * as mockedResponses from './mocked_responses'; -import { setupEnvironment } from '../../helpers'; +import { setupEnvironment, kibanaDeprecationsServiceHelpers } from '../../helpers'; import { OverviewTestBed, setupOverviewPage } from '../overview.helpers'; +import { esDeprecations, esDeprecationsEmpty } from './mocked_responses'; describe('Overview - Fix deprecation issues step', () => { let testBed: OverviewTestBed; const { server, httpRequestsMockHelpers } = setupEnvironment(); + const { + mockedCriticalKibanaDeprecations, + mockedWarningKibanaDeprecations, + } = kibanaDeprecationsServiceHelpers.defaultMockedResponses; beforeEach(async () => { - httpRequestsMockHelpers.setLoadEsDeprecationsResponse(mockedResponses.esDeprecations); + httpRequestsMockHelpers.setLoadEsDeprecationsResponse(esDeprecations); await act(async () => { const deprecationService = deprecationsServiceMock.createStartContract(); - deprecationService.getAllDeprecations = jest - .fn() - .mockReturnValue(mockedResponses.kibanaDeprecations); + kibanaDeprecationsServiceHelpers.setLoadDeprecations({ deprecationService }); testBed = await setupOverviewPage({ services: { @@ -52,7 +54,7 @@ describe('Overview - Fix deprecation issues step', () => { }); test(`Hides deprecation counts if it doesn't have any`, async () => { - httpRequestsMockHelpers.setLoadEsDeprecationsResponse(mockedResponses.esDeprecationsEmpty); + httpRequestsMockHelpers.setLoadEsDeprecationsResponse(esDeprecationsEmpty); await act(async () => { testBed = await setupOverviewPage(); @@ -166,14 +168,18 @@ describe('Overview - Fix deprecation issues step', () => { const { exists, find } = testBed; expect(exists('kibanaStatsPanel')).toBe(true); - expect(find('kibanaStatsPanel.warningDeprecations').text()).toContain('1'); - expect(find('kibanaStatsPanel.criticalDeprecations').text()).toContain('1'); + expect(find('kibanaStatsPanel.warningDeprecations').text()).toContain( + mockedWarningKibanaDeprecations.length + ); + expect(find('kibanaStatsPanel.criticalDeprecations').text()).toContain( + mockedCriticalKibanaDeprecations.length + ); }); test(`Hides deprecation count if it doesn't have any`, async () => { await act(async () => { const deprecationService = deprecationsServiceMock.createStartContract(); - deprecationService.getAllDeprecations = jest.fn().mockRejectedValue([]); + kibanaDeprecationsServiceHelpers.setLoadDeprecations({ deprecationService, response: [] }); testBed = await setupOverviewPage({ services: { @@ -184,7 +190,9 @@ describe('Overview - Fix deprecation issues step', () => { }); }); - const { exists } = testBed; + const { exists, component } = testBed; + + component.update(); expect(exists('noDeprecationsLabel')).toBe(true); expect(exists('kibanaStatsPanel.warningDeprecations')).toBe(false); @@ -204,9 +212,10 @@ describe('Overview - Fix deprecation issues step', () => { test('Handles network failure', async () => { await act(async () => { const deprecationService = deprecationsServiceMock.createStartContract(); - deprecationService.getAllDeprecations = jest - .fn() - .mockRejectedValue(new Error('Internal Server Error')); + kibanaDeprecationsServiceHelpers.setLoadDeprecations({ + deprecationService, + mockRequestErrorMessage: 'Internal Server Error', + }); testBed = await setupOverviewPage({ services: { diff --git a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/fix_issues_step/mocked_responses.ts b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/fix_issues_step/mocked_responses.ts index 57373dbf072695..ece52b344cc227 100644 --- a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/fix_issues_step/mocked_responses.ts +++ b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/fix_issues_step/mocked_responses.ts @@ -5,7 +5,6 @@ * 2.0. */ -import type { DomainDeprecationDetails } from 'kibana/public'; import { ESUpgradeStatus } from '../../../../common/types'; export const esDeprecations: ESUpgradeStatus = { @@ -37,20 +36,3 @@ export const esDeprecationsEmpty: ESUpgradeStatus = { totalCriticalDeprecations: 0, deprecations: [], }; - -export const kibanaDeprecations: DomainDeprecationDetails[] = [ - { - title: 'mock-deprecation-title', - correctiveActions: { manualSteps: ['test-step'] }, - domainId: 'xpack.spaces', - level: 'critical', - message: 'Sample warning deprecation', - }, - { - title: 'mock-deprecation-title', - correctiveActions: { manualSteps: ['test-step'] }, - domainId: 'xpack.spaces', - level: 'warning', - message: 'Sample warning deprecation', - }, -];