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] Split edit policy helpers file into separate domain oriented files #102383

Merged
merged 3 commits into from
Jun 24, 2021
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

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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 {
createMinAgeActions,
createSavePolicyAction,
createSnapshotPolicyActions,
createTogglePhaseAction,
} from '../../helpers';
import { initTestBed } from '../init_test_bed';

type SetupReturn = ReturnType<typeof setupDeleteTestBed>;

export type DeleteTestBed = SetupReturn extends Promise<infer U> ? U : SetupReturn;

export const setupDeleteTestBed = async () => {
const testBed = await initTestBed();
const { exists } = testBed;

return {
...testBed,
actions: {
togglePhase: createTogglePhaseAction(testBed),
savePolicy: createSavePolicyAction(testBed),
delete: {
isShown: () => exists('delete-phase'),
...createMinAgeActions(testBed, 'delete'),
...createSnapshotPolicyActions(testBed),
},
},
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
import { act } from 'react-dom/test-utils';
import { API_BASE_PATH } from '../../../../common/constants';
import { setupEnvironment } from '../../helpers';
import { EditPolicyTestBed, setup } from '../edit_policy.helpers';
import {
DELETE_PHASE_POLICY,
getDefaultHotPhasePolicy,
NEW_SNAPSHOT_POLICY_NAME,
SNAPSHOT_POLICY_NAME,
} from '../constants';
import { DeleteTestBed, setupDeleteTestBed } from './delete_phase.helpers';

describe('<EditPolicy /> delete phase', () => {
let testBed: EditPolicyTestBed;
let testBed: DeleteTestBed;
const { server, httpRequestsMockHelpers } = setupEnvironment();

afterAll(() => {
Expand All @@ -32,7 +32,7 @@ describe('<EditPolicy /> delete phase', () => {
]);

await act(async () => {
testBed = await setup();
testBed = await setupDeleteTestBed();
});

const { component } = testBed;
Expand All @@ -43,7 +43,7 @@ describe('<EditPolicy /> delete phase', () => {
httpRequestsMockHelpers.setLoadPolicies([getDefaultHotPhasePolicy('my_policy')]);

await act(async () => {
testBed = await setup();
testBed = await setupDeleteTestBed();
});

const { component, actions } = testBed;
Expand All @@ -56,21 +56,6 @@ describe('<EditPolicy /> delete phase', () => {
expect(actions.delete.isShown()).toBeFalsy();
});

test('shows timing after it was enabled', async () => {
httpRequestsMockHelpers.setLoadPolicies([getDefaultHotPhasePolicy('my_policy')]);

await act(async () => {
testBed = await setup();
});

const { component, actions } = testBed;
component.update();

expect(actions.delete.hasMinAgeInput()).toBeFalsy();
await actions.togglePhase('delete');
expect(actions.delete.hasMinAgeInput()).toBeTruthy();
});

describe('wait for snapshot', () => {
test('shows snapshot policy name', () => {
expect(testBed.find('snapshotPolicyCombobox').prop('data-currentvalue')).toEqual([
Expand All @@ -83,7 +68,7 @@ describe('<EditPolicy /> delete phase', () => {
test('updates snapshot policy name', async () => {
const { actions } = testBed;

await actions.delete.setWaitForSnapshotPolicy(NEW_SNAPSHOT_POLICY_NAME);
await actions.delete.setSnapshotPolicy(NEW_SNAPSHOT_POLICY_NAME);
await actions.savePolicy();

const expected = {
Expand Down Expand Up @@ -111,16 +96,16 @@ describe('<EditPolicy /> delete phase', () => {
test('shows a callout when the input is not an existing policy', async () => {
const { actions } = testBed;

await actions.delete.setWaitForSnapshotPolicy('my_custom_policy');
expect(testBed.find('noPoliciesCallout').exists()).toBeFalsy();
expect(testBed.find('policiesErrorCallout').exists()).toBeFalsy();
expect(testBed.find('customPolicyCallout').exists()).toBeTruthy();
await actions.delete.setSnapshotPolicy('my_custom_policy');
expect(actions.delete.hasNoPoliciesCallout()).toBeFalsy();
expect(actions.delete.hasPolicyErrorCallout()).toBeFalsy();
expect(actions.delete.hasCustomPolicyCallout()).toBeTruthy();
});

test('removes the action if field is empty', async () => {
const { actions } = testBed;

await actions.delete.setWaitForSnapshotPolicy('');
await actions.delete.setSnapshotPolicy('');
await actions.savePolicy();

const expected = {
Expand All @@ -146,26 +131,30 @@ describe('<EditPolicy /> delete phase', () => {
// need to call setup on testBed again for it to use a newly defined snapshot policies response
httpRequestsMockHelpers.setLoadSnapshotPolicies([]);
await act(async () => {
testBed = await setup();
testBed = await setupDeleteTestBed();
});

testBed.component.update();
expect(testBed.find('customPolicyCallout').exists()).toBeFalsy();
expect(testBed.find('policiesErrorCallout').exists()).toBeFalsy();
expect(testBed.find('noPoliciesCallout').exists()).toBeTruthy();
const { component, actions } = testBed;
component.update();

expect(actions.delete.hasCustomPolicyCallout()).toBeFalsy();
expect(actions.delete.hasPolicyErrorCallout()).toBeFalsy();
expect(actions.delete.hasNoPoliciesCallout()).toBeTruthy();
});

test('shows a callout when there is an error loading snapshot policies', async () => {
// need to call setup on testBed again for it to use a newly defined snapshot policies response
httpRequestsMockHelpers.setLoadSnapshotPolicies([], { status: 500, body: 'error' });
await act(async () => {
testBed = await setup();
testBed = await setupDeleteTestBed();
});

testBed.component.update();
expect(testBed.find('customPolicyCallout').exists()).toBeFalsy();
expect(testBed.find('noPoliciesCallout').exists()).toBeFalsy();
expect(testBed.find('policiesErrorCallout').exists()).toBeTruthy();
const { component, actions } = testBed;
component.update();

expect(actions.delete.hasCustomPolicyCallout()).toBeFalsy();
expect(actions.delete.hasNoPoliciesCallout()).toBeFalsy();
expect(actions.delete.hasPolicyErrorCallout()).toBeTruthy();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
*/

import { act } from 'react-dom/test-utils';
import { TestBed } from '@kbn/test/jest';

import { licensingMock } from '../../../../../licensing/public/mocks';
import { setupEnvironment } from '../../helpers';
import { EditPolicyTestBed, setup } from '../edit_policy.helpers';
import { initTestBed } from '../init_test_bed';

describe('<EditPolicy /> frozen phase', () => {
let testBed: EditPolicyTestBed;
let testBed: TestBed;
const { server, httpRequestsMockHelpers } = setupEnvironment();

beforeAll(() => {
Expand All @@ -28,28 +29,19 @@ describe('<EditPolicy /> frozen phase', () => {
httpRequestsMockHelpers.setDefaultResponses();

await act(async () => {
testBed = await setup();
testBed = await initTestBed();
Copy link
Contributor

Choose a reason for hiding this comment

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

I love the readability of factory functions that state what they return, and then assigning the return value to a variable with the same name! It makes the code so much more transparent for me. Thanks for making this kind of change everywhere.

});

const { component } = testBed;
component.update();
});

test('shows timing only when enabled', async () => {
const { actions, exists } = testBed;

expect(exists('frozen-phase')).toBe(true);
expect(actions.frozen.hasMinAgeInput()).toBeFalsy();
await actions.togglePhase('frozen');
expect(actions.frozen.hasMinAgeInput()).toBeTruthy();
});

describe('on non-enterprise license', () => {
beforeEach(async () => {
httpRequestsMockHelpers.setDefaultResponses();

await act(async () => {
testBed = await setup({
testBed = await initTestBed({
appServicesContext: {
license: licensingMock.createLicense({ license: { type: 'basic' } }),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
* 2.0.
*/

import { createNodeAllocationActions, createSavePolicyAction, setReplicas } from '../../../helpers';
import {
createNodeAllocationActions,
createReplicasAction,
createSavePolicyAction,
} from '../../../helpers';
import { initTestBed } from '../../init_test_bed';

type SetupReturn = ReturnType<typeof setupGeneralNodeAllocation>;
Expand All @@ -20,7 +24,7 @@ export const setupGeneralNodeAllocation = async () => {
actions: {
...createNodeAllocationActions(testBed, 'warm'),
savePolicy: createSavePolicyAction(testBed),
setReplicas: (value: string) => setReplicas(testBed, 'warm', value),
...createReplicasAction(testBed, 'warm'),
},
};
};
Loading