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

[ILM] Clean up remaining js files and any typings #76803

Merged
merged 3 commits into from
Sep 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { PolicyFromES } from '../../../public/application/services/policies/types';
import { PolicyFromES } from '../../../common/types';

export const POLICY_NAME = 'my_policy';
export const SNAPSHOT_POLICY_NAME = 'my_snapshot_policy';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React from 'react';
import React, { ReactElement } from 'react';
import { act } from 'react-dom/test-utils';
import moment from 'moment-timezone';
// axios has a $http like interface so using it to simulate $http

import { findTestSubject } from '@elastic/eui/lib/test';
import { mountWithIntl } from 'test_utils/enzyme_helpers';
import { SinonFakeServer } from 'sinon';
import { ReactWrapper } from 'enzyme';
import axios from 'axios';
import axiosXhrAdapter from 'axios/lib/adapters/xhr';
import { findTestSubject } from '@elastic/eui/lib/test';

import { init as initHttpRequests } from './helpers/http_requests';
import {
Expand All @@ -19,11 +22,11 @@ import {
} from '../../../../../src/core/public/mocks';
import { usageCollectionPluginMock } from '../../../../../src/plugins/usage_collection/public/mocks';

import { mountWithIntl } from '../../../../test_utils/enzyme_helpers';
import { EditPolicy } from '../../public/application/sections/edit_policy/edit_policy';
import { init as initHttp } from '../../public/application/services/http';
import { init as initUiMetric } from '../../public/application/services/ui_metric';
import { init as initNotification } from '../../public/application/services/notification';
import { PolicyFromES } from '../../common/types';
import {
positiveNumbersAboveZeroErrorMessage,
positiveNumberRequiredMessage,
Expand All @@ -38,16 +41,24 @@ import {
policyNameAlreadyUsedErrorMessage,
maximumDocumentsRequiredMessage,
} from '../../public/application/services/policies/policy_validation';
import { HttpResponse } from './helpers/http_requests';
import { createMemoryHistory } from 'history';

// @ts-ignore
initHttp(axios.create({ adapter: axiosXhrAdapter }));
initUiMetric(usageCollectionPluginMock.createSetupContract());
initNotification(
notificationServiceMock.createSetupContract().toasts,
fatalErrorsServiceMock.createSetupContract()
);

let server;
let httpRequestsMockHelpers;
const history = createMemoryHistory();
let server: SinonFakeServer;
let httpRequestsMockHelpers: {
setPoliciesResponse: (response: HttpResponse) => void;
setNodesListResponse: (response: HttpResponse) => void;
setNodesDetailsResponse: (nodeAttributes: string, response: HttpResponse) => void;
};
const policy = {
phases: {
hot: {
Expand All @@ -60,32 +71,33 @@ const policy = {
},
},
};
const policies = [];
const policies: PolicyFromES[] = [];
for (let i = 0; i < 105; i++) {
policies.push({
version: i,
modified_date: moment().subtract(i, 'days').valueOf(),
linkedIndices: i % 2 === 0 ? [`index${i}`] : null,
modified_date: moment().subtract(i, 'days').toISOString(),
linkedIndices: i % 2 === 0 ? [`index${i}`] : undefined,
name: `testy${i}`,
policy: {
...policy,
name: `testy${i}`,
},
});
}
window.scrollTo = jest.fn();
window.TextEncoder = null;
let component;
const activatePhase = async (rendered, phase) => {

let component: ReactElement;
const activatePhase = async (rendered: ReactWrapper, phase: string) => {
const testSubject = `enablePhaseSwitch-${phase}`;
await act(async () => {
await findTestSubject(rendered, testSubject).simulate('click');
});
rendered.update();
};
const expectedErrorMessages = (rendered, expectedErrorMessages) => {
const expectedErrorMessages = (rendered: ReactWrapper, expectedMessages: string[]) => {
const errorMessages = rendered.find('.euiFormErrorText');
expect(errorMessages.length).toBe(expectedErrorMessages.length);
expectedErrorMessages.forEach((expectedErrorMessage) => {
expect(errorMessages.length).toBe(expectedMessages.length);
expectedMessages.forEach((expectedErrorMessage) => {
let foundErrorMessage;
for (let i = 0; i < errorMessages.length; i++) {
if (errorMessages.at(i).text() === expectedErrorMessage) {
Expand All @@ -95,42 +107,37 @@ const expectedErrorMessages = (rendered, expectedErrorMessages) => {
expect(foundErrorMessage).toBe(true);
});
};
const noRollover = (rendered) => {
const noRollover = (rendered: ReactWrapper) => {
findTestSubject(rendered, 'rolloverSwitch').simulate('click');
rendered.update();
};
const getNodeAttributeSelect = (rendered, phase) => {
const getNodeAttributeSelect = (rendered: ReactWrapper, phase: string) => {
return rendered.find(`select#${phase}-selectedNodeAttrs`);
};
const setPolicyName = (rendered, policyName) => {
const setPolicyName = (rendered: ReactWrapper, policyName: string) => {
const policyNameField = findTestSubject(rendered, 'policyNameField');
policyNameField.simulate('change', { target: { value: policyName } });
rendered.update();
};
const setPhaseAfter = (rendered, phase, after) => {
const setPhaseAfter = (rendered: ReactWrapper, phase: string, after: string) => {
const afterInput = rendered.find(`input#${phase}-selectedMinimumAge`);
afterInput.simulate('change', { target: { value: after } });
rendered.update();
};
const setPhaseIndexPriority = (rendered, phase, priority) => {
const setPhaseIndexPriority = (rendered: ReactWrapper, phase: string, priority: string) => {
const priorityInput = rendered.find(`input#${phase}-phaseIndexPriority`);
priorityInput.simulate('change', { target: { value: priority } });
rendered.update();
};
const save = (rendered) => {
const save = (rendered: ReactWrapper) => {
const saveButton = findTestSubject(rendered, 'savePolicyButton');
saveButton.simulate('click');
rendered.update();
};
describe('edit policy', () => {
beforeEach(() => {
component = (
<EditPolicy
history={{ push: () => {} }}
getUrlForApp={() => {}}
policies={policies}
policyName={''}
/>
<EditPolicy history={history} getUrlForApp={jest.fn()} policies={policies} policyName={''} />
);
({ server, httpRequestsMockHelpers } = initHttpRequests());

Expand Down Expand Up @@ -162,8 +169,8 @@ describe('edit policy', () => {
<EditPolicy
policyName={'testy0'}
policies={policies}
getUrlForApp={() => {}}
history={{ push: () => {} }}
getUrlForApp={jest.fn()}
history={history}
/>
);
const rendered = mountWithIntl(component);
Expand Down Expand Up @@ -272,7 +279,7 @@ describe('edit policy', () => {
const rendered = mountWithIntl(component);
noRollover(rendered);
setPolicyName(rendered, 'mypolicy');
setPhaseIndexPriority(rendered, 'hot', -1);
setPhaseIndexPriority(rendered, 'hot', '-1');
save(rendered);
expectedErrorMessages(rendered, [positiveNumberRequiredMessage]);
});
Expand Down Expand Up @@ -300,7 +307,7 @@ describe('edit policy', () => {
noRollover(rendered);
setPolicyName(rendered, 'mypolicy');
await activatePhase(rendered, 'warm');
setPhaseAfter(rendered, 'warm', 0);
setPhaseAfter(rendered, 'warm', '0');
save(rendered);
expectedErrorMessages(rendered, []);
});
Expand All @@ -309,7 +316,7 @@ describe('edit policy', () => {
noRollover(rendered);
setPolicyName(rendered, 'mypolicy');
await activatePhase(rendered, 'warm');
setPhaseAfter(rendered, 'warm', -1);
setPhaseAfter(rendered, 'warm', '-1');
save(rendered);
expectedErrorMessages(rendered, [positiveNumberRequiredMessage]);
});
Expand All @@ -318,8 +325,8 @@ describe('edit policy', () => {
noRollover(rendered);
setPolicyName(rendered, 'mypolicy');
await activatePhase(rendered, 'warm');
setPhaseAfter(rendered, 'warm', 1);
setPhaseIndexPriority(rendered, 'warm', -1);
setPhaseAfter(rendered, 'warm', '1');
setPhaseIndexPriority(rendered, 'warm', '-1');
save(rendered);
expectedErrorMessages(rendered, [positiveNumberRequiredMessage]);
});
Expand All @@ -330,7 +337,7 @@ describe('edit policy', () => {
await activatePhase(rendered, 'warm');
findTestSubject(rendered, 'shrinkSwitch').simulate('click');
rendered.update();
setPhaseAfter(rendered, 'warm', 1);
setPhaseAfter(rendered, 'warm', '1');
const shrinkInput = rendered.find('input#warm-selectedPrimaryShardCount');
shrinkInput.simulate('change', { target: { value: '0' } });
rendered.update();
Expand All @@ -342,7 +349,7 @@ describe('edit policy', () => {
noRollover(rendered);
setPolicyName(rendered, 'mypolicy');
await activatePhase(rendered, 'warm');
setPhaseAfter(rendered, 'warm', 1);
setPhaseAfter(rendered, 'warm', '1');
findTestSubject(rendered, 'shrinkSwitch').simulate('click');
rendered.update();
const shrinkInput = rendered.find('input#warm-selectedPrimaryShardCount');
Expand All @@ -356,7 +363,7 @@ describe('edit policy', () => {
noRollover(rendered);
setPolicyName(rendered, 'mypolicy');
await activatePhase(rendered, 'warm');
setPhaseAfter(rendered, 'warm', 1);
setPhaseAfter(rendered, 'warm', '1');
findTestSubject(rendered, 'forceMergeSwitch').simulate('click');
rendered.update();
const shrinkInput = rendered.find('input#warm-selectedForceMergeSegments');
Expand All @@ -370,7 +377,7 @@ describe('edit policy', () => {
noRollover(rendered);
setPolicyName(rendered, 'mypolicy');
await activatePhase(rendered, 'warm');
setPhaseAfter(rendered, 'warm', 1);
setPhaseAfter(rendered, 'warm', '1');
findTestSubject(rendered, 'forceMergeSwitch').simulate('click');
rendered.update();
const shrinkInput = rendered.find('input#warm-selectedForceMergeSegments');
Expand Down Expand Up @@ -446,7 +453,7 @@ describe('edit policy', () => {
noRollover(rendered);
setPolicyName(rendered, 'mypolicy');
await activatePhase(rendered, 'cold');
setPhaseAfter(rendered, 'cold', 0);
setPhaseAfter(rendered, 'cold', '0');
save(rendered);
expectedErrorMessages(rendered, []);
});
Expand All @@ -455,7 +462,7 @@ describe('edit policy', () => {
noRollover(rendered);
setPolicyName(rendered, 'mypolicy');
await activatePhase(rendered, 'cold');
setPhaseAfter(rendered, 'cold', -1);
setPhaseAfter(rendered, 'cold', '-1');
save(rendered);
expectedErrorMessages(rendered, [positiveNumberRequiredMessage]);
});
Expand Down Expand Up @@ -517,8 +524,8 @@ describe('edit policy', () => {
noRollover(rendered);
setPolicyName(rendered, 'mypolicy');
await activatePhase(rendered, 'cold');
setPhaseAfter(rendered, 'cold', 1);
setPhaseIndexPriority(rendered, 'cold', -1);
setPhaseAfter(rendered, 'cold', '1');
setPhaseIndexPriority(rendered, 'cold', '-1');
save(rendered);
expectedErrorMessages(rendered, [positiveNumberRequiredMessage]);
});
Expand All @@ -529,7 +536,7 @@ describe('edit policy', () => {
noRollover(rendered);
setPolicyName(rendered, 'mypolicy');
await activatePhase(rendered, 'delete');
setPhaseAfter(rendered, 'delete', 0);
setPhaseAfter(rendered, 'delete', '0');
save(rendered);
expectedErrorMessages(rendered, []);
});
Expand All @@ -538,7 +545,7 @@ describe('edit policy', () => {
noRollover(rendered);
setPolicyName(rendered, 'mypolicy');
await activatePhase(rendered, 'delete');
setPhaseAfter(rendered, 'delete', -1);
setPhaseAfter(rendered, 'delete', '-1');
save(rendered);
expectedErrorMessages(rendered, [positiveNumberRequiredMessage]);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import sinon, { SinonFakeServer } from 'sinon';

type HttpResponse = Record<string, any> | any[];
export type HttpResponse = Record<string, any> | any[];

const registerHttpRequestMockHelpers = (server: SinonFakeServer) => {
const setPoliciesResponse = (response: HttpResponse = []) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import {
import { HttpService } from '../../../../../src/core/public/http';
import { usageCollectionPluginMock } from '../../../../../src/plugins/usage_collection/public/mocks';

import { PolicyFromES } from '../../common/types';
import { PolicyTable } from '../../public/application/sections/policy_table/policy_table';
import { init as initHttp } from '../../public/application/services/http';
import { init as initUiMetric } from '../../public/application/services/ui_metric';
import { PolicyFromES } from '../../public/application/services/policies/types';

initHttp(
new HttpService().setup({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import axiosXhrAdapter from 'axios/lib/adapters/xhr';

import { mountWithIntl } from 'test_utils/enzyme_helpers';
import { usageCollectionPluginMock } from '../../../../src/plugins/usage_collection/public/mocks';
import { Index } from '../common/types';
import {
retryLifecycleActionExtension,
removeLifecyclePolicyActionExtension,
Expand All @@ -20,7 +21,6 @@ import {
} from '../public/extend_index_management';
import { init as initHttp } from '../public/application/services/http';
import { init as initUiMetric } from '../public/application/services/ui_metric';
import { Index } from '../public/application/services/policies/types';

// We need to init the http with a mock for any tests that depend upon the http service.
// For example, add_lifecycle_confirm_modal makes an API request in its componentDidMount
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

export * from './policies';
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { Index as IndexInterface } from '../../../../../index_management/public';
import { Index as IndexInterface } from '../../../index_management/common/types';

export interface SerializedPolicy {
name: string;
Expand All @@ -28,7 +28,7 @@ export interface PolicyFromES {
}

export interface SerializedPhase {
min_age: string;
min_age?: string;
actions: {
[action: string]: any;
};
Expand Down Expand Up @@ -94,10 +94,10 @@ export interface SerializedDeletePhase extends SerializedPhase {
}

export interface AllocateAction {
number_of_replicas: number;
number_of_replicas?: number;
include: {};
exclude: {};
require: {
require?: {
[attribute: string]: string;
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
HotPhase,
WarmPhase,
FrozenPhase,
} from '../services/policies/types';
} from '../../../common/types';

export const defaultNewHotPhase: HotPhase = {
phaseEnabled: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { EuiFieldNumber, EuiFlexGroup, EuiFlexItem, EuiFormRow, EuiSelect } from
import { LearnMoreLink } from './learn_more_link';
import { ErrableFormRow } from './form_errors';
import { PhaseValidationErrors, propertyof } from '../../../services/policies/policy_validation';
import { PhaseWithMinAge, Phases } from '../../../services/policies/types';
import { PhaseWithMinAge, Phases } from '../../../../../common/types';

function getTimingLabelForPhase(phase: keyof Phases) {
// NOTE: Hot phase isn't necessary, because indices begin in the hot phase.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { LearnMoreLink } from './learn_more_link';
import { ErrableFormRow } from './form_errors';
import { useLoadNodes } from '../../../services/api';
import { NodeAttrsDetails } from './node_attrs_details';
import { PhaseWithAllocationAction, Phases } from '../../../services/policies/types';
import { PhaseWithAllocationAction, Phases } from '../../../../../common/types';
import { PhaseValidationErrors, propertyof } from '../../../services/policies/policy_validation';

const learnMoreLink = (
Expand Down
Loading