Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get rid of axios dependency in the Upgrade Assistant tests. #127122

Merged
merged 3 commits into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -39,8 +40,14 @@ const createActions = (testBed: TestBed) => {
};
};

export const setupAppPage = async (overrides?: Record<string, unknown>): Promise<AppTestBed> => {
const initTestBed = registerTestBed(WithAppDependencies(App, overrides), testBedConfig);
export const setupAppPage = async (
httpSetup: HttpSetup,
overrides?: Record<string, unknown>
): Promise<AppTestBed> => {
const initTestBed = registerTestBed(
WithAppDependencies(App, httpSetup, overrides),
testBedConfig
);
const testBed = await initTestBed();

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,14 @@ import { AppTestBed, setupAppPage } from './app.helpers';

describe('Cluster upgrade', () => {
let testBed: AppTestBed;
let server: ReturnType<typeof setupEnvironment>['server'];
let httpRequestsMockHelpers: ReturnType<typeof setupEnvironment>['httpRequestsMockHelpers'];

beforeEach(() => {
({ server, httpRequestsMockHelpers } = setupEnvironment());
});

afterEach(() => {
server.restore();
let mockEnvironment: ReturnType<typeof setupEnvironment>;
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', () => {
Expand All @@ -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: {
Expand All @@ -52,7 +46,7 @@ describe('Cluster upgrade', () => {
});

await act(async () => {
testBed = await setupAppPage();
testBed = await setupAppPage(mockEnvironment.httpSetup);
});
});

Expand All @@ -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: {
Expand All @@ -76,7 +70,7 @@ describe('Cluster upgrade', () => {
});

await act(async () => {
testBed = await setupAppPage();
testBed = await setupAppPage(mockEnvironment.httpSetup);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -65,10 +66,11 @@ const createActions = (testBed: TestBed) => {
};

export const setupESDeprecationLogsPage = async (
httpSetup: HttpSetup,
overrides?: Record<string, unknown>
): Promise<EsDeprecationLogsTestBed> => {
const initTestBed = registerTestBed(
WithAppDependencies(EsDeprecationLogs, overrides),
WithAppDependencies(EsDeprecationLogs, httpSetup, overrides),
testBedConfig
);
const testBed = await initTestBed();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,16 @@ const getLoggingResponse = (toggle: boolean): DeprecationLoggingStatus => ({

describe('ES deprecation logs', () => {
let testBed: EsDeprecationLogsTestBed;
const { server, httpRequestsMockHelpers } = setupEnvironment();

let mockEnvironment: ReturnType<typeof setupEnvironment>;
beforeEach(async () => {
httpRequestsMockHelpers.setLoadDeprecationLoggingResponse(getLoggingResponse(true));
testBed = await setupESDeprecationLogsPage();
mockEnvironment = setupEnvironment();
mockEnvironment.httpRequestsMockHelpers.setLoadDeprecationLoggingResponse(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of destructing httpRequestsMockHelpers from mockEnvironment? I think that will reduce the noise of the diff and also make things easier to follow since the changes will be scoped to the setup of the test rather the implementation of each.

I think this change can be applied throughout most of the test files in this PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, good point, let me do that!

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;
Expand All @@ -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;
Expand All @@ -102,7 +105,7 @@ describe('ES deprecation logs', () => {

const { actions, exists } = testBed;

httpRequestsMockHelpers.setUpdateDeprecationLoggingResponse(undefined, error);
mockEnvironment.httpRequestsMockHelpers.setUpdateDeprecationLoggingResponse(undefined, error);

await actions.clickDeprecationToggle();

Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -280,7 +287,7 @@ describe('ES deprecation logs', () => {

expect(exists('errorCallout')).toBe(true);

httpRequestsMockHelpers.setLoadDeprecationLogsCountResponse({
mockEnvironment.httpRequestsMockHelpers.setLoadDeprecationLogsCountResponse({
count: 0,
});

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

Expand All @@ -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: {
Expand All @@ -344,7 +351,7 @@ describe('ES deprecation logs', () => {

component.update();

httpRequestsMockHelpers.setLoadDeprecationLogsCountResponse({ count: 0 });
mockEnvironment.httpRequestsMockHelpers.setLoadDeprecationLogsCountResponse({ count: 0 });

await actions.clickResetButton();

Expand All @@ -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(() => {
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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: {
Expand Down
Loading