Skip to content

Commit

Permalink
[Upgrade Assistant] Refactor kibana deprecation service mocks (#111168)
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonelizabeth authored Sep 7, 2021
1 parent 936e758 commit 48b25a6
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
*/

export { setupEnvironment, WithAppDependencies } from './setup_environment';
export { kibanaDeprecationsServiceHelpers } from './kibana_deprecations_service.mock';
Original file line number Diff line number Diff line change
@@ -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<DeprecationsServiceStart>;
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<DeprecationsServiceStart>;
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'
),
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand All @@ -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: {
Expand All @@ -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);

Expand All @@ -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);

Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<DeprecationsServiceStart>;

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: {
Expand All @@ -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 () => {
Expand All @@ -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', () => {
Expand All @@ -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 () => {
Expand All @@ -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);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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: {
Expand All @@ -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: {
Expand Down

This file was deleted.

Loading

0 comments on commit 48b25a6

Please sign in to comment.